how can i use usb-cam and pi-cam cuncurrently?

I want to use usb-camera and pi-camera in same time.

but when i ran a multithread that captured image using usb-cam and pi-cam both concurrently. my jetson was stopped and couldn’t handling it anymore.

how can i fixed it?

hello hayoung.is.here,

according to Camera Architecture Stack,
you should note that usb-camera and Raspberry PI camera were based-on different interface.
usb-camera has built-in ISP hence it won’t going through [Camera Core], but Raspberry PI camera is a bayer sensor.

may I know what’s your use-case,
could you please also share the commands for review,
please share the commands you’re used to capture images from those two different cameras.
you may also gather failures message for checking,
thanks

I made multithread for using two type of cameras one is pi-cam the other is USB-webcam in same time.
first, I wrote a code referenced by Jackson Hacks

=========================================================================================
def gstreamer_pipeline(
capture_width=120,
capture_height=320,
display_width=120,
display_height=320,
framerate=10,
flip_method=0,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
“video/x-raw, format=(string)BGR ! appsink”
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)

def show_camera(cnt):
# To flip the image, modify the flip_method parameter (0 and 2 are the most common)
print(gstreamer_pipeline(flip_method=0))
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=2), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow(“CSI Camera”, cv2.WINDOW_AUTOSIZE)
# Window
while cv2.getWindowProperty(“CSI Camera”, 0) >= 0:
ret_val, img = cap.read()
if cnt % 5000 ==0:
print(“CSI-cam”,img)
#cv2.imshow(“CSI Camera”, img)
# This also acts as
keyCode = cv2.waitKey(30) & 0xFF
# Stop the program on the ESC key
if keyCode == 27:
break
cap.release()
cv2.destroyAllWindows()
else:
print(“Unable to open camera”)

if name == “main”:
show_camera()

=======================================================================================

when I ran a code over 7-8 times, it generated error–>
/dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:532 Failed to create CaptureSession

I have another problem for using camera.
“cv2.imshow(“CSI Camera”, img)” is stopped after 7s to start.
it is hardware problem? or my code is weird?

how can i fixed it?

hello hayoung.is.here,

as I mentioned in comment #2, you’ll need to enable v4l2src to access USB-webcam,
suggest you should separate command lines to validate your camera streaming.
for example,

# CSI-camrea
$ gst-launch-1.0 nvarguscamerasrc sensor-id=0 ! 'video/x-raw(memory:NVMM),width=1920, height=1080, framerate=30/1, format=NV12' ! nvoverlaysink -ev

# USB-camrea
$ gst-launch-1.0 -v v4l2src device=/dev/video1 ! video/x-raw,framerate=30/1,width=640,height=480 ! xvimagesink

I checked CIS-camera &USB-camera now.
CIS-camera is ok, but USB-camera get below error

ERROR: from element /GstPipeline:pipeline0/GstV4l2Src:v4l2src0: Internal data stream error.
Additional debug info:
gstbasesrc.c(3055): gst_base_src_loop (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src0:
streaming stopped, reason not-negotiated (-4)

what is that mean?

hello hayoung.is.here,

please have a try to configure environment settings,

$ export DISPLAY=:0
$ gst-launch-1.0 -v v4l2src device=/dev/video0 ! video/x-raw,framerate=30/1,width=640,height=480 ! xvimagesink

you may also have an alternative way to launch usb-cam for capture a jpg as below, thanks

$ v4l2-ctl -d /dev/video0 --set-fmt-video=width=1920,height=1080,pixelformat=MJPG --stream-mmap --stream-count=1 --stream-to=usb.mjpg

Hi, when I paste the USB-camera code above in the command line it works pretty well, but when I add this command on my VideoCapture code the following error appears:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (801) open OpenCV | GStreamer warning: cannot find appsink in manual pipeline

When I change the code from xvimagesink to appsink the following error appears:

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (1757) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Internal data stream error.

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created.

Could you help me?