Code completion is not working for OpenCV and Python

烂漫一生 提交于 2020-06-11 20:00:35

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/43093400/code-completion-is-not-working-for-opencv-and-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!