Code completion is not working for OpenCV and Python
Asked Answered
H

4

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.

Hooper answered 29/3, 2017 at 12:22 Comment(1)
the link the the guide mentioned.Hooper
H
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.

Hyperparathyroidism answered 16/4, 2020 at 4:15 Comment(2)
ImportError: Bindings generation error. Submodule name should always start with a parent module name. Parent name: cv2.cv2. Submodule name: cv2Ofilia
https://mcmap.net/q/428863/-opencv-autocomplete-not-working-on-pycharm Although this is not a solution that directly addresses your problem, this method also helps to solve the problem.Hyperparathyroidism
L
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

Lamellicorn answered 4/4, 2017 at 14:27 Comment(2)
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?Hooper
@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.Lamellicorn
P
1

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

Poulterer answered 26/11, 2022 at 10:6 Comment(1)
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.Argentic
R
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

Roxie answered 5/12, 2020 at 14:28 Comment(2)
This feels like a sledge hammer. It would be nice to find which setting causes the issue rather than nuking all of my settings.Elidaelidad
The screenshot doesn't even show any improvement over what the OP already had in the first placeKatherine

© 2022 - 2024 — McMap. All rights reserved.