【问题标题】:Cannot Close Video Window in OpenCV无法在 OpenCV 中关闭视频窗口
【发布时间】:2019-02-24 15:57:40
【问题描述】:

我想使用 python 在 openCV 中播放视频并随时关闭该窗口,但它不起作用。

import numpy as np
import cv2

fileName='test.mp4'  # change the file name if needed

cap = cv2.VideoCapture(fileName)   # load the video

while(cap.isOpened()):
    # play the video by reading frame by frame
    ret, frame = cap.read()
    if ret==True:
        # optional: do some image processing here 

        cv2.imshow('frame',frame)              # show the video
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)

窗口打开并开始播放视频,但我无法关闭窗口。

【问题讨论】:

  • 您能否详细说明您的代码“不起作用”的原因?你期待什么,实际发生了什么?如果您遇到异常/错误,请发布它发生的行和异常/错误详细信息。请edit这些详细信息,否则我们可能无法提供帮助。
  • @PatrickArtner 我进行了编辑。谢谢
  • “我无法关闭窗口” .. 在什么意义上?对按键没有反应? (如果是这样,当您击键时窗口是否聚焦?)
  • @DanMašek 每次我单击窗口顶部的红十字关闭窗口时,它都会消失,但随后又会弹出。

标签: python python-3.x opencv


【解决方案1】:

您可以使用cv2.getWindowProperty('window-name', index) 来检测窗口是否关闭。我不完全确定索引,但这对我有用:

import cv2

filename = 'test.mp4'
cam = cv2.VideoCapture(filename)

while True:
    ret, frame = cam.read()
    if not ret:
        break
    cv2.imshow('asd', frame)
    cv2.waitKey(1)
    if cv2.getWindowProperty('asd', 4) < 1:
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    相关资源
    最近更新 更多