繁体   English   中英

无法使用OpenCV从摄像机获取视频

[英]Not able to fetch the video from the camera using OpenCV

我无法从便携式摄像机捕获视频

我正在使用OpenCV- Python。 我正在尝试获取摄像机视频,但仅显示单帧。

import cv2

# 1, -1, 0
cap = cv2.VideoCapture(0);

while(True):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    #cv2.imshow('frame', frame)
    cv2.imshow('frame', gray)

    if cv2.waitKey(0): #& OxFF == ord('q'): Error so commented
        break

cap.release()
cv2.destroyAllWindows()

期待相机视频,但仅接收单帧。

如果提供0作为cv2.waitKey的参数,它将等待直到有按键。 如果将0更改为1 (或其他一些小数字),该脚本将起作用。

出于兴趣,即使不更改0 ,您也可以看到脚本'works'。 如果您反复点按任意键,则相框会更新。

0xFF是您尝试使用O (字母)而不是0 (数字),因此python无法识别它。

完整清单:

import cv2

# 1, -1, 0
cap = cv2.VideoCapture(0);

while(True):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

    #cv2.imshow('frame', frame)
    cv2.imshow('frame', gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM