Error (-215) size.width>0 && size.height>0 occurred when attempting to display an image using OpenCV
我有类似的问题与此讨论,在我读了它仍然没有线索,为如何解决我的,我张贴在这里我的情况
- 我使用raspberrypi 4 ; 32位,Linux运行Python ;罗技USB摄像头透镜;在虚拟环境中运行 *
- Python脚本
opencv_camera.py
import cv2
# define a video capture object
vid = cv2.VideoCapture(1) ########## 1- device id =1###if 1 deosn't work try 2
while(True):
ret, frame = vid.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
发生错误:
函数“imshow”中出现错误:OpenCV(4.6.0)....
- 输出:
(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:0@0.020] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video1): can't open camera by index
Traceback (most recent call last):
File "/home/joy/Desktop/opencv_camera.py", line 14, in <module>
cv2.imshow('frame', frame)
cv2.error: OpenCV(4.6.0) /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
- 故障排除状态:现在发现Ret没有
import cv2
# define a video capture object
vid = cv2.VideoCapture(2) ########## 1- device id =1###if 1 deosn't work try 2
while(True):
ret, frame = vid.read()
if ret == True:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
print("ret is empty")
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
输出:
(venv) joy@raspberrypi:~/Desktop $ python opencv_camera.py
[ WARN:0@0.019] global /tmp/pip-wheel-u79916uk/opencv-python_ea2489746b3a43bfb3f2b5331b7ab47a/opencv/modules/videoio/src/cap_v4l.cpp (902) open VIDEOIO(V4L2:/dev/video2): can't open camera by index
ret is empty
2条答案
按热度按时间qncylg1j1#
如果要拍摄图像,则不应使用
因为这意味着你将不断地阅读来自相机的内容。
参考下面的代码,如果你只想拍一张照片,你可能会不断运行
重拍一张新照片
代码:
vkc1a9a22#
在我将
vid = cv2.VideoCapture()
更改为0
之后,现在视频功能正在工作,但我最初的目的是拍摄一张图像