Scailable.
Search
K

GenICam USB and GigE

Use GenICam through a V4L2 loopback device to connect industrial cameras
This tutorial provides a step-by-step guide on how to compile and install a V4L2 loopback device, as well as how to compile and install the Aravis GStreamer plugin on Ubuntu 20.04. It then explains how to utilize the Aravis plugin to make a wide range of standard GenICam USB3 and gigabit ethernet machine vision cameras available to the Scailable Edge AI Manager through the V4L2 loopback device.
To begin, you'll need to install specific packages that are necessary for compiling Aravis and the V4L2 loopback device.
sudo apt update
sudo apt install libxml2-dev libglib2.0-dev cmake libusb-1.0-0-dev \
gobject-introspection libgtk-3-dev gtk-doc-tools xsltproc \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libgirepository1.0-dev gettext unzip python3-pip \
ninja-build git
# if you have no (recent) version of meson installed
python3 -m pip install meson
sudo ln -s /usr/local/bin/meson /usr/bin/meson
Next, install the V4L2 loopback device, which will allow you to create a "virtual video devices" that will allow any Linux application to access the industrial camera as a default Linux V4L2 video device:
sudo apt-get install linux-headers-`uname -r`
git clone https://github.com/umlaeute/v4l2loopback.git
cd v4l2loopback
make && sudo make install
sudo depmod -a
Then, compile and install Aravis:
sudo apt install unzip autoremove
wget https://github.com/AravisProject/aravis/archive/refs/heads/main.zip
unzip main.zip
cd aravis-main/
meson setup build
cd build
ninja
ninja install
sudo ldconfig
If all went well, you can preview your machine vision camera(s) using the following command:
arv-viewer-0.8
To use Gstreamer with Scailable AI Manager, start by using Aravis to stream the camera input through Gstreamer (adjusting the format, width, height, and framerate parameters according to your camera's specifications):
gst-launch-1.0 --gst-plugin-path=/usr/local/lib/x86_64-linux-gnu/ aravissrc ! \
video/x-raw,format=GRAY8,width=2048,height=1536,framerate=20/1 ! \
videoconvert ! queue ! ximagesink
To make the image available to other applications as a V4L2 standard video device instead of displaying it in an X window, you need to generate a virtual V4L2 device:
sudo modprobe v4l2loopback card_label="vid0cam"
By creating this virtual loopback device, we enable the transmission of output from a machine vision camera to the Aravis Gstreamer plugin. Gstreamer then facilitates the availability of the camera output through this virtual loopback device:
gst-launch-1.0 --gst-plugin-path=/usr/local/lib/x86_64-linux-gnu/ \
aravissrc num-arv-buffers=5 ! \
video/x-raw,format=GRAY8,width=2048,height=1536,framerate=15/1 ! \
videoconvert ! video/x-raw,format=YUY2 ! queue ! v4l2sink device=/dev/video0
Your video pipeline should now start running:
At this stage, you can access the camera via /dev/video0 using any Linux application. Specifically, for our purposes, you can access it through the Scailable Edge AI Manager UI at http://localhost:8081:
To put the camera to good use, select, for instance, the people detection model and run it:
It is recommended to automate the creation of the loopback device and the execution of the Gstreamer pipeline during system boot. This can be achieved by implementing a startup script that executes the following two commands:
sudo modprobe v4l2loopback card_label="vid0cam"
gst-launch-1.0 --gst-plugin-path=/usr/local/lib/x86_64-linux-gnu/ \
aravissrc num-arv-buffers=5 ! \
video/x-raw,format=GRAY8,width=2048,height=1536,framerate=15/1 ! \
videoconvert ! video/x-raw,format=YUY2 ! queue ! v4l2sink device=/dev/video0

Start and stop camera on state change

The Scailable AI Manager offers the possibility to add code to shell scripts that are run when the AI Manager changes state. These scripts live in /opt/sclbl/etc/scripts.
For setup we describe here, you could for instance add the following to the /opt/sclbl/etc/scripts/start_run.sh script:
#!/bin/sh
# make sure no running gst-launch processes are killed
killall gst-launch-1.0
# make sure there is a v4l2loopback device available
sudo modprobe v4l2loopback card_label="vid0cam"
# make a camera available for the input
gst-launch-1.0 --gst-plugin-path=/usr/local/lib/x86_64-linux-gnu/ aravissrc \
num-arv-buffers=1 gain=0.4 exposure=65000 ! \
video/x-raw,format=GRAY8,width=2448,height=2048,framerate=36/1 ! \
videoconvert ! video/x-raw,format=YUY2 ! \
v4l2sink device=/dev/video0 &
# wait a little to make sure the camera is streaming and available
sleep 1
This script is executed when the AI Manager changes state to start a run. It performs the following steps:
  • Kills any existing gst-launch-1.0 processes to ensure a clean start.
  • Loads the v4l2loopback kernel module with the specified card_label.
  • Launches the camera stream using gst-launch-1.0 and the specified parameters. You'll need to provide the full command or replace ... with the actual parameters.
  • Sleeps for 1 second to allow the camera stream to start.
Then add the following to the /opt/sclbl/etc/scripts/stop_run.sh script:
#!/bin/sh
# make sure the camera is stopped
killall gst-launch-1.0
This script is executed when the AI Manager changes state to stop a run. It simply kills any running gst-launch-1.0 processes, effectively stopping the camera stream.
Note that you may need to customize the camera stream command in start_run.sh based on your specific requirements, such as setting the correct exposure, resolution, and pipeline configuration.