2

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:

"cannot find reference 'VideoCapture' in init.py" on Pycharm IDE

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.

bad_coder
  • 8,684
  • 19
  • 37
  • 59
FiloPietra
  • 21
  • 1
  • 3
  • 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

2 Answers2

4

Instead of import cv2 use from cv2 import cv2

Ushan Bro
  • 41
  • 1
2

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
  • 8,684
  • 19
  • 37
  • 59
MDT
  • 21
  • 2