【问题标题】:videocapture window not closing - OpenCV视频捕获窗口未关闭 - OpenCV
【发布时间】:2020-04-19 14:28:57
【问题描述】:

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

源代码

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

【问题讨论】:

  • 单击显示图像的窗口(不是运行脚本的终端),然后按q
  • 是的 q 有效,但关闭选项无效。为什么?
  • 不知何故opencv无法直接理解窗口关闭事件,总是使用键盘。

标签: python opencv


【解决方案1】:

您可以在while循环的末尾添加以下内容,以检测窗口是否关闭并终止循环:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多