Code completion is not working for OpenCV and Python - python

I am using Ubuntu 14.04. I have installed OpenCV using Adrian Rosebrock's guide. I am also using PyCharm for programming python and opencv.
My problem is that I can use code completion for cv2 modules but code completion wont work for instances initiated from cv2. An example is shown below.
This works:
This does not:
There is no run time error when I write my program as expected. Such that cap.isOpened() works without an error.

Though I am Window user, I also had faced similar problem with you. In my case, I could solve this problem by importing this way:
from cv2 import cv2
As I'm lack of knowledge of how does the python imports module, I can't explain you clearly about why this solve the problem, but it works anyway.
Good luck.

The openCV python module is a dynamically generated wrapper of the underlying c++ library. PyCharm relies on the availability of python source code to provide autocomplete functionality. When the source code is missing (as in the opencv case), pycharm will generate skeleton files with function prototypes and rely on those for autocompletion but with diminished capabilities.
As a result when you autocomplete at
cv2.
it can figure out that the module cv2 has the following members and provide suggestions.
On the other hand when you
cap = cv2.VideoCapture(file_name)
PyCharm can figure out that you just called a method from the cv2 module and assigned it to cap but has no information about the type of the result of this method and does not know where to go look for suggestions for
cap.
If you try the same things in shell mode, you will see the behavior you actually expected to see, since in shell mode will actually introspect live objects (it will ask the created cap object what members it has and provide those as suggestions)
You can also write stubs for the opencv module yourself to enable correct autocompletion in edit mode.
Take a look here

If anyone still is experiencing the issue, downgrading to opencv to 4.5.5.62 helped my case.

I am using PyCharm on windows 10 and faced similar issue on the intellisense for cv2.
This is my solution:
Pycharm>File>Manage IDE settings> Restore Default settings
Restart the Pycharm IDE
Reconfigure Python Interpretor

Related

Why is VS Code showing "windll Windows only: Creates ()" intellisense suggestions?

While writing Python code on VS Code, it is showing suggestions like:
windll Windows only: Creates ()
I have tried to see if this was coming from snippet extension I might have installed but I don't think I have any... can anyone shine a light on why I am getting his code completion nonsense?
I had this same issue, every time :, ., def, or if is typed, there's annoying suggestions that don't make sense. I found out it's the Python Extended extension that's responsible for this. It seems to be reported on it's GitHub issues page, but for now I would disable the extension as it doesn't seem to be working at all for me. I recommend PyLance as a replacement.
It's the extension problem.
You have installed the extension named: Python Extended inside your vs code and you will need to uninstall it.
It works well on my editor after uninstalling this extension.

How to disable OpenCL with wand in python

So I'm using wand for a project and it's been working fine, except whenever I want to use a function that has been 'OpenCL-accelerated' (list of functions here), it stops working, no error or anything, but I'm pretty sure that OpenCL is what's causing it.
So my question is, how can I disable OpenCL in wand so that I can use those functions? Again, I've looked for solutions, but alas I couldn't find anything python specific, and the api module and the python module itself don't seem to mention anything about it either.
Any help would be greatly appreciated, thanks!
Just set the environment variable MAGICK_OCL_DEVICE=off to disable OpenCL.
Either before running the script.
export MAGICK_OCL_DEVICE=OFF
python myScript.py
Or within the python script before invoking any Wand code.
import os
os.environ['MAGICK_OCL_DEVICE'] = 'OFF'
# ... wand stuff ...
And the reason that it looks like your application stops running is that ImageMagick will need to run an OpenCL benchmark the first time and that can take a while. The result of the benchmark will be cached and used the next time an "OpenCL-accelerated" method is executed.

Python Code Completion for C++ Libraries (Using Pycharm, VTK)

I'm trying to get back into coding for some math/physics experimentations and found VTK as a powerful tool using python. So I installed Python(x,y) and Pycharm Community edition. But I cannot get the Code Completion for VTK to work. I know this question has been posted quite a lot of times, but I couldn't find any concrete answer.
Here's what I know so far:
In order for Code Completion to work Pycharm constructs Skeletons. (Basically Python files with empty Classes/Methods that match the C++ API and can then be used like any other Python file for code completion.)
If I locate these files they don't appear to be complete and look something like this:
If this is indeed the skeleton (the file is called vtkRenderingPython.py) then shouldn't there be empty function declarations?
The result is that I get code completion for the classnames, but not the functions. For a library this huge that's rather annoying. Is there an easy way to get this working, or is this just a limitation I have to live with? Is there maybe a way to get complete Skeletons and replace the ones I have here? Am I missing the point entirely?
After another couple of hours I tried my luck with the PyDev extension for Eclipse. I didn't think that would work, but to my surprise it did! No settings necessary, it just worked out of the box.
The only drawback is that inherited methods are not shown in code completion. The base class is shown in the documentation window though so you can get the available functions by creating a temporary object of the base class and scrolling through the code completion there.

PyDev does not highlight and autocomplete Numpy correctly

I am using a mac, I set up all the external libraries through PYTHONPATH, I tried setting it through Pydev interpreter as well.
It simply does not work. The odd thing is that pydev is highlighting the syntax as error, but when I run the code, it works perfectly. I am out of ideas of what the problem might be. I used to program in java with eclipse, and once you set up the external libraries all the highlighting and autocompletion is working. With pydev I have no idea what is wrong.
Here is the image of the wrong highlighting:
There's a similar problem with importing wxPython with PyDev Aptana Studio 3. Based on the research I did then, PyDev has a hard time validating new imports when a module has recently been downloaded. This means that there is just a bug in PyDev, so it would be a good idea to report it.
As for the solution, from what I've found a useful link with a ton of answers that should fix your problem. There's some detailed instructions but the fix that I found was towards the bottom someone said to try wx = wx after importing wx. This worked for me, but may not work for you so here is the link if you need a better answer:
Undefined variable from import when using wxPython in pydev

Why is OpenCV python crashing in Windows?

I am running OpenCV2.2 on Windows 7 64-bit.
I built OpenCV using cmake with MinGW 32-bit.
I have no problem with the C/C++ build. However the python bindings seem to have some problems. Most of the python samples are crashing.
The HighGui functions seem to cause trouble. Python crashes when I try to read an image (using imread, LoadImage or LoadImageM), display an image (imshow or ShowImage) or even create and manipulate windows (NamedWindow, MoveWindow, etc).\
WaitKey doesn't crash python but it always returns a -1 immediately, no matter how long the delay I provide. Similarly imgproc functionality listed under "Image Filtering" in the reference seem to crash python. I haven't been able to test it with too many other functions, mainly because I can't load in images and I am forced to test it with matrices i have built myself.
I am currently using python 2.6.6 (32-bit) , but I got the same problem with python 2.7 (32-bit). I also tried building the python wrappers for OpenCV2.1, and I got the same problem. It's very likely a problem specific to my setup. I've been cracking my head against this problem for quite some time now, I would appreciate any help.
Thanking you in advance.
Regards,
Anush.

Categories