繁体   English   中英

videocapture window 未关闭 - OpenCV

[英]videocapture window not closing - OpenCV

我正在尝试使用我的网络摄像头捕捉实时视频。
我从互联网上学到的代码就像一个魅力。
但是在我将 opencv 更新到 4.2.0 后出现了一个问题,无论我尝试多少次,videoCapture window 都不会关闭。

源代码

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 循环的末尾添加以下内容,以检测 window 是否已关闭并终止循环:

    if cv.getWindowProperty('frame', cv.WND_PROP_VISIBLE) < 1:
        break

如果 window frame不再存在, getWindowProperty将返回 0。

暂无
暂无

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

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