Ubuntu 16.04上OpenCV中的集成网络摄像头正在抛出以下错误。我用不同的程序检查了奶酪,它显示了静止的图像和视频,因此这里似乎不是相机本身的问题。
我用来测试的代码:
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(2)
if not cam.isOpened():
print('Cannot open camera')
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
错误:
无法打开相机(来自
if not cam.isOpened():
脚本的反馈)。OpenCV错误:imshow中的断言失败(size.width> 0& size.height> 0),文件/io/opencv/modules/highgui/src/window.cpp,第325行
追踪(最近一次通话): 文件“Video_test.py”,第13行,in cv2.imshow('webcam',frame) cv2.error:/io/opencv/modules/highgui/src/window.cpp:325:错误:(-215)size.width> 0&&函数imshow中的size.height> 0
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
使用cam.open()
:
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(2) # camera index (default = 0) (added based on Randyr's comment).
print 'cam has image : %s' % cam.read()[0] # True = got image captured.
# False = no pics for you to shoot at.
# Lets check start/open your cam!
if cam.read() == False:
cam.open()
if not cam.isOpened():
print('Cannot open camera')
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
您还可以使用cam = cv2.VideoCapture(value)
值...现在设置为2。试试范围......例如。 1-10。