Am trying to capture a live video using my webcam.
And the code i learnt from the internet works like a charm.
But there's an issue after i updated my opencv to 4.2.0 that the videoCapture window is not at all closing no matter how many times i try.
Source Code
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()
You can add the following at the end of the while loop to detect if the window was closed and terminate the loop:
if cv.getWindowProperty('frame', cv.WND_PROP_VISIBLE) < 1:
break
The getWindowProperty
will return 0 if the window frame
no longer exists.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.