[英]opencv error Assertion failed python
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp, line 3737 Traceback (most recent call last): File "test.py", line 11, in gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || OpenCV 错误:cvtColor 中的断言失败(scn == 3 || scn == 4),文件 /home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp,第 3737 行 Traceback(最近一次调用):文件“test.py”,第 11 行,灰色 = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error:/home/pi/opencv-2.4.9/modules/imgproc/src/color.cpp: 3737: 错误: (-215) scn == 3 || scn == 4 in function cvtColor函数 cvtColor 中的 scn == 4
当文件名不存在或不是图像时,这通常会发生在我身上。
This is happening because there is an error in reading image from the video.发生这种情况是因为从视频中读取图像时出错。 you can try the below code and if you are seeing nothing, then the problem is with your webcam.您可以尝试以下代码,如果您什么也没看到,则问题出在您的网络摄像头上。
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
while ret:
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
ret, frame = cap.read()
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
If you are using a docker container to run your code, the issue might be in the way you set up your docker container.如果您使用 docker 容器来运行代码,则问题可能出在您设置 docker 容器的方式上。 In particular, you need to specify a flag --device
to enable a camera usage in the docker container when creating the container like this:特别是,在创建容器时,您需要指定一个标志--device
以在 docker 容器中启用相机使用,如下所示:
docker run --device <device-path> <rest-of-the-paramaters>
Before that, you need to check the path of the camera device by using ls -ltrh /dev/video*
for Ubuntu.在此之前,您需要使用ls -ltrh /dev/video*
for Ubuntu 检查摄像头设备的路径。 Usually, the path is /dev/video0
.通常,路径是/dev/video0
。
I ran your code, it works perfectly fine for me.我运行了你的代码,它对我来说非常好。 But I did get some indentation errors.但我确实遇到了一些缩进错误。
Here's the updated code:这是更新后的代码:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
If one still gets this error, try doing the following:如果仍然出现此错误,请尝试执行以下操作:
if ret:
assert not isinstance(frame,type(None)), 'frame not found'
OR或者
assert (frame is not None) == ret, (ret, type(frame))
OR或者
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.