I've been working with a Jetson running Ubuntu. There's two cameras attached to it. A CSI camera that uses gstreamer and a USB uv (uv light) camera.
I can detect and run the CSI camera just fine. But whenever I try to connect to the USB camera, it either throws an error or tries to connect to the CSI camera.
This is my most recent version of the testing code for trying to get the USB camera to work:
import cv2
# Create the capture objects
usb_cap = cv2.VideoCapture(1)
# If they aren't opened correctly
if not usb_cap.isOpened():
print("Cannot open usb camera")
exit()
# Set usb477 height and width
usb_cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
usb_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# Window
while True:
# Capture frame-by-frame
usb_read, usb_frame = usb_cap.read()
# if frame isn't read correctly
if not usb_read:
print("Can't receive frame from usb (stream end?).\nExiting.")
break
# Display the resulting frame
cv2.imshow("USB frame", usb_frame)
# Escape the loop by pressing 'q'
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
usb_cap.release()
cv2.destroyAllWindows()
Running it throws this series of errors:
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (1761) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (515) startPipeline OpenCV | GStreamer warning: unable to start pipeline
Can't receive frame from usb (stream end?).
Exiting.
[ WARN:0] global /usr/local/src/opencv-4.4.0/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
I'd really appreciate it if someone could at least try to point me in the right direction for fixing this.
CodePudding user response:
You would have to specify V4L capture backend for using V4L controls. Seems here that without specifying it, gstreamer backend is used, and it instanciates a pipeline with v4l2src, but you cannot change the resolution after creation.
So the simplest solution is just using V4L2 capture backend:
usb_cap = cv2.VideoCapture(1, cv2.CAP_V4L2)
or build a gstreamer pipeline where you can specify the sizes (better set a framerate as well):
pipeline='v4l2src device=/dev/video1 ! video/x-raw,width=1280,height=720 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
usb_cap = cv2.VideoCapture(pipeline, cv2.CAP_GSTREAMER)
