15

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 one works.

This does not:

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.

Nelewout
  • 6,281
  • 3
  • 29
  • 39
Justin Case
  • 189
  • 1
  • 1
  • 8
  • the link the the [guide](http://www.pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/) mentioned. – Justin Case Mar 29 '17 at 12:23

4 Answers4

13

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.

kimDragonHyeon
  • 414
  • 5
  • 9
  • ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2 – huang Jul 16 '23 at 15:13
  • 1
    https://stackoverflow.com/a/75054982/8423105 Although this is not a solution that directly addresses your problem, this method also helps to solve the problem. – kimDragonHyeon Jul 16 '23 at 16:57
8

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

Nelewout
  • 6,281
  • 3
  • 29
  • 39
Giannis Spiliopoulos
  • 2,628
  • 18
  • 27
  • As it appears, in the python console of PyCharm auto-complete is working as intended just like you mentioned. However, this doesn't solve my problem for when I try to utilize autocomplete in the editor. Is there a way to enforce introspection also in the editor? – Justin Case Apr 11 '17 at 07:19
  • @Justin, I disagree that this answers your question. It both, explains the behavior you see and provides a way to get to the behavior you want (writing stubs for the opencv module). As this a non-trivial amount of work, you should not expect me to provide it. As for introspection in the editor that would be difficult and dangerous since anything you wrote in the editor would have to be first evaluated in a python interpreter. – Giannis Spiliopoulos Apr 11 '17 at 19:52
1

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

  • I suffered this with opencv-python 4.6.0.66, MiniConda, Ubuntu 20.04, and PyCharm 2022.2.3. Inspired by this suggestion, I downgraded it to 4.5.3.56 and all stubs work well in PyCharm. – cmpltrtok Dec 03 '22 at 10:40
0

I am using PyCharm on windows 10 and faced similar issue on the intellisense for cv2.

This is my solution:

  1. Pycharm>File>Manage IDE settings> Restore Default settings
  2. Restart the Pycharm IDE
  3. Reconfigure Python Interpretor

enter image description here

enter image description here

edwin
  • 97
  • 1
  • 3