cam_driver 5.64 KB
Newer Older
Inès EL HADRI's avatar
Inès EL HADRI committed
1 2 3 4 5 6 7
#!/usr/bin/python3
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
import pyrealsense2 as rs
import signal, time, numpy as np
import sys, cv2
Inès EL HADRI's avatar
Inès EL HADRI committed
8
from cv_bridge import CvBridge
Inès EL HADRI's avatar
Inès EL HADRI committed
9 10 11 12 13 14 15 16


class CameraDriver(Node):

    def __init__(self, name="cam_driver", timerFreq = 1/60.0):
        super().__init__(name) # Create the node

        # Initialize publishers
17 18 19 20
        self._rgb_publisher = self.create_publisher(Image, 'cam_rgb', 10)
        self._depth_publisher = self.create_publisher(Image, 'cam_depth', 10)
        self._infra_publisher_1 = self.create_publisher(Image, 'cam_infra_1',10)
        self._infra_publisher_2 = self.create_publisher(Image, 'cam_infra_2',10)
Inès EL HADRI's avatar
Inès EL HADRI committed
21 22

        # Initialize a clock for the publisher
Inès EL HADRI's avatar
Inès EL HADRI committed
23
        self.create_timer(timerFreq, self.publish_imgs)
Inès EL HADRI's avatar
Inès EL HADRI committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51


        # ------------------------------ Initialize camera ------------------------------ #

        ## Configure depth and color streams
        self._pipeline = rs.pipeline()
        self._config = rs.config()

        ## Get device product line for setting a supporting resolution
        pipeline_wrapper = rs.pipeline_wrapper(self._pipeline)
        pipeline_profile = self._config.resolve(pipeline_wrapper)
        device = pipeline_profile.get_device()
        device_product_line = str(device.get_info(rs.camera_info.product_line))

        print( f"Connect: {device_product_line}" )
        found_rgb = True
        for s in device.sensors:
            print( "Name:" + s.get_info(rs.camera_info.name) )
            if s.get_info(rs.camera_info.name) == 'RGB Camera':
                found_rgb = True

        if not (found_rgb):
            print("Depth camera required !!!")
            exit(0)

        ## Configure stream width, height, format and frequency
        self._config.enable_stream(rs.stream.color, width=848, height=480, format=rs.format.bgr8, framerate=60)
        self._config.enable_stream(rs.stream.depth, width=848, height=480, format=rs.format.z16, framerate=60)
52 53 54
        self._config.enable_stream(rs.stream.infrared, 1, width=848, height=480, format=rs.format.y8, framerate=60)
        self._config.enable_stream(rs.stream.infrared, 2, width=848, height=480, format=rs.format.y8, framerate=60)

Inès EL HADRI's avatar
Inès EL HADRI committed
55 56 57 58 59
        ## Start the acquisition    
        self._pipeline.start(self._config)

    def read_imgs(self):
        """lire et traduire images camera"""
60 61
        
        # Get frames
Inès EL HADRI's avatar
Inès EL HADRI committed
62
        frames = self._pipeline.wait_for_frames()
63 64
        
        # Split frames
Inès EL HADRI's avatar
Inès EL HADRI committed
65 66
        color_frame = frames.first(rs.stream.color)
        depth_frame = frames.first(rs.stream.depth)
67 68 69 70 71 72
        infra_frame_1 = frames.get_infrared_frame(1)
        infra_frame_2 = frames.get_infrared_frame(2)


        if not (depth_frame and color_frame and infra_frame_1 and infra_frame_2):
            print("eh oh le truc y marche pas")
Inès EL HADRI's avatar
Inès EL HADRI committed
73 74 75 76 77
            return
        
        # Convert images to numpy arrays
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
78 79
        infra_image_1 = np.asanyarray(infra_frame_1.get_data())
        infra_image_2 = np.asanyarray(infra_frame_2.get_data())
Inès EL HADRI's avatar
Inès EL HADRI committed
80

81 82 83 84 85
        # Apply colormap on depth and IR images (image must be converted to 8-bit per pixel first)
        depth_colormap   = cv2.applyColorMap(cv2.convertScaleAbs(depth_image,   alpha=0.03), cv2.COLORMAP_JET)
        infra_colormap_1 = cv2.applyColorMap(cv2.convertScaleAbs(infra_image_1, alpha=1), cv2.COLORMAP_JET)
        infra_colormap_2 = cv2.applyColorMap(cv2.convertScaleAbs(infra_image_2, alpha=1), cv2.COLORMAP_JET)
        
Inès EL HADRI's avatar
Inès EL HADRI committed
86
        bridge=CvBridge()
87 88
        # Convert to ROS2 Image type
        # - RGB Image
Inès EL HADRI's avatar
Inès EL HADRI committed
89 90 91
        self._rgb_image = bridge.cv2_to_imgmsg(color_image,"bgr8")
        self._rgb_image.header.stamp = self.get_clock().now().to_msg()
        self._rgb_image.header.frame_id = "image"
92 93 94
        # - Depth Image
        self._depth_image = bridge.cv2_to_imgmsg(depth_colormap, "bgr8")
        self._depth_image.header.stamp = self._rgb_image.header.stamp
Inès EL HADRI's avatar
Inès EL HADRI committed
95
        self._depth_image.header.frame_id = "depth"
96 97 98 99 100 101 102 103
        # - IR1 Image
        self._infra1_image = bridge.cv2_to_imgmsg(infra_colormap_1,"bgr8")
        self._infra1_image.header.stamp = self._rgb_image.header.stamp
        self._infra1_image.header.frame_id = "infrared_1"
        # - IR2 Image
        self._infra2_image = bridge.cv2_to_imgmsg(infra_colormap_2,"bgr8")
        self._infra2_image.header.stamp = self._rgb_image.header.stamp
        self._infra2_image.header.frame_id = "infrared_2"
Inès EL HADRI's avatar
Inès EL HADRI committed
104

105
        return self._rgb_image, self._depth_image, self._infra1_image, self._infra2_image
Inès EL HADRI's avatar
Inès EL HADRI committed
106 107 108 109

    def publish_imgs(self):
        self.publish_rgb()
        self.publish_depth()
110
        self.publish_IR()
Inès EL HADRI's avatar
Inès EL HADRI committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126


    def publish_rgb(self):
        """ 
        Publish RGB image
        """

        self._rgb_publisher.publish(self._rgb_image)

        return 0
    
    def publish_depth(self):
        """ 
        Publish depth colormap image
        """

Inès EL HADRI's avatar
Inès EL HADRI committed
127
        self._depth_publisher.publish(self._depth_image)
Inès EL HADRI's avatar
Inès EL HADRI committed
128 129 130

        return 0

131 132 133 134 135 136 137 138 139 140
    def publish_IR(self):
        """ 
        Publish infrared colormap image
        """

        self._infra_publisher_1.publish(self._infra1_image)
        self._infra_publisher_2.publish(self._infra2_image)

        return 0

Inès EL HADRI's avatar
Inès EL HADRI committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
# Capture ctrl-c event
isOk=True
def signalInterruption(signum, frame):
    global isOk
    print( "\nCtrl-c pressed" )
    isOk= False

def main():
    """
    Main loop
    """

    signal.signal(signal.SIGINT, signalInterruption)

    rclpy.init()
Inès EL HADRI's avatar
Inès EL HADRI committed
156
    rosNode = CameraDriver(timerFreq=1.0/120)
Inès EL HADRI's avatar
Inès EL HADRI committed
157 158 159 160 161 162 163 164 165 166 167
    isOk = True
    while isOk:
        rosNode.read_imgs()
        rclpy.spin_once(rosNode, timeout_sec=0.001)


    rosNode.destroy_node()
    rclpy.shutdown()

if __name__ == "__main__":
    main()