Reputation: 1738
So I'm running this piece of code.
import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("Resources/test_video.mp4")
while True:
success, img = cap.read()
if img is None:
break
img = cv2.resize(img, (frameWidth, frameHeight))
cv2.imshow("Result", img)
keyPressed = cv2.waitKey(5)
if keyPressed == ord('q'):
break;
test_video.mp4 is a short video here The moment it finishes running, the "Result" window freezes and become not responding. Even when I press "Q", nothing happens.
I run the program on Anaconda Spyder. cv2
is installed using pip install opencv-python
Edit: the code has been fixed so that the window exit when "q" is pressed
Upvotes: 0
Views: 1665
Reputation: 456
Try adding these two lines at the end:
import cv2
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("Resources/test_video.mp4")
while True:
success, img = cap.read()
if img is None:
break
#img = cv2.resize(img, (frameWidth, frameHeight))
cv2.imshow("Result", img)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It could be that it's failing to release the resource at the end of the script. See this post for further reference: What's the meaning of cv2.videoCapture.release()?
It also seems to be a common issue. See here and here.
Edit: Update to respond to comment requesting video exit on 'q'. Replace the lines:
if cv2.waitKey(1) and 0xFF == ord('q'):
break
With:
key = cv2.waitKey(1)
if key == ord('q'):
break
Tested and behaviour is as expected using:
Upvotes: 3