4

I am new to python and installed opencv by typing: pip install opencv-contrib-python in the command prompt.

Below I have shown a very simple example of reading in an image and showing it on the screen:

enter image description here

However, I get this error:

enter image description here

When I hover over the imread method in PyCharm, it says Cannot find reference 'imread' in '__init__.py'

It was showing import and runtime errors when I typed import cv2 and import numpy in the command prompt.

I tried going back to the previous version of NumPy by typing pip install --force-reinstall numpy=1.19.3. So, now when I type import cv2 and import numpy in the command prompt, it shows no error but it says it still can't find a reference to it in PyCharm.

I am using Python 3.9.1 with pip 20.2.3.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Killjoy
  • 51
  • 1
  • 6
  • @Hihikomori How do I do that? The instructor here: https://youtu.be/oXlwWbU8l2o?t=137 didn't say anything about that. – Killjoy Jan 05 '21 at 16:47
  • @Hihikomori there are multiple versions available, which version should I install? The latest one? – Killjoy Jan 05 '21 at 17:43
  • 2
    @Hihikomori False - The `opencv-contrib-python` PyPI package installs both the compiled version of the library as well as the wrapper. All you need to do is run that command and you will have OpenCV available for use without any strings attached. The OP is not required to download anything from the official OpenCV website because the project automatically checks out the latest version on Github, builds it, and also builds the Python wrappers. The project is directly available on Github where the packages get constantly released on PyPI: https://github.com/skvark/opencv-python – rayryeng Jan 06 '21 at 00:00
  • Did you try to run your code from cli? – Hihikomori Jan 06 '21 at 00:05
  • 1
    Does this answer your question? [Python 3 Opencv set up problem: "cannot find reference 'VideoCapture' in \_\_init\_\_.py" on Pycharm IDE](https://stackoverflow.com/questions/60229392/python-3-opencv-set-up-problem-cannot-find-reference-videocapture-in-init) – bfontaine Oct 13 '22 at 16:45
  • Be aware that there is bug in the current``opencv` package - see https://stackoverflow.com/a/76110480/11473934 – Kraego Apr 26 '23 at 12:00

4 Answers4

1

If you are using python interpreter with anaconda change it to default python one. if you have not installed python separately then install it and assign the path to it.

somewhat this will be the path

c://user/APpData/Local/Programs/Python/python39/python.exe

Peter sunny
  • 167
  • 1
  • 3
0

The NumPy multicore import error is a result of installing NumPy on Windows that is incompatible with the version of OpenCV that you have. Judging from your screenshots as well as the error, you are running Windows. Try installing Christoph Gohlke's NumPy libraries instead that were built with multicore support enabled.

First do:

pip install pipwin

Then:

pipwin install numpy

This should hopefully settle the OpenCV dependency problem you have.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • It says: Could not install packages due to an EnvironmentError: [WinError 5] Access is denied: 'C:\\Users\\Max\\AppData\\Local\\Programs\\Python\\Python39\\Lib\\site-packages\\~umpy\\.libs\\libopenblas.QVLO2T66WEPI7JZ63PS3HMOHFEY472BC.gfortran-win_amd64.dll' – Killjoy Jan 06 '21 at 04:51
  • You need to run your command prompt as an administrator. Right click on the Command Prompt icon then click on Run as Administrator. Try it again once you have it in that mode. – rayryeng Jan 06 '21 at 05:13
  • It got installed but the error has not been resolved. Pycharm still says cannot find reference. – Killjoy Jan 06 '21 at 07:28
  • Your command prompt env is not the same as PyCharm's. – rayryeng Jan 06 '21 at 07:49
  • How do I fix that? – Killjoy Jan 06 '21 at 10:12
-1

To solve it, you should upgrade numpy installation. Try:

pip install -U numpy

For information here

  • The latest version of numpy was already giving me runtime and import error which is why I force reinstalled the previous version. – Killjoy Jan 05 '21 at 17:37
-1

Instead of doing:

import cv2

try doing:

from cv2 import cv2

This worked for me after a lot of troubleshooting also using pycharm. I now have access to all of the regular cv2 methods.

knowledge_seeker
  • 811
  • 1
  • 8
  • 18