【问题标题】:I cant understand why my webcam is capturing only one frame (the first one one when webcam is started)我不明白为什么我的网络摄像头只捕获一帧(网络摄像头启动时的第一帧)
【发布时间】:2020-01-21 17:07:46
【问题描述】:

我编写了一个 python 脚本(使用 OpenCV)来启动,从我的网络摄像头捕获视频,将其转换为灰度并显示以下视频。当我第一次编译它时,它工作得很好,但在第三或第四次编译之后,它被困在只捕获网络摄像头启动时的第一帧。 这是代码:

import numpy 
import cv2 as cv 

cap=cv.VideoCapture(0)

while(True):
    #capture frame by frame
    ret, frame = cap.read() #cv.VideoCapture(0) aslo returns a boolean true value if read correctly    
    #Operations on the frame 
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    #display
    cv.imshow('frame', gray)
    if cv.waitKey(0) &0xFF: #== ord('q'):
        break

#when everything is done, realse the capture 
cap.realse()
cv.destroyAllWindows()      

【问题讨论】:

    标签: python visual-studio opencv visual-studio-code


    【解决方案1】:

    所以,它只显示一帧然后突然退出,还是等待按下按钮?

    如果你按下任何键('q' 除外),它应该得到下一帧。 如果您希望循环继续并显示实时提要,请更改

    if cv.waitKey(0) &0xFF: #== ord('q'):
            break
    

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

    这是因为cv.waitkey(x) 等待 'x' 毫秒以等待按键按下,但如果 x 为 0,它会无限期地等待,直到您按下按键。

    另外,你有一个错字,cap.release()

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多