Home > Software design >  Unable to run OpenCV Program in Visual Studio Code
Unable to run OpenCV Program in Visual Studio Code

Time:04-02

I was trying to run a opencv code with the following lines

import cv2 as cv

image = cv.imread('Photos\cat.jpg')

cv.imshow('Image of Catt', image)

cv.waitKey(0)

and when I run the code in Visual Studio Code I get the error in the output tab of the bottom of the screen after clicking on the run code button on the top right part of the screen

[Running] python -u 
"c:\Users\jimmy\Documents\Projects\Programing\Python\Learning 
Python\Youtube\openCV\read.py"
[ WARN:[email protected]] global D:\a\opencv-python\opencv-                
python\opencv\modules\imgcodecs\src\loadsave.cpp (239) 
cv::findDecoder imread_('Photos\cat.jpg'): can't open/read file: 
check file path/integrity
Traceback (most recent call last):
  File "c:\Users\jimmy\Documents\Projects\Programing\Python\Learning 
Python\Youtube\openCV\read.py", line 5, in <module>
    cv.imshow('Image of Catt', image)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv- 
python\opencv\modules\imgproc\src\color.cpp:182: error: 
(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'


[Done] exited with code=1 in 0.243 seconds

So What I did was reinstall opencv with many methods

I did that by uninstalling using many methods such as

pip uninstall opencv

I get the error

WARNING: Skipping opencv as it is not installed.

I tried

pip3 uninstall opencv

Which I get the error

WARNING: Skipping opencv as it is not installed

I tried

pip uninstall opencv-python

which I succeeded uninstalling opencv with the output of

Found existing installation: opencv-python 4.5.5.64
Uninstalling opencv-python-4.5.5.64:
  Would remove:
    c:\programdata\anaconda3\lib\site-packages\cv2\*
c:\programdata\anaconda3\lib\site-packages\opencv_python- 
4.5.5.64.dist-info\*
Proceed (Y/n)? Y
  Successfully uninstalled opencv-python-4.5.5.64

But just in case I have other installations of python I uninstalled with

pip uninstall opencv-contrib-python

which I got

Found existing installation: opencv-contrib-python 4.5.5.64
Uninstalling opencv-contrib-python-4.5.5.64:
  Would remove:
    c:\programdata\anaconda3\lib\site-packages\opencv_contrib_python-4.5.5.64.dist-info\*
Proceed (Y/n)? Y
  Successfully uninstalled opencv-contrib-python-4.5.5.64

I also uninstalled with

pip3 uninstall opencv-python

which I got

WARNING: Skipping opencv-python as it is not installed.

I tried

pip3 uninstall opencv-contrib-python

which I got

WARNING: Skipping opencv-contrib-python as it is not installed.

but at this point I was very sure that I have uninstalled opencv

I now installed opencv with

pip install opencv

Which I got

ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv

I tried again installing opencv with

pip3 install opencv

Which again I got

ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv

I tried again with

pip install opencv-python

Which I got

  Using cached opencv_python-4.5.5.64-cp36-abi3-win_amd64.whl (35.4 MB)
Requirement already satisfied: numpy>=1.14.5 in c:\programdata\anaconda3\lib\site-packages (from opencv-python) (1.22.3)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.5.64

I wanna make sure it was on python3 so I installed with

pip3 install opencv-python

Which I then got

Requirement already satisfied: opencv-python in c:\programdata\anaconda3\lib\site-packages (4.5.5.64)
Requirement already satisfied: numpy>=1.14.5 in c:\programdata\anaconda3\lib\site-packages (from opencv-python) (1.22.3)

But I wasn't completely satisfied yet so I installed again with

pip install opencv-contrib-python

Which I got

    Collecting opencv-contrib-python
  Using cached opencv_contrib_python-4.5.5.64-cp36-abi3-win_amd64.whl (42.2 MB)
Requirement already satisfied: numpy>=1.17.3 in c:\programdata\anaconda3\lib\site-packages (from opencv-contrib-python) (1.22.3)
Installing collected packages: opencv-contrib-python
Successfully installed opencv-contrib-python-4.5.5.64

I also installed with pip3

pip3 install opencv-contrib-python

Which I got

Requirement already satisfied: opencv-contrib-python in c:\programdata\anaconda3\lib\site-packages (4.5.5.64)
Requirement already satisfied: numpy>=1.14.5 in c:\programdata\anaconda3\lib\site-packages (from opencv-contrib-python) (1.22.3)

I heard people also installing with other methods so just in case of some error I tried these

python -m pip install opencv

Which gave me

ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv

I tried

python3 -m pip install opencv

Which I got the exaact same results

I then tried

pip install -I opencv-python

Which I got

Collecting opencv-python
  Using cached opencv_python-4.5.5.64-cp36-abi3-win_amd64.whl (35.4 MB)
Collecting numpy>=1.14.5
  Using cached numpy-1.22.3-cp39-cp39-win_amd64.whl (14.7 MB)
Installing collected packages: numpy, opencv-python
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.

daal4py 2021.3.0 requires daal==2021.2.3, which is not installed. numba 0.54.1 requires numpy<1.21,>=1.17, but you have numpy 1.22.3 which is incompatible. Successfully installed numpy-1.22.3 opencv-python-4.5.5.64

At this point I was finally satisfied

So I ran my code with the start button in Visual Studio Code on the top right corner

import cv2 as cv

image = cv.imread('Photos\cat.jpg')

cv.imshow('Image of Catt', image)

cv.waitKey(0)

In the output section it wrote

[Running] python -u "c:\Users\jimmy\Documents\Projects\Programing\Python\Learning Python\Youtube\openCV\read.py"
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('Photos\cat.jpg'): can't open/read file: check file path/integrity
Traceback (most recent call last):
  File "c:\Users\jimmy\Documents\Projects\Programing\Python\Learning Python\Youtube\openCV\read.py", line 5, in <module>
    cv.imshow('Image of Catt', image)

cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

[Done] exited with code=1 in 1.233 seconds

I used all the methods but it still doesn't work, any help is appreciated! btw I am on windows and if you are too tired reading this much I am just afraid I didn't clarify on something or didn't try out a obvious answer.

CodePudding user response:

The command for installing opencv for python is: python3 -m pip install opencv-python. This command assumes you are installing opencv for python3. You may also run just python -m pip install opencv-python or pip install opencv-python. Visit https://pypi.org/project/opencv-python/ to learn more about opencv-python, the different versions available and how to install them.

CodePudding user response:

Hope it will work !

pip install -I opencv-python
pip install --ignore-installed opencv-python

Referance

  • Related