簡體   English   中英

如何關閉 cv2 窗口?

[英]How can I close a cv2 window?

我構建了一個調用 IP 攝像機的 python 模塊,但是當我嘗試關閉由 cv2 生成的窗口時,該窗口會在無限循環中一次又一次地生成。 你能幫我嗎?

這是腳本

import cv2

source = "rtsp://admin:admin2016@192.168.1.2//Streaming/Channels/2"
cap = cv2.VideoCapture(source)

ok_flag = True

while ok_flag:
    (ok_flag, img) = cap.read()

    if not ok_flag:
        break

    cv2.imshow("CallingCamera View", img)
    if cv2.waitKey(1) == 27:
        ok_flag = False
        break  
cv2.destroyAllWindows()

我無法完全復制你的錯誤(我最終用你的代碼使 Python 崩潰),但我確實想出了一個修復方法。

while ok_flag:
    (ok_flag, img) = cap.read()
    cv2.imshow("CallingCamera View", img)
    if cv2.waitKey(0) == 27:
        ok_flag = False

cv2.destroyAllWindows()

我不確定您是否有理由將 waitKey 時間設置為 1 毫秒,出於測試目的,將其設置為 0(永遠)同樣有效。 我想將它設置為 1 毫秒會給你最新的圖像? 我的測試是使用桌面上的靜態圖像運行的。 否則刪除兩個中斷和 if not ok_flag 語句似乎解決了所有問題。 您實際上並不需要這些,因為一旦 ok_flag 變為 False 循環就會終止。

cv2.destroyWindow("shown_img")

該參數與用於cv2.imshow("shown_img", img)的相同

暫無
暫無

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

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