簡體   English   中英

無法在 OpenCV 中關閉視頻窗口

[英]Cannot Close Video Window in OpenCV

我想使用 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)

窗口打開並開始播放視頻,但我無法關閉窗口。

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM