Hello,
I am running the camera.py script to simply display the camera output on the screen and everything runs fine, however I need to detect when the camera is disconnected.
Right now, one of two things happen when the camera is disconnected:
1- The camera output and the script freezes (I want to avoid that)
2- ret_val is false and I am able to handle the exception ( I want this to happen always)
The script:
import sys
import cv2
import os
def read_cam():
cap =cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1 ", cv2.CAP_GSTREAMER)
if cap.isOpened():
cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
while True:
ret_val, img = cap.read();
if not ret_val:
print("Camera Disconnected")
break
cv2.imshow('demo',img)
if cv2.waitKey(1) & 0xFF ==ord('s'):
break
else:
print("Camera open failed")
cv2.destroyAllWindows()
if __name__ == '__main__':
read_cam()
I tried using this script to monitor a timeout but it did not work: python - opencv videocapture hangs/freeze when camera disconnected instead of returning "False" - Stack Overflow
Any ideas how I can avoid the screen freeze upon disconnection?