Outils pour utilisateurs

Outils du site


streamer_des_images_opencv_avec_v4l2-loopback

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
streamer_des_images_opencv_avec_v4l2-loopback [2022/02/25 09:04] – [Ressources] sergestreamer_des_images_opencv_avec_v4l2-loopback [2022/03/03 12:35] (Version actuelle) – [Profondeur d'une RealSense D455] serge
Ligne 3: Ligne 3:
 <WRAP center round box 60%> <WRAP center round box 60%>
 Le **[[streaming_over_network_with_opencv_et_zeromq|stream avec zeromq]]** est sans latence, mais ne peut pas être reçu par Pure Data et VLC.\\ Le **[[streaming_over_network_with_opencv_et_zeromq|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) en lecture dans VLC, mais pas de latence si lecture avec OpenCV+**v4l2-loopback** a un peu de latence (0.1 à 0.2 seconde) en lecture dans VLC, mais pas de latence si lecture avec OpenCV\\ 
 +**Nous utilisons pyfakewebcam**
 </WRAP> </WRAP>
  
Ligne 13: Ligne 14:
   * https://github.com/umlaeute/v4l2loopback   * https://github.com/umlaeute/v4l2loopback
  
-=====Installation==== +=====Sources des exemples===== 
-A voir, nécessaire mais peut-être pas suffisant: +  * **[[https://github.com/sergeLabo/fakewebcam|fakewebcam de github.com/sergeLabo]]**
-  sudo apt install v4l2loopback-utils +
-  +
-<code bash> +
-sudo apt install python3-pip +
-python3 -m pip install --upgrade pip +
-sudo apt install python3-venv+
  
-cd /le/dossier/de/votre/projet +====Installation==== 
-python3 -m venv mon_env +Voir le README ci-dessus.
-source mon_env/bin/activate +
-python3 -m pip install opencv-python pyfakewebcam +
-</code>  +
  
 =====Exemple simple pour tester===== =====Exemple simple pour tester=====
 <file python cam_relay.py>   <file python cam_relay.py>  
-""" 
-Insert the v4l2loopback kernel module. 
-modprobe v4l2loopback devices=2 
-will create two fake webcam devices 
-""" 
-import time 
 import pyfakewebcam import pyfakewebcam
 import cv2 import cv2
Ligne 41: Ligne 27:
  
 cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
-camera = pyfakewebcam.FakeWebcam('/dev/video1', 640, 480)+camera = pyfakewebcam.FakeWebcam('/dev/video11', 640, 480) 
 while True: while True:
     ret, image = cap.read()     ret, image = cap.read()
Ligne 49: Ligne 36:
     if cv2.waitKey(1) == 27:     if cv2.waitKey(1) == 27:
         break         break
- 
-""" 
-Run the following command to see the output of the fake webcam. 
-ffplay /dev/video1 
-or open a camera in vlc 
-""" 
 </file> </file>
  
-Exécuter le script avec: +Run the following command to see the output of the fake webcam.\\ 
-  cd /le/dossier/de/votre/projet +ffplay /dev/video11\\ 
-  modprobe v4l2loopback devices=2 +or open the camera 11 in vlc
-  ./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 ===== ===== Profondeur d'une OAK-D Lite =====
- <code bash> 
-cd /le/dossier/de/votre/projet 
-source mon_env/bin/activate 
-python3 -m pip install depthai numpy 
-</code> 
- 
 <file python sender_oak_depth.py> <file python sender_oak_depth.py>
 import cv2 import cv2
Ligne 79: Ligne 49:
  
 pipeline = dai.Pipeline() pipeline = dai.Pipeline()
 +
 # Define a source - two mono (grayscale) cameras # Define a source - two mono (grayscale) cameras
 left = pipeline.createMonoCamera() left = pipeline.createMonoCamera()
Ligne 112: Ligne 83:
 depth.disparity.link(xout.input) depth.disparity.link(xout.input)
  
-camera = pyfakewebcam.FakeWebcam('/dev/video1', 640, 480)+camera = pyfakewebcam.FakeWebcam('/dev/video11', 640, 480)
  
 with dai.Device(pipeline) as device: with dai.Device(pipeline) as device:
Ligne 134: Ligne 105:
             break             break
 </file> </file>
-Exécuter le script avec: + 
-  cd /le/dossier/de/votre/projet +Ouvrir /dev/video11 dans VLC
-  modprobe v4l2loopback devices=2 +
-  ./mon_env/bin/python3 sender_oak_depth.py +
-Ouvrir /dev/video1 dans VLC+
 {{ :media_15:oak_depth_in_vlc.png?400 |}} {{ :media_15:oak_depth_in_vlc.png?400 |}}
-====Profondeur d'une RealSense D455====+=====Profondeur d'une RealSense D455=====
 Pour l'installation, voir **https://github.com/sergeLabo/grande_echelle#installation** Pour l'installation, voir **https://github.com/sergeLabo/grande_echelle#installation**
-<code bash> 
-cd /le/dossier/de/votre/projet 
-source mon_env/bin/activate 
-python3 -m pip install  pyrealsense2 numpy 
-</code> 
  
 <file python sender_rs_depth.py> <file python sender_rs_depth.py>
 +"""
 +Voir https://github.com/sergeLabo/fakewebcam
 +
 +Suppression du fond, voir
 +https://github.com/IntelRealSense/librealsense/blob/master/wrappers/python/examples/align-depth2color.py
 +"""
 +
 import os import os
 import time import time
Ligne 155: Ligne 125:
 import numpy as np import numpy as np
 import pyrealsense2 as rs import pyrealsense2 as rs
 +
 +
 +# Le faux device
 +VIDEO = '/dev/video11'
 +
 +# Avec ou sans slider pour régler CLIPPING_DISTANCE_IN_MILLIMETER
 +SLIDER = 1
 +# Réglable avec le slider
 +# We will be removing the background of objects more than
 +# CLIPPING_DISTANCE_IN_MILLIMETER away
 +CLIPPING_DISTANCE_IN_MILLIMETER = 2000
 +
  
 class MyRealSense: class MyRealSense:
-    """Initialise la caméra et permet d'accéder aux images""" + 
-    def __init__(self):+    def __init__(self, video, slider, clip): 
 +        self.video = video 
 +        self.slider = slider 
 +        self.clip = clip 
         self.width = 1280         self.width = 1280
         self.height = 720         self.height = 720
Ligne 181: Ligne 167:
                                 format=rs.format.z16,                                 format=rs.format.z16,
                                 framerate=30)                                 framerate=30)
-        self.pipeline.start(config)+ 
 +        profile = self.pipeline.start(config)
         self.align = rs.align(rs.stream.color)         self.align = rs.align(rs.stream.color)
         unaligned_frames = self.pipeline.wait_for_frames()         unaligned_frames = self.pipeline.wait_for_frames()
         frames = self.align.process(unaligned_frames)         frames = self.align.process(unaligned_frames)
-        depth = frames.get_depth_frame() + 
-        self.depth_intrinsic depth.profile.as_video_stream_profile().intrinsics+        # Getting the depth sensor's depth scale (see rs-align example for explanation) 
 +        depth_sensor profile.get_device().first_depth_sensor() 
 +        self.depth_scale depth_sensor.get_depth_scale() 
 +        print("Depth Scale is: " , self.depth_scale) 
         # Affichage de la taille des images         # Affichage de la taille des images
         color_frame = frames.get_color_frame()         color_frame = frames.get_color_frame()
Ligne 192: Ligne 183:
         print(f"Taille des images:"         print(f"Taille des images:"
               f"     {img.shape[1]}x{img.shape[0]}")               f"     {img.shape[1]}x{img.shape[0]}")
-        self.camera = pyfakewebcam.FakeWebcam('/dev/video1', 640480)+ 
 +        self.camera = pyfakewebcam.FakeWebcam(VIDEO, 1280, 720) 
 + 
 +        if self.slider: 
 +            self.create_slider() 
 + 
 +    def create_slider(self): 
 +        cv2.namedWindow('controls'
 +        cv2.createTrackbar('background', 'controls'1000, 8000, 
 +                            self.remove_background_callback) 
 +        cv2.setTrackbarPos('background', 'controls', self.clip) 
 +        cv2.namedWindow('depth', cv2.WND_PROP_FULLSCREEN) 
 + 
 +    def remove_background_callback(self, value): 
 +        if value != 1000: 
 +            self.clip = int(value)
  
     def run(self):     def run(self):
         """Boucle infinie, quitter avec Echap dans la fenêtre OpenCV"""         """Boucle infinie, quitter avec Echap dans la fenêtre OpenCV"""
 +
         while self.pose_loop:         while self.pose_loop:
-            frames = self.pipeline.wait_for_frames(timeout_ms=80)+ 
 +            # Get frameset of color and depth 
 +            frames = self.pipeline.wait_for_frames() 
 +            # frames.get_depth_frame() is a 640x360 depth image 
             # Align the depth frame to color frame             # Align the depth frame to color frame
             aligned_frames = self.align.process(frames)             aligned_frames = self.align.process(frames)
-            depth_color_frame = aligned_frames.get_depth_frame() 
  
-            # Convert 16bit dataà vérifier, c'est bon pour gray, en RGB ? +            # aligned_depth_frame is a 640x480 depth image 
-            detph_color_16bit = np.array(depth_color_framedtype=np.uint16+            aligned_depth_frame = aligned_frames.get_depth_frame() 
-            detph_color_16bit *= 256 +            color_frame = aligned_frames.get_color_frame() 
-            self.camera.schedule_frame(detph_color_16bit+ 
-            +            # Validate that both frames are valid 
 +            if not aligned_depth_frame or not color_frame: 
 +                continue 
 + 
 +            depth_image = np.asanyarray(aligned_depth_frame.get_data()) 
 +            color_image = np.asanyarray(color_frame.get_data()) 
 + 
 +            # Remove background - Set pixels further than clipping_distance to grey 
 +            # depth image is 1 channelcolor is 3 channels 
 +            depth_image_3d = np.dstack((depth_image, depth_image, depth_image)
 +            clipping_distance = self.clip / (1000*self.depth_scale) 
 +            bg_removed np.where((depth_image_3d > clipping_distance) |\ 
 +                        (depth_image_3d <= 0), 0, color_image) 
 + 
 +            depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, 
 +                                                                   alpha=0.03), 
 +                                                                   cv2.COLORMAP_JET) 
 +            images = np.hstack((bg_removed, depth_colormap)) 
 + 
 +            if self.slider: 
 +                cv2.imshow('depth', images) 
 + 
 +            self.camera.schedule_frame(bg_removed
             if cv2.waitKey(1) == 27:             if cv2.waitKey(1) == 27:
                 break                 break
 +
 +
  
 if __name__ == '__main__': if __name__ == '__main__':
-    mrs = MyRealSense()+ 
 +    mrs = MyRealSense(VIDEO, SLIDER, CLIPPING_DISTANCE_IN_MILLIMETER)
     mrs.run()     mrs.run()
 </file> </file>
- 
-Exécuter le script avec: 
-  cd /le/dossier/de/votre/projet 
-  modprobe v4l2loopback devices=2 
-  ./mon_env/bin/python3 sender_oak_depth.py 
      
-===Réception=== +=====Réception===== 
-Ouvrir /dev/video1 dans VLC\\+Ouvrir /dev/video11 dans VLC\\ 
 + 
 +{{:media_15:color_depth.png?800|}} 
 ou ou
 <file python receiver.py> <file python receiver.py>
streamer_des_images_opencv_avec_v4l2-loopback.1645779885.txt.gz · Dernière modification : 2022/02/25 09:04 de serge