Python 视频捕获窗口未关闭-OpenCV
我正试图用我的网络摄像头捕捉一段实时视频。Python 视频捕获窗口未关闭-OpenCV,python,opencv,Python,Opencv,我正试图用我的网络摄像头捕捉一段实时视频。我从互联网上学到的代码非常有用。但是,在我将opencv更新到4.2.0之后,有一个问题是,无论我尝试了多少次,视频捕捉窗口都不会关闭。 源代码 import numpy as np import cv2 as cv cap = cv.VideoCapture(0) if not cap.isOpened(): print("Cannot open camera") exit() while True: # Capture fra
我从互联网上学到的代码非常有用。
但是,在我将opencv更新到4.2.0之后,有一个问题是,无论我尝试了多少次,视频捕捉窗口都不会关闭。
源代码
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv.flip(frame,1)
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
您可以在while循环结束时添加以下内容,以检测窗口是否已关闭并终止循环:
如果cv.getWindowProperty('frame',cv.WND\u PROP\u可见)<1:
打破
如果窗口
帧不再存在,则getWindowProperty
将返回0。单击显示图像的窗口(不是运行脚本的终端),然后按q
。是的,q可以工作,但关闭选项不起作用。为什么?opencv无法直接理解窗口关闭事件,请始终使用键盘。