Welcome to Jetson Orin NANO Developer Kit repository
17.Preview USB camera image
Previewing the USB Camera Feed
Previewing the USB Camera Feed1. Implementation Principle2. Implementation Effect3. Implementation Code
1. Implementation Principle
Use the cv2.VideoCapture() function to capture video frames from a USB camera or a video file.
2. Implementation Effect
xxxxxxxxxxcd ~/opencv
xxxxxxxxxxpython3 17.camera_preview_usb.py # The program opens video0 by default; to open another camera, modify the device number yourself
Note: click the camera window and press q to exit.

3. Implementation Code
import cv2def preview_usb_camera():cap = cv2.VideoCapture(0)cap.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)if not cap.isOpened():print("Error: Could not open USB camera.")returnwhile True:ret, frame = cap.read()if not ret:breakcv2.imshow('USB Camera Preview', cv2.resize(frame, (640, 480)))if cv2.waitKey(1) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()preview_usb_camera()