USB Webcam Not Working on Jetson Nano for OpenCV/TensorFlow Object Detection
Hello guys, I am trying to set up a USB webcam on my NVIDIA Jetson Nano to perform real-time video analytics using OpenCV and TensorFlow, I have installed OpenCV and TensorFlow on Ubuntu, connected a USB webcam to the Jetson Nano, wrote a Python script to capture video frames and perform object detection. But I keep getting the error
[ WARN:0] global /tmp/pip-req-build-1234abcd/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
[ WARN:0] global /tmp/pip-req-build-1234abcd/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index
This is my code snippet
import cv2import tensorflow as tf# Load the pre-trained object detection modelmodel = tf.saved_model.load('ssd_mobilenet_v2/saved_model')# Open the USB webcamcap = cv2.VideoCapture(0)while cap.isOpened(): ret, frame = cap.read() if not ret: print("Failed to capture image") break # Preprocess the frame for the model input_tensor = tf.convert_to_tensor(frame) input_tensor = input_tensor[tf.newaxis, ...] # Perform object detection detections = model(input_tensor) # Display the frame cv2.imshow('Frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows()
import cv2import tensorflow as tf# Load the pre-trained object detection modelmodel = tf.saved_model.load('ssd_mobilenet_v2/saved_model')# Open the USB webcamcap = cv2.VideoCapture(0)while cap.isOpened(): ret, frame = cap.read() if not ret: print("Failed to capture image") break # Preprocess the frame for the model input_tensor = tf.convert_to_tensor(frame) input_tensor = input_tensor[tf.newaxis, ...] # Perform object detection detections = model(input_tensor) # Display the frame cv2.imshow('Frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): breakcap.release()cv2.destroyAllWindows()