1010from cv_bridge import CvBridge
1111from detectron2 .utils .visualizer import VisImage
1212from detic .predictor import VisualizationDemo
13- from jsk_recognition_msgs .msg import Label , LabelArray , VectorArray
13+ from jsk_recognition_msgs .msg import Label , LabelArray , Rect , RectArray , VectorArray
1414from node_config import NodeConfig
1515from sensor_msgs .msg import Image
1616from std_msgs .msg import Header
@@ -28,6 +28,7 @@ class InferenceRawResult:
2828 visualization : Optional [VisImage ]
2929 header : Header
3030 detected_class_names : List [str ]
31+ boxes : List [List [float ]]
3132
3233 def get_ros_segmentaion_image (self ) -> Image :
3334 seg_img = _cv_bridge .cv2_to_imgmsg (self .segmentation_raw_image , encoding = "32SC1" )
@@ -68,6 +69,14 @@ def get_segmentation_info(self) -> SegmentationInfo:
6869 header = self .header )
6970 return seg_info
7071
72+ def get_rect_array (self ) -> RectArray :
73+ rects = [Rect (x = int (box [0 ]),
74+ y = int (box [1 ]),
75+ width = int (box [2 ] - box [0 ]),
76+ height = int (box [3 ] - box [1 ])) for box in self .boxes ]
77+ rec_arr = RectArray (header = self .header , rects = rects )
78+ return rec_arr
79+
7180
7281class DeticWrapper :
7382 predictor : VisualizationDemo
@@ -122,12 +131,14 @@ def infer(self, msg: Image) -> InferenceRawResult:
122131 pred_masks = list (instances .pred_masks )
123132 scores = instances .scores .tolist ()
124133 class_indices = instances .pred_classes .tolist ()
134+ boxes = list (instances .pred_boxes )
125135
126136 if len (scores ) > 0 and self .node_config .output_highest :
127137 best_index = np .argmax (scores )
128138 pred_masks = [pred_masks [best_index ]]
129139 scores = [scores [best_index ]]
130140 class_indices = [class_indices [best_index ]]
141+ boxes = [boxes [best_index ]]
131142
132143 if self .node_config .verbose :
133144 rospy .loginfo ("{} with highest score {}" .format (self .class_names [class_indices [0 ]], scores [best_index ]))
@@ -150,5 +161,6 @@ def infer(self, msg: Image) -> InferenceRawResult:
150161 scores ,
151162 visualized_output ,
152163 msg .header ,
153- detected_classes_names )
164+ detected_classes_names ,
165+ boxes )
154166 return result
0 commit comments