Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code completion is not working for OpenCV and 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 one works.

But this one wouldn't.

There is no run time error when I write my program as expected. Such that cap.isOpened() works without an error.

like image 921
Justin Case Avatar asked Mar 29 '17 12:03

Justin Case


2 Answers

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.

like image 181
kimDragonHyeon Avatar answered Sep 18 '22 14:09

kimDragonHyeon


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

like image 44
Giannis Spiliopoulos Avatar answered Sep 17 '22 14:09

Giannis Spiliopoulos