Python - Sift Class SIFT.compute - python

I am trying to retrieve descriptors for key points that I have already found. I am using the following lines of code:
sift = cv2.SIFT()
self.features,des = sift.compute(self.gray,self.features)
However, I am receiving the error:
'cv2.SIFT' object has no attribute 'compute'
I don't understand why, because according to this link (http://docs.opencv.org/trunk/modules/nonfree/doc/feature_detection.html), this should be possible.
Would anyone know what is going on here?
I also noticed that:
cv2.SIFT.detectAndCompute(image, mask[, descriptors[, useProvidedKeypoints]]) → keypoints, descriptors¶
allows you to use already detected keypoints... would someone be able to tell me how to input the arguments correctly for this function to work? I don't need to use the mask argument by the way.
Thank you for your help.

The link you just gave, is for OpenCV version 3, not for version 2, that you are using. Thus, any differences in functions or library structure is adequately explained as you are not using the same version (sift = cv2.SIFT()).

Related

Keeping alias types simple in Python documentation?

I'm trying to use the typing module to document my Python package, and I have a number of situations where several different types are allowable for a function parameter. For instance, you can either pass a number, an Envelope object (one of the classes in my package), or a list of numbers from which an Envelope is constructed, or a list of lists of numbers from which an envelope is constructed. So I make an alias type as follows:
NumberOrEnvelope = Union[Sequence[Real], Sequence[Sequence[Real]], Real, Envelope]
Then I write the function:
def example_function(parameter: NumberOrEnvelope):
...
And that looks great to me. However, when I create the documentation using Sphinx, I end up with this horrifically unreadable function signature:
example_function(parameter: Union[Sequence[numbers.Real], Sequence[Sequence[numbers.Real]], numbers.Real, expenvelope.envelope.Envelope])
Same thing also with the hints that pop up when I start to try to use the function in PyCharm.
Is there some way I can have it just leave it as "NumberOrEnvelope". Ideally that would also link in the documentation to a clarification of what "NumberOrEnvelope" is, though even if it didn't it would be way better than what's appearing now.
I had the same issue and used https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases, introduced in version 3.3.
In your sphinx conf.py, insert this section. It does not seem to make much sense at the first sight, but does the trick:
autodoc_type_aliases = dict(NumberOrEnvelope='NumberOrEnvelope')
Warning: It only works in modules that start with from __future__ import annotation
Note: If there is a target in the documentation, type references even have a hyperlink to the definition. I have classes, documented elsewhere with autoclass, which are used as types of function parameters, and the docs show the nice names of the types with links.
Support for this appears to be in the works.
See Issue #6518.
That issue can be closed by the recent updates to Pull Request #8007 (under review).
If you want the fix ASAP, you can perhaps try using that build.
EDIT: This doesn't quite work, sadly.
Turns out after a little more searching, I found what I was looking for. Instead of:
NumberOrEnvelope = Union[Sequence[Real], Sequence[Sequence[Real]], Real, Envelope]
I found that you can create your own compound type that does the same thing:
NumberOrEnvelope = TypeVar("NumberOrEnvelope", Sequence[Real], Sequence[Sequence[Real]], Real, Envelope)
This displays in documentation as "NumberOrEnvelope", just as I wanted.

How to get description on Python Method?

I know that to get help for a function, we use help(func) or ?func, but what about a method? simply using help(method) or ?method won't do anything.
I tried using a preloaded object name before a method, it worked. But are there any other way?
I found this: https://www.programiz.com/python-programming/methods/built-in/help
Try these on Python shell.
help('random thing')
help('print')
help('def')
from math import *
help('math.pow')
Each one will show different results - but the last couple should help you understand how it works for what you are asking.
This format is inappropriate ?method and will produce an error. If you're having trouble understanding some Python object then help() will do.
help(func)
help(method)
help(Class.method)

How to implement refining conflict in constraint programming

I use Docplex with python 3.7 to implement constraints programming. when it was infeasible, how can i proceed to list constraints those was to source of the conflict?
mdl.export_as_cpo(out="/home/..../MCP3.lp")
msol = mdl.solve(FailLimit=700000, TimeLimit=1600)
DInfos= msol.get_solver_infos()
mconflict=msol.CpoRefineConflictResult()
mconflict.get_all_member_constraints()
Error message:
mconflict=msol.CpoRefineConflictResult()
AttributeError: 'CpoSolveResult' object has no attribute 'CpoRefineConflictResult'
solve returns a SolveResult, and CpoRefineConflictResult is a class in docplex.cp.solution. So, the error message is correct: a SolveResult does not have an attribute CpoRefineConflictResult. You'd expect the CpoRefineConflictResult as the result of the conflict refiner.
You should probably read through the documentation a bit more http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solution.py.html
You can call the .refine_conflict() method on the CpoSolver object to obtain a CpoRefineConflictResult, as documented here http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solver.solver.py.html#detailed-description
Perhaps you can provide a minimal, reproducible example, if you need a more specific solution to your problem. https://stackoverflow.com/help/minimal-reproducible-example
I have add:
from docplex.cp.solver.solver import CpoSolver
After, i have add those lines if the model is infeasible:
mconfl= CpoSolver(model)
mconf = mconfl.refine_conflict()

Example from NetworkX documentation gets an error (all_pairs_shortest_path)

I tried to copy and paste an example from NetworkX package documentation.
This is the example:
>>>G = nx.path_graph(5)
>>> path = nx.all_pairs_shortest_path(G)
>>> print(path[0][4])
[0, 1, 2, 3, 4]
Unfortunately, instead of the expected output, I get the following error message:
'generator' object has no attribute '__getitem__'
So your error is due to the fact that in Python 2.x many of the methods that used to return dicts now return generators. Among them is all_pairs_shortest_path. You're using this new version of networkx, but looking at an out-of-date tutorial.
So the error message you saw comes from the fact that you have a generator path and you're trying to access path[0], which doesn't make sense to Python. The easiest fix here is to simply follow the answer provided by Walter and say
path = dict(nx.all_pairs_shortest_path(G))
In general, when using code that was written for networkx 1.x, but you are using version 2.x, you should consult the migration guide (though in your case it's not particularly useful).
Looks like path is a generator: convert it into a dictionary and it works:
path = dict(nx.all_pairs_shortest_path(G))

In python, "request.args.get" returns what type of variable?

Please bear with me as I am a beginner in python. I'm using a framework to change values of my drone's parameters. One of the command I would like to use is vehicle.parameters['INJECT_TO_GPS']=100. When I use
vehicle.parameters['GPS_TO_INJECT']=100
It works well and changes it to 100. Now I want to include this in a function (I'm using flask to get the value of a from a web page), so If I use:
def change_value():
a = request.args.get('a', 0)
vehicle.parameters['INJECT_TO_GPS']=a
It does not work, printing me:
error: required argument is not a float
I also tried:
vehicle.parameters['INJECT_TO_GPS']=float(a)
But in this, it complains that it has to be a string...
What is wrong with it ? thanks a lot in advance
To know the type You can use inbuilt function type or isinstance to test that it belongs to that class:
print(type(a))
print(isinstance(a , class_or_type_you_want_check))

Categories