Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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.

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, 2020 at 15:56
  • Right-click on source Directory
  • Mark Directory as -> source root
  • File --> Invalidate Caches / Restart... -> Invalidate and Restart
  • In your IDE, go to settings -> preferences -> django
  • 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.

    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.

    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. – Community Oct 24, 2022 at 5:33

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.