![](/img/trans.png)
[英]openCV: Issue with cv2.VideoCapture(0) and cv2.VideoCapture(-1)
[英]OpenCV VideoCapture issue
我似乎无法弄清楚为什么我要返回一个值为0而不是实际数字的NumPy数组。 当程序执行时,它返回True,因此我知道python能够读取VideoCapture对象。
我做错什么了吗?
import cv2,time
video = cv2.VideoCapture(0)
check, frame = video.read()
print(check)
print(frame)
time.sleep(3)
video.release()
我也确实收到一条消息[ WARN:0] terminating async callback
但不确定为什么收到此消息。
我在执行程序时的输出如下所示。
True
[[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
...
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]
[[0 0 0]
[0 0 0]
[0 0 0]
...
[0 0 0]
[0 0 0]
[0 0 0]]]
更新的代码(仍然面临问题):
import cv2,time
import numpy as np
video = cv2.VideoCapture(0)
print('video.isOpened() =', video.isOpened())
check, frame = video.read()
np.set_printoptions(threshold='inf')
print(check)
print(frame)
time.sleep(3)
video.release()
我将video = cv2.VideoCapture(0)
中的video = cv2.VideoCapture(0)
替换0
计算机中的本地视频文件,并且您的代码能够读取视频并输出有意义的numpy
数组。 所以我认为您的python
代码很好。 通常, VideoCapture
0
是指计算机上的默认摄像机,可以是笔记本电脑上的内置网络摄像机,也可以是通过USB等外部接口插入的摄像机。 您能检查一下它是否正常运行吗? 你可以加
print 'video.isOpened() =', video.isOpened()
在该行之后,您可以创建视频以查看其是否已成功打开。
通过使用以下命令设置numpy
的打印选项来打印每个像素值也将很有帮助
numpy.set_printoptions(threshold='inf')
这样一来,您便可以检查自己是不是全为零,或者大多数情况下都是零而未显示非零值,如果相机的保护盖未打开,则可能是这种情况。
示例代码可帮助您进行调试:
import cv2,time
import numpy
import sys
numpy.set_printoptions(threshold=sys.maxsize)
video = cv2.VideoCapture("/home/.../opencv-3.1.0/samples/data/768x576.avi")
print 'video.isOpened() =', video.isOpened()
check = True
count = 0
while check and count < 100:
check, frame = video.read()
cv2.imshow('frame', frame)
count += 1
cv2.waitKey(10)
cv2.destroyAllWindows()
cv2.imwrite('frame.png', frame)
print(check)
print(frame)
time.sleep(3)
video.release()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.