webcam-loopback-manipulation-screensharing-and-stuff git · main
vcam: v4l2 loopback with overlays, filters, OpenCV and RVM matting
C 51.3% C++ 40.7% Markdown 6.9% Makefile 1%git clone https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff.gitwget https://git.christianimmanuel.de/linux-system/webcam-loopback-manipulation-screensharing-and-stuff/archive/webcam-loopback-manipulation-screensharing-and-stuff.tar.gzscripts/vcam-setup raw
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only
# vcam — Copyright (C) 2026 the vcam authors. GPL-2.0, see LICENSE.
# vcam-setup – diagnose and configure v4l2loopback for Chromium compatibility
#
# Chromium-based browsers pick the FIRST /dev/video* node by default.
# If that's your real camera and vcam is using it, you get NotReadableError.
# The fix is to make the v4l2loopback device sit at a LOWER video_nr than
# the real camera.
#
# This script:
# - lists current video devices and analyses ordering
# - writes /etc/modprobe.d/vcam.conf with video_nr=0 + correct params
# - detects whether the system uses systemd, OpenRC, runit, or none of
# those, and gives you concrete next steps for YOUR init system
#
# Run with sudo to install.
set -eu
# ── help ─────────────────────────────────────────────────────
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
sed -n '/^# vcam-setup/,/^$/p' "$0" | sed 's/^# \?//'
echo "Usage:"
echo " vcam-setup show current state and diagnosis"
echo " sudo vcam-setup --install install /etc/modprobe.d/vcam.conf"
echo " sudo vcam-setup --uninstall remove it"
echo " vcam-setup --models download the OpenCV model files (face/person/emotion)"
echo " vcam-setup --models-style download the 5 neural style transfer models"
echo " vcam-setup --models-rvm download RVM video matting (best personfx quality)"
echo " vcam-setup --ort download ONNX Runtime (runs RVM correctly)"
echo " vcam-setup --models-anime [ghibli|hayao|shinkai]"
echo " download an AnimeGAN img2img model for 'genai'"
echo " vcam-setup --models-depth download MiDaS v2.1 small (depthfx: every pixel gets a distance)"
echo " vcam-setup --models-pose download BlazePose landmarks (posefx: skeleton/energy-hands)"
exit 0
fi
# ── OpenCV model download ────────────────────────────────────
# Fetches the ONNX models the optional OpenCV DNN filters need
# (facefx, personfx). Both are small and permissively licensed
# (YuNet: MIT, PP-HumanSeg: Apache-2.0). Installs into a per-user
# data dir by default; override with VCAM_MODEL_DIR or --system.
if [ "${1:-}" = "--models" ]; then
if [ "${2:-}" = "--system" ]; then
MODEL_DIR="${VCAM_MODEL_DIR:-/usr/local/share/vcam/models}"
else
MODEL_DIR="${VCAM_MODEL_DIR:-$HOME/.local/share/vcam/models}"
fi
# name|url pairs (HuggingFace raw, resolve/main)
YUNET_URL="https://huggingface.co/opencv/face_detection_yunet/resolve/main/face_detection_yunet_2023mar.onnx"
SEG_URL="https://huggingface.co/opencv/human_segmentation_pphumanseg/resolve/main/human_segmentation_pphumanseg_2023mar.onnx"
FER_URL="https://huggingface.co/opencv/facial_expression_recognition/resolve/main/facial_expression_recognition_mobilefacenet_2022july.onnx"
YUNET_FILE="face_detection_yunet_2023mar.onnx"
SEG_FILE="human_segmentation_pphumanseg_2023mar.onnx"
FER_FILE="facial_expression_recognition_mobilefacenet_2022july.onnx"
# pick a downloader
if command -v curl >/dev/null 2>&1; then
DL="curl -fL --retry 3 -o"
elif command -v wget >/dev/null 2>&1; then
DL="wget -O"
else
echo "Error: need curl or wget to download models." >&2
exit 1
fi
echo "Model directory: $MODEL_DIR"
if ! mkdir -p "$MODEL_DIR" 2>/dev/null; then
echo "Error: cannot create $MODEL_DIR (try --system with sudo, or set VCAM_MODEL_DIR)." >&2
exit 1
fi
rc=0
for pair in "$YUNET_FILE|$YUNET_URL" "$SEG_FILE|$SEG_URL" "$FER_FILE|$FER_URL"; do
f=${pair%%|*}
u=${pair#*|}
if [ -s "$MODEL_DIR/$f" ]; then
echo " already have $f — skipping"
continue
fi
echo " downloading $f ..."
# shellcheck disable=SC2086
if $DL "$MODEL_DIR/$f.part" "$u" 2>/dev/null && [ -s "$MODEL_DIR/$f.part" ]; then
mv "$MODEL_DIR/$f.part" "$MODEL_DIR/$f"
echo " ok: $MODEL_DIR/$f"
else
rm -f "$MODEL_DIR/$f.part"
echo " FAILED to fetch $f" >&2
rc=1
fi
done
if [ "$rc" -eq 0 ]; then
echo ""
echo "Models ready. The OpenCV filters (facefx, personfx, moodfx) can find them now."
echo "If you built without OpenCV, rebuild with: make OPENCV=1 && sudo make install"
fi
exit "$rc"
fi
# ── BlazePose landmarks for the 'posefx' filter ──────────────
# OpenCV-zoo export, same host that serves our face/seg/FER models.
if [ "${1:-}" = "--models-pose" ]; then
MODELS_DIR="${VCAM_MODELS_DIR:-$HOME/.local/share/vcam/models}"
mkdir -p "$MODELS_DIR" || { echo "Error: cannot create $MODELS_DIR" >&2; exit 1; }
DEST="$MODELS_DIR/pose_estimation_mediapipe_2023mar.onnx"
U="https://huggingface.co/opencv/pose_estimation_mediapipe/resolve/main/pose_estimation_mediapipe_2023mar.onnx"
if [ -s "$DEST" ]; then
echo "already present: $DEST"
else
if command -v curl >/dev/null 2>&1; then DL="curl -fL --retry 3 -o";
elif command -v wget >/dev/null 2>&1; then DL="wget -O"; else
echo "Error: need curl or wget." >&2; exit 1; fi
echo "downloading BlazePose landmarks (~5 MB) ..."
# shellcheck disable=SC2086
if $DL "$DEST" "$U" && [ -s "$DEST" ] \
&& [ "$(wc -c < "$DEST")" -gt 1000000 ]; then
echo "ok: $DEST"
else
rm -f "$DEST"; echo "FAILED to download $U" >&2; exit 1
fi
fi
echo ""
echo "Use it:"
echo " vcam-ctl filter add posefx 2 # energy orbs in your palms"
echo " vcam-ctl filter add posefx 0 # glowing skeleton"
exit 0
fi
# ── MiDaS depth model for the 'depthfx' filter ───────────────
# Official release asset, URL taken from the MiDaS repo's own docs
# (github.com/isl-org/MiDaS, tag v2_1). ~64 MB.
if [ "${1:-}" = "--models-depth" ]; then
MODELS_DIR="${VCAM_MODELS_DIR:-$HOME/.local/share/vcam/models}"
mkdir -p "$MODELS_DIR" || { echo "Error: cannot create $MODELS_DIR" >&2; exit 1; }
DEST="$MODELS_DIR/model-small.onnx"
U="https://github.com/isl-org/MiDaS/releases/download/v2_1/model-small.onnx"
if [ -s "$DEST" ]; then
echo "already present: $DEST"
else
if command -v curl >/dev/null 2>&1; then DL="curl -fL --retry 3 -o";
elif command -v wget >/dev/null 2>&1; then DL="wget -O"; else
echo "Error: need curl or wget." >&2; exit 1; fi
echo "downloading MiDaS v2.1 small ..."
# shellcheck disable=SC2086
if $DL "$DEST" "$U" && [ -s "$DEST" ] \
&& [ "$(wc -c < "$DEST")" -gt 1000000 ]; then
echo "ok: $DEST"
else
rm -f "$DEST"
echo "FAILED to download $U" >&2; exit 1
fi
fi
echo ""
echo "Use it:"
echo " vcam-ctl filter add depthfx 0 # true depth-of-field"
echo " vcam-ctl filter add depthfx 3 # 3D parallax wiggle"
exit 0
fi
# ── AnimeGAN img2img models for the 'genai' neural filter ────
# Official AnimeGANv3 releases (github.com/TachibanaYoshino/AnimeGANv3).
# Their documented I/O convention (RGB, /127.5-1, sizes %8==0) is
# exactly what vcam's genai engine implements. The Ghibli asset's URL
# is taken from the project's own docs; for the others we probe a few
# plausible release tags, since assets are spread across releases.
if [ "${1:-}" = "--models-anime" ]; then
STYLE="${2:-ghibli}"
case "$STYLE" in
ghibli) ASSET="AnimeGANv3_large_Ghibli_c1_e299.onnx"; TAGS="1.0 v1.0" ;;
hayao) ASSET="AnimeGANv3_Hayao_36.onnx"; TAGS="v1.1.0 1.1.0 v1.0 1.0" ;;
shinkai) ASSET="AnimeGANv3_Shinkai_37.onnx"; TAGS="v1.1.0 1.1.0 v1.0 1.0" ;;
*) echo "Error: style must be ghibli, hayao, or shinkai" >&2; exit 1 ;;
esac
MODELS_DIR="${VCAM_MODELS_DIR:-$HOME/.local/share/vcam/models}"
mkdir -p "$MODELS_DIR" || { echo "Error: cannot create $MODELS_DIR" >&2; exit 1; }
DEST="$MODELS_DIR/$ASSET"
if [ -s "$DEST" ]; then
echo "already present: $DEST"
else
if command -v curl >/dev/null 2>&1; then DL="curl -fL --retry 2 -o";
elif command -v wget >/dev/null 2>&1; then DL="wget -O";
else echo "Error: need curl or wget." >&2; exit 1; fi
GOT=""
for TAG in $TAGS; do
U="https://github.com/TachibanaYoshino/AnimeGANv3/releases/download/$TAG/$ASSET"
echo "trying $U ..."
# shellcheck disable=SC2086
if $DL "$DEST" "$U" 2>/dev/null && [ -s "$DEST" ] \
&& [ "$(wc -c < "$DEST")" -gt 1000000 ]; then
GOT=1; break
fi
rm -f "$DEST"
done
if [ -z "$GOT" ]; then
echo "FAILED — the asset may live under a different release tag." >&2
echo "Browse https://github.com/TachibanaYoshino/AnimeGANv3/releases ," >&2
echo "download any .onnx, then: vcam-ctl filter genmodel /path/to/it" >&2
exit 1
fi
echo "ok: $DEST"
fi
echo ""
echo "Use it:"
echo " vcam-ctl filter genmodel $DEST"
echo " vcam-ctl filter add genai # full anime transform"
echo " vcam-ctl filter set 0 p2 224 # smaller = faster if laggy"
exit 0
fi
# ── ONNX Runtime: the backend that runs RVM correctly ────────
# OpenCV 5's young DNN graph engine loads RVM but miscomputes it
# (empty mattes). ONNX Runtime is RVM's reference runtime. This
# fetches the official prebuilt library (MIT license, ~60 MB) into
# ~/.local/share/vcam/onnxruntime, then vcam is rebuilt with ORT=1.
if [ "${1:-}" = "--ort" ]; then
ORT_VER="${VCAM_ORT_VERSION:-1.27.0}"
ORT_DIR="${VCAM_ORT_DIR:-$HOME/.local/share/vcam/onnxruntime}"
F="onnxruntime-linux-x64-$ORT_VER.tgz"
U="https://github.com/microsoft/onnxruntime/releases/download/v$ORT_VER/$F"
if [ -e "$ORT_DIR/include/onnxruntime_c_api.h" ]; then
echo "already installed: $ORT_DIR"
echo "build with: make OPENCV=1 ORT=1 && sudo make install"
exit 0
fi
if command -v curl >/dev/null 2>&1; then DL="curl -fL --retry 3 -o";
elif command -v wget >/dev/null 2>&1; then DL="wget -O";
else echo "Error: need curl or wget." >&2; exit 1; fi
TMP="${TMPDIR:-/tmp}/vcam-ort.$$"
mkdir -p "$TMP" "$ORT_DIR" || { echo "Error: cannot create dirs" >&2; exit 1; }
echo "downloading ONNX Runtime $ORT_VER (~60 MB) ..."
# shellcheck disable=SC2086
if $DL "$TMP/$F" "$U" && [ -s "$TMP/$F" ]; then
tar -xzf "$TMP/$F" -C "$ORT_DIR" --strip-components=1
rm -rf "$TMP"
if [ -e "$ORT_DIR/include/onnxruntime_c_api.h" ]; then
echo "ok: $ORT_DIR"
echo ""
echo "Now rebuild vcam with the backend:"
echo " make OPENCV=1 ORT=1 && sudo make install"
echo " pkill vcam; vcam &"
echo "(and make sure the RVM model is there: vcam-setup --models-rvm)"
else
echo "extraction looks wrong — check $ORT_DIR" >&2; exit 1
fi
else
rm -rf "$TMP"
echo "FAILED to download. Manual route:" >&2
echo " 1. fetch $U" >&2
echo " 2. tar -xzf $F --strip-components=1 -C $ORT_DIR" >&2
exit 1
fi
exit 0
fi
# ── RVM: Robust Video Matting (recommended personfx upgrade) ─
# A recurrent video-native matting network: temporally stable, hair-
# level alpha. GPL-3.0 licensed weights, fetched from the official
# GitHub release; ~15 MB. vcam uses it automatically when present and
# falls back to PP-HumanSeg when not (or if this OpenCV can't load it).
if [ "${1:-}" = "--models-rvm" ]; then
if [ "${2:-}" = "--system" ]; then
MODEL_DIR="${VCAM_MODEL_DIR:-/usr/local/share/vcam/models}"
else
MODEL_DIR="${VCAM_MODEL_DIR:-$HOME/.local/share/vcam/models}"
fi
if command -v curl >/dev/null 2>&1; then DL="curl -fL --retry 3 -o";
elif command -v wget >/dev/null 2>&1; then DL="wget -O";
else echo "Error: need curl or wget." >&2; exit 1; fi
mkdir -p "$MODEL_DIR" || { echo "Error: cannot create $MODEL_DIR" >&2; exit 1; }
F="rvm_mobilenetv3_fp32.onnx"
U="https://github.com/PeterL1n/RobustVideoMatting/releases/download/v1.0.0/$F"
if [ -s "$MODEL_DIR/$F" ]; then
echo "already have $F"
exit 0
fi
echo "downloading $F (~15 MB) ..."
# shellcheck disable=SC2086
if $DL "$MODEL_DIR/$F.part" "$U" && [ -s "$MODEL_DIR/$F.part" ]; then
mv "$MODEL_DIR/$F.part" "$MODEL_DIR/$F"
echo "ok: $MODEL_DIR/$F"
echo "Restart vcam; personfx will pick it up automatically."
echo "(If your OpenCV can't run it, vcam prints a note and falls back.)"
else
rm -f "$MODEL_DIR/$F.part"
echo "FAILED — check your network, or download manually from:" >&2
echo " $U" >&2
exit 1
fi
exit 0
fi
# ── Neural style transfer models ─────────────────────────────
# Fetches the five fast-neural-style networks (Johnson et al.) used by
# the 'styleai' filter. MIT-licensed ONNX exports, ~6.5 MB each, from
# the ONNX Model Zoo mirror on HuggingFace.
if [ "${1:-}" = "--models-style" ]; then
if [ "${2:-}" = "--system" ]; then
MODEL_DIR="${VCAM_MODEL_DIR:-/usr/local/share/vcam/models}"
else
MODEL_DIR="${VCAM_MODEL_DIR:-$HOME/.local/share/vcam/models}"
fi
if command -v curl >/dev/null 2>&1; then
DL="curl -fL --retry 3 -o"
elif command -v wget >/dev/null 2>&1; then
DL="wget -O"
else
echo "Error: need curl or wget to download models." >&2
exit 1
fi
echo "Model directory: $MODEL_DIR"
mkdir -p "$MODEL_DIR" || { echo "Error: cannot create $MODEL_DIR" >&2; exit 1; }
rc=0
for name in mosaic-9 candy-9 rain-princess-9 udnie-9 pointilism-9; do
f="$name.onnx"
u="https://huggingface.co/onnxmodelzoo/$name/resolve/main/$f"
if [ -s "$MODEL_DIR/$f" ]; then
echo " already have $f — skipping"
continue
fi
echo " downloading $f ..."
# shellcheck disable=SC2086
if $DL "$MODEL_DIR/$f.part" "$u" 2>/dev/null && [ -s "$MODEL_DIR/$f.part" ]; then
mv "$MODEL_DIR/$f.part" "$MODEL_DIR/$f"
echo " ok: $MODEL_DIR/$f"
else
rm -f "$MODEL_DIR/$f.part"
echo " FAILED to fetch $f" >&2
rc=1
fi
done
[ "$rc" -eq 0 ] && { echo ""; echo "Styles ready. Try: vcam-ctl filter add styleai 1"; }
exit "$rc"
fi
INIT_SYSTEM=""
detect_init() {
if [ -d /run/systemd/system ]; then
INIT_SYSTEM="systemd"
elif command -v rc-status >/dev/null 2>&1 && rc-status >/dev/null 2>&1; then
INIT_SYSTEM="openrc"
elif command -v sv >/dev/null 2>&1 && [ -d /etc/service ] || [ -d /var/service ]; then
INIT_SYSTEM="runit"
elif [ -d /etc/rc.d ] || [ -d /etc/init.d ]; then
INIT_SYSTEM="sysv-or-bsd"
else
INIT_SYSTEM="unknown"
fi
}
list_devices() {
echo "── Current /dev/video* devices ─────────────────────────"
for dev in /dev/video*; do
[ -e $dev ] || continue
local name="" driver=""
if command -v v4l2-ctl >/dev/null 2>&1; then
name=$(v4l2-ctl -d "$dev" --info 2>/dev/null | \
awk -F': ' '/Card type/ {print $2; exit}')
driver=$(v4l2-ctl -d "$dev" --info 2>/dev/null | \
awk -F': ' '/Driver name/ {print $2; exit}')
fi
printf " %-15s %-25s (driver: %s)\n" "$dev" "${name:-?}" "${driver:-?}"
done
echo ""
}
# ── show current state ───────────────────────────────────────
list_devices
detect_init
LOOPBACK_DEV=""
REAL_CAMS=""
for dev in /dev/video*; do
[ -e $dev ] || continue
if command -v v4l2-ctl >/dev/null 2>&1; then
driver=$(v4l2-ctl -d "$dev" --info 2>/dev/null | \
awk -F': ' '/Driver name/ {print $2; exit}')
if [ "$driver" = "v4l2 loopback" ]; then
LOOPBACK_DEV="$dev"
elif [ -n "$driver" ]; then
REAL_CAMS="$REAL_CAMS $dev"
fi
fi
done
# ── analysis ─────────────────────────────────────────────────
echo "── Analysis ────────────────────────────────────────────"
echo " Init system : $INIT_SYSTEM"
if [ -z "$LOOPBACK_DEV" ]; then
echo " Loopback : NOT FOUND"
else
echo " Loopback : $LOOPBACK_DEV"
fi
if [ -z "$(echo $REAL_CAMS | tr -d " ")" ]; then
echo " Real cam : NOT FOUND"
else
echo " Real cam(s) : $REAL_CAMS"
fi
echo ""
if [ -n "$LOOPBACK_DEV" ] && [ -n "$(echo $REAL_CAMS | tr -d " ")" ]; then
loopback_nr=${LOOPBACK_DEV#/dev/video}
# Find the lowest-numbered real cam
lowest_real=999
for rc in $REAL_CAMS; do
n=${rc#/dev/video}
[ "$n" -lt "$lowest_real" ] && lowest_real=$n
done
echo "── Chromium compatibility ──────────────────────────────"
if [ "$loopback_nr" -lt "$lowest_real" ]; then
echo " OK — loopback at $LOOPBACK_DEV comes before real cam at /dev/video$lowest_real"
echo " Chromium should pick the loopback by default."
else
echo " PROBLEM — loopback ($LOOPBACK_DEV) is AFTER real cam (/dev/video$lowest_real)"
echo " Chromium will try /dev/video$lowest_real first → NotReadableError."
echo ""
echo " To fix: sudo $0 --install"
fi
echo ""
fi
# ── install ──────────────────────────────────────────────────
if [ "${1:-}" = "--install" ]; then
if [ $EUID -ne 0 ]; then
echo "Error: --install requires root." >&2
exit 1
fi
# /etc/modprobe.d/ works regardless of init system — it's read by
# modprobe itself, not by an init system.
mkdir -p /etc/modprobe.d
cat > /etc/modprobe.d/vcam.conf <<MODPROBE
# vcam virtual camera (v4l2loopback)
# video_nr=0 claim /dev/video0 so Chromium picks it by default
# card_label human-readable name in browser camera lists
# exclusive_caps=1 advertise only V4L2_CAP_VIDEO_CAPTURE — required for Firefox/Chromium
# max_buffers=8 enough to support vcam-write + multiple simultaneous
# readers (browser, ffmpeg recording, ffplay preview).
# With max_buffers=2 the second reader gets EBUSY at
# VIDIOC_REQBUFS — so e.g. 'vcam-ctl record start'
# failed if the cam was already in use.
options v4l2loopback video_nr=0 card_label="vcam" exclusive_caps=1 max_buffers=8
MODPROBE
echo "Wrote /etc/modprobe.d/vcam.conf"
# Also write systemd's modules-load.d if the directory exists, since
# it's harmless on other systems and helpful where systemd is present.
if [ -d /etc/modules-load.d ]; then
echo "v4l2loopback" > /etc/modules-load.d/vcam.conf
echo "Wrote /etc/modules-load.d/vcam.conf (systemd-style autoload)"
fi
echo ""
echo "── Init-system-specific next steps ─────────────────────"
case "$INIT_SYSTEM" in
systemd)
cat <<MSG
systemd: /etc/modules-load.d/vcam.conf already written above.
systemd will auto-load v4l2loopback at boot before USB devices.
To activate now without rebooting:
sudo modprobe -r uvcvideo
sudo modprobe -r v4l2loopback
sudo modprobe v4l2loopback
sudo modprobe uvcvideo
MSG
;;
openrc)
cat <<MSG
OpenRC: append the module name to the modules autoload list:
echo "v4l2loopback" | sudo tee -a /etc/conf.d/modules
Then to activate immediately:
sudo modprobe -r uvcvideo
sudo modprobe -r v4l2loopback
sudo modprobe v4l2loopback
sudo modprobe uvcvideo
MSG
;;
runit)
cat <<MSG
runit (e.g. Void Linux): add v4l2loopback to the kernel module loader:
echo "v4l2loopback" | sudo tee -a /etc/modules.d/vcam.conf
Some distros instead use /etc/modules — try both.
Activate immediately:
sudo modprobe -r uvcvideo
sudo modprobe -r v4l2loopback
sudo modprobe v4l2loopback
sudo modprobe uvcvideo
MSG
;;
*)
cat <<MSG
Manual / unknown init: load v4l2loopback at boot via whatever
mechanism you normally use (rc.local, /etc/modules, an init script,
etc.). The key requirement is that v4l2loopback is loaded BEFORE
the uvcvideo module / before USB cameras enumerate.
Activate immediately:
sudo modprobe -r uvcvideo
sudo modprobe -r v4l2loopback
sudo modprobe v4l2loopback
sudo modprobe uvcvideo
Or for a permanent setup on a manual init system, add this to your
boot script (e.g. /etc/rc.local) before any device setup:
modprobe v4l2loopback
MSG
;;
esac
echo ""
echo "After reloading the modules, run this script again to verify."
echo "Then start vcam with the new device numbers, e.g.:"
echo " vcam -i /dev/video1 -o /dev/video0"
exit 0
fi
if [ "${1:-}" = "--uninstall" ]; then
if [ $EUID -ne 0 ]; then
echo "Error: --uninstall requires root." >&2
exit 1
fi
rm -f /etc/modprobe.d/vcam.conf
rm -f /etc/modules-load.d/vcam.conf 2>/dev/null || true
echo "Removed /etc/modprobe.d/vcam.conf"
echo "Reload modules or reboot to fully revert."
exit 0
fi
echo "Usage:"
echo " $0 show current state and diagnosis"
echo " sudo $0 --install install /etc/modprobe.d/vcam.conf"
echo " sudo $0 --uninstall remove it"