![](/img/trans.png)
[英]How to resize a small OpenCV videocapture window in Python?
[英]videocapture window not closing - OpenCV
我正在嘗試使用我的網絡攝像頭捕捉實時視頻。
我從互聯網上學到的代碼就像一個魅力。
但是在我將 opencv 更新到 4.2.0 后出現了一個問題,無論我嘗試多少次,videoCapture window 都不會關閉。
源代碼
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()
您可以在 while 循環的末尾添加以下內容,以檢測 window 是否已關閉並終止循環:
if cv.getWindowProperty('frame', cv.WND_PROP_VISIBLE) < 1:
break
如果 window frame
不再存在, getWindowProperty
將返回 0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.