Jetson-utils for loopback / virtual camera

I would like to explore getting the result as below gst-launch command using jetson-utils and python.

gst-launch-1.0 -v nvarguscamerasrc ! ‘video/x-raw(memory:NVMM), format=NV12, width=1920, height=1080, framerate=30/1’ ! nvvidconv ! ‘video/x-raw, width=640, height=480, format=I420, framerate=30/1’ ! videoconvert ! identity drop-allocation=1 ! ‘video/x-raw, width=640, height=480, format=RGB, framerate=30/1’ ! v4l2sink device=/dev/video2

Example python scripts have good examples of capturing image and processing it. But doesn’t explain how to pipe it to a virtual camera using V4l2sink or other methods. Any pointers?

This is just a standard gstreamer pipeline. You may read some basics of gstreamer in python.

You would just prepare the pipeline string without the quotes used with gst-launch, and use Gst.parse_launch():

import sys
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

gst_str = "nvarguscamerasrc ! video/x-raw(memory:NVMM), format=NV12, width=1920, height=1080, framerate=30/1 ! nvvidconv ! video/x-raw, width=640, height=480, format=I420, framerate=30/1 ! videoconvert ! identity drop-allocation=1 ! video/x-raw, width=640, height=480, format=RGB, framerate=30/1 ! v4l2sink device=/dev/video2 "

pipeline = Gst.parse_launch(gst_str)


Then you can access your virtual camera with V4L API like a USB cam:

./ --camera=/dev/video2

the code below will create a persistent virtual device at /dev/video*
gstreamer pipeline from posts 1&2 will direct CSI Bayer output to the virtual device’s RGB
there will be Bayer to non Bayer conversion.

sudo su
cd /usr/src/linux-headers-4.9.140-tegra-ubuntu18.04_aarch64/kernel-4.9
mkdir v4l2loopback
git clone v4l2loopback
cd v4l2loopback && git checkout -b v0.10.0
make install
apt-get install -y v4l2loopback-dkms v4l2loopback-utils
modprobe v4l2loopback devices=1 video_nr=2 exclusive_caps=1
echo options v4l2loopback devices=1 video_nr=2 exclusive_caps=1 > /etc/modprobe.d/v4l2loopback.conf
echo v4l2loopback > /etc/modules
update-initramfs -u