USB Web Camera

It is possible to have a USB Video Class (UVC) compliant web camera plugged into the device at the same time as the cameras connected on the CSI bus. The Gstreamer commands for the USB camera depend on whether it was plugged in during the boot or plugged in after the boot has completed. This relates to how the /dev/media devices and the /dev/video devices get enumerated.

If plugged in before the boot:

gst-launch-1.0 v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! vaapipostproc ! vaapisink

If plugged in after the boot has completed:

gst-launch-1.0 v4l2src device=/dev/video45 ! video/x-raw,width=640,height=480,framerate=30/1 ! vaapipostproc ! vaapisink

Command for video recording from both CSI cameras and the USB web camera at the same time:

gst-launch-1.0 -e v4l2src device=/dev/video45 ! video/x-raw,width=640,height=480,framerate=30/1 ! vaapipostproc ! vaapih264enc !  h264parse ! mp4mux ! queue ! filesink location=test_usb.mp4 icamerasrc device-name=1 io-mode=3 printfps=true ! video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! vaapih264enc tune=low-power dmabuf-alloc-tiled=true !  h264parse ! mp4mux ! queue ! filesink location=test_1.mp4 icamerasrc device-name=0 io-mode=3 printfps=true ! video/x-raw,format=NV12,width=1280,height=720,framerate=30/1 ! vaapih264enc tune=low-power dmabuf-alloc-tiled=true !  h264parse ! mp4mux ! queue ! filesink location=test_0.mp4

The USB 3.0 connector allows webcams with higher resolutions to be attached, typically with associated frame-rates dependent on the camera module’s specification.

For example, a viewfinder can be used at HD and UHD resolutions:

1280x720 (720p) raw or mjpeg:

gst-launch-1.0 -v v4l2src device=/dev/video45 ! video/x-raw,width=1280,height=720 ! vaapipostproc ! vaapisink

gst-launch-1.0 -v v4l2src device=/dev/video45 ! image/jpeg,width=1280,height=720 ! vaapijpegdec ! vaapisink

1920x1080 (1080p) raw or mjpeg:

gst-launch-1.0 -v v4l2src device=/dev/video45 ! video/x-raw,width=1920,height=1080 ! vaapipostproc ! vaapisink

gst-launch-1.0 -v v4l2src device=/dev/video45 ! image/jpeg,width=1920,height=1080 ! vaapijpegdec ! vaapisink

3840x2160 (4k UHD) raw or mjpeg:

gst-launch-1.0 -v v4l2src device=/dev/video45 ! video/x-raw,width=3840,height=2160 ! vaapipostproc ! vaapisink

gst-launch-1.0 -v v4l2src device=/dev/video45 ! image/jpeg,width=3840,height=2160 ! vaapijpegdec ! vaapisink

Similarly, 4k UHD video recording can be done like this:

gst-launch-1.0 -e v4l2src device=/dev/video45 ! image/jpeg,width=3840,height=2160 ! vaapijpegdec ! vaapih264enc tune=low-power ! h264parse ! mp4mux ! queue ! filesink location=test.mp4

To do the same 4k UHD video recording while monitoring the actual frame-rate during the recording:

gst-launch-1.0 -ve v4l2src device=/dev/video45 ! image/jpeg,width=3840,height=2160 ! vaapijpegdec ! vaapih264enc tune=low-power ! h264parse ! mp4mux ! queue ! fpsdisplaysink text-overlay=false video-sink="filesink location=test.mp4" fps-update-interval=1000

Use WebCam from Python or OpenCV

It is expected that you have python pip installed and you have downloaded the OpenCV python package. See PIP package installer for details.

Connect the WebCam and boot device. After you have a command prompt, issue:

$ export DISPLAY=:0

then

$ nano cam_test.py

and copy the following code a file named cam_test.py:

import cv2
x = 1200.0
y = 800.0
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, x)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, y)
cv2.namedWindow('Feed')
cv2.namedWindow('Captured')
cv2.moveWindow('Captured', 10, 10)
while(True):
  ret, frame = cap.read()
  if ret == False:
    print("Failed to get frame")
  else:
    img = cv2.flip(frame, 1) # comment out and replace with: img = frame
    cv2.imshow('Feed', img)
    key = cv2.waitKey(10)
    if key & 0XFF == ord('w'):
      fname = "Capture.jpg"
      cv2.imwrite(fname, cv2.flip(frame,1))
      imgFile = cv2.imread(fname)
      cv2.imshow('Captured', imgFile)
    if (key & 0XFF == ord('q') or key & 0xFF == 0x1b): # ESC
      cap.release()
      cv2.destroyAllWindows()
      exit()

then issue command:

$ python3 cam_test.py

You can change the resolutions by changing x and y

For more complete information about compiler optimizations, see our Optimization Notice.