Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 Opencv set up problem: "cannot find reference 'VideoCapture' in __init__.py" on Pycharm IDE

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.

like image 539
FiloPietra Avatar asked Mar 28 '26 12:03

FiloPietra


2 Answers

Use this command line to avoid the reference issue

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

like image 125
akshay Avatar answered Mar 30 '26 02:03

akshay


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.

like image 45
MDT Avatar answered Mar 30 '26 00:03

MDT