我试图运行一个opencv代码,代码有以下几行
import cv2 as cv
image = cv.imread('Photos\cat.jpg')
cv.imshow('Image of Catt', image)
cv.waitKey(0)
在Visual代码中运行代码时,单击屏幕右上部分的run代码按钮后,屏幕底部的output选项卡中会出现错误
[Running] python -u
"c:\Users\jimmy\Documents\Projects\Programing\Python\Learning
Python\Youtube\openCV\read.py"
[ WARN:0@0.131] 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
所以我所做的就是用许多方法重新安装opencv
为此,我使用了许多方法来卸载,例如
pip uninstall opencv
我知道错误了
WARNING: Skipping opencv as it is not installed.
我试过了
pip3 uninstall opencv
我得到了错误
WARNING: Skipping opencv as it is not installed
我试过了
pip uninstall opencv-python
的输出卸载opencv。
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
但是为了防止我卸载了其他python的安装。
pip uninstall opencv-contrib-python
我得到了
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
我还卸载了
pip3 uninstall opencv-python
我得到了
WARNING: Skipping opencv-python as it is not installed.
我试过了
pip3 uninstall opencv-contrib-python
我得到了
WARNING: Skipping opencv-contrib-python as it is not installed.
但是在这一点上,我非常确信我已经卸载了opencv。
我现在安装了opencv
pip install opencv
我得到了
ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv
我再次尝试安装opencv
pip3 install opencv
我又得到了
ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv
我再试一次
pip install opencv-python
我得到了
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
我想确认它在python3上所以我安装了
pip3 install opencv-python
然后我得到了
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)
但是我还没有完全满足,所以我重新安装了
pip install opencv-contrib-python
我得到了
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
我还安装了pip3
pip3 install opencv-contrib-python
我得到了
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)
我听说人们也用其他方法进行安装,所以为了防止出现错误,我尝试了以下方法
python安装-m pip
这给了我
ERROR: Could not find a version that satisfies the requirement opencv (from versions: none)
ERROR: No matching distribution found for opencv
我试过了
python3 -m pip install opencv
我得到了同样的结果
然后我试着
pip install -I opencv-python
我得到了
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需要没有安装的daal==2021.2.3。Numba0.54.1需要numpy<1.21,>=1.17,但是numpy 1.22.3不兼容。成功安装numpy-1.22.3 opencv-python-4.5.5.64
这时,我终于满意了。
因此,我在右上角使用代码中的start按钮运行我的代码
import cv2 as cv
image = cv.imread('Photos\cat.jpg')
cv.imshow('Image of Catt', image)
cv.waitKey(0)
在它编写的输出部分中
[Running] python -u "c:\Users\jimmy\Documents\Projects\Programing\Python\Learning Python\Youtube\openCV\read.py"
[ WARN:0@0.605] 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)
D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182:错误:(-215:断言失败)!函数'cv::cvtColor‘中的_src.empty()
[Done] exited with code=1 in 1.233 seconds
我使用了所有的方法,但它仍然不起作用,任何帮助都是值得感谢的!顺便说一句,我在窗户上,如果你看这么多书太累了,我只是担心我没有澄清什么,或者没有尝试一个明显的答案。
发布于 2022-04-01 02:10:45
为python安装opencv的命令是:python3 -m pip install opencv-python
。此命令假定您正在为python3安装opencv。您还可以只运行python -m pip install opencv-python
或pip install opencv-python
。访问https://pypi.org/project/opencv-python/,了解更多关于opencv、可用的不同版本以及如何安装它们的信息。
发布于 2022-04-01 08:19:13
发布于 2022-05-31 12:10:00
我也偶然发现了这个问题。这个问题似乎与特定的opencv版本4.5.5.64有关。运行以下命令:
pip install -U opencv-python==4.5.5.62
你应该能运行你的代码。至少在我的案子里它解决了。在许多开放的github问题中,您可以在多个opencv发行版中发现许多类似的行为,例如:Opencv Github问题
我用来测试这个问题的代码如下:
import cv2
video_capture = cv2.VideoCapture(0)
fps = video_capture.get(cv2.CAP_PROP_FPS)
print(f"Frames per second using video.get(cv2.CAP_PROP_FPS) : {fps}")
while True:
ret, frame = video_capture.read()
if ret:
cv2.imshow('temp', frame)
if cv2.waitKey(1) == ord('q'):
break
https://stackoverflow.com/questions/71700197
复制相似问题