-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
61 lines (51 loc) · 2 KB
/
publisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
52
53
54
55
56
57
58
59
60
61
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image as RosImage
from cv_bridge import CvBridge
import cv2
import pyzed.sl as sl
class ZedPublisher(Node):
def __init__(self):
super().__init__('zed_publisher')
self.publisher = self.create_publisher(RosImage, 'cam_in', 10)
self.bridge = CvBridge()
# Initialize ZED camera
self.zed = sl.Camera()
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD720
init_params.camera_fps = 30
init_params.depth_mode = sl.DEPTH_MODE.NONE # We only need RGB for YOLO
# Open the camera
err = self.zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
self.get_logger().error(f"Failed to open ZED camera: {err}")
return
self.image = sl.Mat()
self.timer = self.create_timer(1 / 30.0, self.publish_frame)
def publish_frame(self):
if self.zed.grab() == sl.ERROR_CODE.SUCCESS:
self.zed.retrieve_image(self.image, sl.VIEW.LEFT)
# Convert ZED image to OpenCV format
frame = self.image.get_data()
if frame.ndim == 2: # Grayscale image
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR) # Convert to BGR
elif frame.shape[2] == 4: # RGBA image
frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2BGR) # Convert to BGR
# Convert to ROS Image message
ros_image = self.bridge.cv2_to_imgmsg(frame, encoding='bgr8')
self.publisher.publish(ros_image)
else:
self.get_logger().warn("Failed to grab ZED frame")
def __del__(self):
self.zed.close()
def main():
rclpy.init()
zed_publisher = ZedPublisher()
executor = rclpy.executors.SingleThreadedExecutor()
executor.add_node(zed_publisher)
executor.spin()
zed_publisher.destroy_node()
rclpy.shutdown()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()