11

I am trying to install OpenCv and run some code on PyCharm on Mac.

I have installed the opencv package as well as numpy but when I run the code this error shows up on Pycharm IDE: "cannot find reference 'VideoCapture' in __init__.py"

In fact, every time I write example_variable = cv2., PyCharm is not giving me any option to autocomplete with a function attached to cv2. Why is this happening?

Here's the code I am trying to run. I tried also running other codes but the same error displays:

import cv2
import numpy as np

cap = cv2.VideoCapture()

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',gray)

    if cv2.waitKey(1) & OxFF == ord('q'):
        break

cap.release
cv2.destroyAllWindows()

Any tips on how to run this code smoothly? The end goal is to have my front webcam capture live video and react to it.

Neuron
  • 5,141
  • 5
  • 38
  • 59
FiloPietra
  • 111
  • 1
  • 1
  • 4
  • Can you provide information on how you installed OpenCV? Because there are various ways of which some are working only with python 2 and causing those kind of problems for python 3. Especially when using the pip version of OpenCV (`opencv-python`) which works I guess only with python 2 correctly. – malliaridis Jul 14 '20 at 15:56
  • [This](https://stackoverflow.com/a/69849595/3690582) solution worked for me on PyCharm 2022.1. – greatertomi Jul 22 '22 at 23:42

6 Answers6

9

Use this command line to avoid the reference issue

pip install --force-reinstall --no-cache -U opencv-python==4.5.5.62

Kraego
  • 2,978
  • 2
  • 22
  • 34
akshay
  • 91
  • 1
  • 3
5

Try one of this solutions:

  1. Right-click on source Directory
  2. Mark Directory as -> source root
  3. File --> Invalidate Caches / Restart... -> Invalidate and Restart

OR:

  1. In your IDE, go to settings -> preferences -> django
  2. Ensure your Root directory, your settings.py and your manage.py are in the right place if you are in Django.

After all that, you can also delete the .idea folder in your root directory and restart the IDE.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
MDT
  • 59
  • 1
  • 2
2

Instead of import cv2 use from cv2 import cv2

Ushan Bro
  • 53
  • 1
0

I agree with "from cv2 import cv2"

there have an another way to solve problems:

** Do Not Use Pip Install **

I try to use download opencv_python-4.5.5.62-cp36-abi3-win_amd64.whl file and then manual double click to install

wait a minute, Pycharm IDE successfully Show code completion.


by the way,can your code run?

if code can run, above will help you a bit.

and your path(project python) should be Englisgh ; not be Chinese or other language.

0

This worked for

  1. Before I was using python from Anaconda, instead
  2. Choose the default python interpreter
Peter sunny
  • 167
  • 1
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 24 '22 at 05:33
0

It worked for me by the following method:

from the directory /xx/.conda/envs/mmrot/lib/python3.8/site-packages/cv2/ copy cv2.abi3.so file to the parent dir /xx/.conda/envs/mmrot/lib/python3.8/site-packages/

Even though this doesn't make the reason clear, but it does worked.

EthanLee
  • 1
  • 1