我才刚刚开始学习python。这是我的第一个代码。我尝试使用网络摄像头。由于某种原因,它给了我一个错误。 代码
import cv2, time
video = cv2.VideoCapture(0)
check, frame = video.read()
print(check)
print(frame)
cv2.imshow("vegans have big gay", frame)
cv2.waitKey(0)
video.release()
错误:
False
[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875772
None
[ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772
[ WARN:1] videoio(MSMF): can't grab frame. Error: -1072875772
Traceback (most recent call last):
File "C:/Users/stasiek/PycharmProjects/pierwszy/capture.py", line 9, in <module>
cv2.imshow("vegans have big gay", frame)
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
答案 0 :(得分:2)
仅用于故障排除,还值得检查当前是否有任何其他程序打开了相机。我注意到在 can't grab frame. Error: -1072875772
之后我也无法通过 VLC 访问相机(即使在 video.release()
之后)。在访问它的其他程序 (ContaCam) 中选择“删除相机”使其可用于 OpenCV。
答案 1 :(得分:0)
尝试这种方法,调整视频大小并将视频捕获包装成一个循环(按下按键时会中断):
import cv2
video = cv2.VideoCapture(0)
video.set(cv2.CAP_PROP_FRAME_WIDTH,640);
video.set(cv2.CAP_PROP_FRAME_HEIGHT,480);
while True:
check, frame = video.read()
cv2.imshow('Video window', frame)
# press 'q' on keyboard to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
答案 2 :(得分:0)
尝试一下
video = cv2.VideoCapture(0)
代替
{{1}}