1
1
#!/usr/bin/env python3
2
+ import array
2
3
from ada_feeding_msgs .action import SegmentFromPoint
3
4
from ada_feeding_msgs .msg import Mask
4
5
import cv2
15
16
import time
16
17
17
18
19
+ # The fixed header that ROS2 Humble's compressed depth image transport plugin prepends to
20
+ # the data. The exact value was empirically determined, but the below link shows the code
21
+ # that prepends additional data:
22
+ #
23
+ # https://github.com/ros-perception/image_transport_plugins/blob/5ef79d74c4347e6a2d151df63230d5fea1357137/compressed_depth_image_transport/src/codec.cpp#L337
24
+ _COMPRESSED_DEPTH_16UC1_HEADER = array .array (
25
+ "B" , [0 , 0 , 0 , 0 , 46 , 32 , 133 , 4 , 192 , 24 , 60 , 78 ]
26
+ )
27
+
28
+
18
29
class SegmentFromPointNode (Node ):
19
30
def __init__ (self , sleep_time = 2.0 , send_feedback_hz = 10 ):
20
31
"""
@@ -44,8 +55,8 @@ def __init__(self, sleep_time=2.0, send_feedback_hz=10):
44
55
45
56
# Subscribe to the depth topic, to store the latest image
46
57
self .depth_subscriber = self .create_subscription (
47
- Image ,
48
- "/camera/aligned_depth_to_color/image_raw" ,
58
+ CompressedImage ,
59
+ "/camera/aligned_depth_to_color/image_raw/compressedDepth " ,
49
60
self .depth_callback ,
50
61
1 ,
51
62
)
@@ -125,7 +136,9 @@ def goal_callback(self, goal_request):
125
136
126
137
# Reject if there is already an active goal
127
138
if self .active_goal_request is not None :
128
- self .get_logger ().info ("Rejecting goal request" )
139
+ self .get_logger ().info (
140
+ "Rejecting goal request because there is already an active goal"
141
+ )
129
142
return GoalResponse .REJECT
130
143
131
144
# Reject if this node has not received an RGB and depth image
@@ -137,8 +150,11 @@ def goal_callback(self, goal_request):
137
150
or self .latest_depth_msg is None
138
151
or self .camera_info is None
139
152
):
140
- self .get_logger ().info ("Rejecting cancel request" )
141
- return CancelResponse .REJECT
153
+ self .get_logger ().info (
154
+ "Rejecting goal request because RGB and depth images "
155
+ "have not been received"
156
+ )
157
+ return GoalResponse .REJECT
142
158
143
159
# Otherwise accept
144
160
self .get_logger ().info ("Accepting goal request" )
@@ -183,7 +199,12 @@ def segment_image(self, seed_point, result, segmentation_success):
183
199
result .camera_info = camera_info
184
200
img = self .bridge .compressed_imgmsg_to_cv2 (latest_img_msg , "bgr8" )
185
201
width , height , _ = img .shape
186
- depth_img = self .bridge .imgmsg_to_cv2 (latest_depth_msg , "passthrough" )
202
+ depth_img = cv2 .imdecode (
203
+ np .frombuffer (
204
+ latest_depth_msg .data [len (_COMPRESSED_DEPTH_16UC1_HEADER ) :], np .uint8
205
+ ),
206
+ cv2 .IMREAD_UNCHANGED ,
207
+ )
187
208
188
209
# Sleep (dummy segmentation)
189
210
time .sleep (self .sleep_time )
0 commit comments