Outils pour utilisateurs

Outils du site


streamer_des_images_opencv_avec_v4l2-loopback

Ceci est une ancienne révision du document !


Streamer des images OpenCV avec v4l2-loopback

Le stream avec zeromq est sans latence, mais ne peut pas être reçu par Pure Data et VLC.
v4l2-loopback a un peu de latence (0.1 à 0.2 seconde) mais c'est un flux v4l2

Ressources

Installation

A voir, nécessaire mais peut-être pas suffisant:

sudo apt install v4l2loopback-utils
sudo apt install python3-pip
python3 -m pip install --upgrade pip
sudo apt install python3-venv
 
cd /le/dossier/de/votre/projet
python3 -m venv mon_env
source mon_env/bin/activate
python3 -m pip install opencv-python pyfakewebcam

Exemple simple pour tester

cam_relay.py
"""
Insert the v4l2loopback kernel module.
modprobe v4l2loopback devices=2
will create two fake webcam devices
"""
import time
import pyfakewebcam
import cv2
import numpy as np
 
cap = cv2.VideoCapture(0)
camera = pyfakewebcam.FakeWebcam('/dev/video1', 640, 480)
while True:
    ret, image = cap.read()
    if ret:
        # # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        camera.schedule_frame(image)
    if cv2.waitKey(1) == 27:
        break
 
"""
Run the following command to see the output of the fake webcam.
ffplay /dev/video1
or open a camera in vlc
"""

Exécuter le script avec:

cd /le/dossier/de/votre/projet
modprobe v4l2loopback devices=2
./mon_env/bin/python3 cam_relay.py

Dans un autre terminal

ffplay /dev/video1

Il faudra peut-être adapter les numéro de /dev/video

Profondeur d'une OAK-D Lite

cd /le/dossier/de/votre/projet
source mon_env/bin/activate
python3 -m pip install depthai numpy
sender_oak_depth.py
import cv2
import depthai as dai
import numpy as np
import pyfakewebcam
 
pipeline = dai.Pipeline()
# Define a source - two mono (grayscale) cameras
left = pipeline.createMonoCamera()
left.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
left.setBoardSocket(dai.CameraBoardSocket.LEFT)
 
right = pipeline.createMonoCamera()
right.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
right.setBoardSocket(dai.CameraBoardSocket.RIGHT)
 
# Create a node that will produce the depth map
# (using disparity output as it's easier to visualize depth this way)
depth = pipeline.createStereoDepth()
depth.setConfidenceThreshold(200)
 
# Options: MEDIAN_OFF, KERNEL_3x3, KERNEL_5x5, KERNEL_7x7 (default)
median = dai.StereoDepthProperties.MedianFilter.KERNEL_7x7 # For depth filtering
depth.setMedianFilter(median)
 
# Better handling for occlusions:
depth.setLeftRightCheck(False)
# Closer-in minimum depth, disparity range is doubled:
depth.setExtendedDisparity(False)
# Better accuracy for longer distance, fractional disparity 32-levels:
depth.setSubpixel(False)
 
left.out.link(depth.left)
right.out.link(depth.right)
 
# Create output
xout = pipeline.createXLinkOut()
xout.setStreamName("disparity")
depth.disparity.link(xout.input)
 
camera = pyfakewebcam.FakeWebcam('/dev/video1', 640, 480)
 
with dai.Device(pipeline) as device:
    device.startPipeline()
 
    # Output queue will be used to get the disparity frames from the outputs defined above
    q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
 
    while True:
        inDepth = q.get()  # blocking call, will wait until a new data has arrived
        frame = inDepth.getFrame()
        frame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX)
 
        depth_gray_image = cv2.resize(np.asanyarray(frame), (640, 480),
                                interpolation = cv2.INTER_AREA)
        # v4l2 doit être RGB
        color = cv2.cvtColor(depth_gray_image, cv2.COLOR_GRAY2RGB)
        camera.schedule_frame(color)
 
        if cv2.waitKey(1) == 27:
            break

Profondeur d'une RealSense D455

Pour l'installation, voir https://github.com/sergeLabo/grande_echelle#installation

cd /le/dossier/de/votre/projet
source mon_env/bin/activate
python3 -m pip install  pyrealsense2 numpy
sender_rs_depth.py
 
streamer_des_images_opencv_avec_v4l2-loopback.1645363569.txt.gz · Dernière modification : 2022/02/20 13:26 de serge