Detect camera disconnection on Jetson Nano 2 GB

I am running the 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 =;
            if not ret_val:
                print("Camera Disconnected")
            if cv2.waitKey(1) & 0xFF ==ord('s'):
     print("Camera open failed")


if __name__ == '__main__':

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?

I shall move your post to the Jetson Nano are - the community there is likely to get back to you quickly.

Sorry to tell Nano unable to support it. Suppose XNX able to support it.

Thank you @ShaneCCC,
So, as of now, there is no way to reliably detect camera disconnection on the Jetson Nano 2gb dev kit?

Also, if I am designing my own carrier board for the jetson nano, do you have any idea how can I add this feature?

What I can think is have GPIO to detect if the sensor is present or not and kill the streaming process.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.