简体   繁体   English

videocapture window 未关闭 - OpenCV

[英]videocapture window not closing - OpenCV

Am trying to capture a live video using my webcam.我正在尝试使用我的网络摄像头捕捉实时视频。
And the code i learnt from the internet works like a charm.我从互联网上学到的代码就像一个魅力。
But there's an issue after i updated my opencv to 4.2.0 that the videoCapture window is not at all closing no matter how many times i try.但是在我将 opencv 更新到 4.2.0 后出现了一个问题,无论我尝试多少次,videoCapture window 都不会关闭。

Source Code源代码

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()

You can add the following at the end of the while loop to detect if the window was closed and terminate the loop:您可以在 while 循环的末尾添加以下内容,以检测 window 是否已关闭并终止循环:

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

The getWindowProperty will return 0 if the window frame no longer exists.如果 window frame不再存在, getWindowProperty将返回 0。

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

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