44 """Class for image processing""" 52 rospy.wait_for_service(rospy.get_name() +
'/get_cnn_metadata')
55 if mode ==
'segmentation':
56 get_metadata = rospy.ServiceProxy(
57 rospy.get_name() +
'/get_cnn_metadata', getSegmentationMetadata)
58 elif mode ==
'detection':
59 get_metadata = rospy.ServiceProxy(
60 rospy.get_name() +
'/get_cnn_metadata', getDetectionMetadata)
62 except rospy.ServiceException, e:
63 rospy.logerr(
"Could not get metadata: %e", e)
67 Overlay an image with a segmentation result for multiple classes. 71 image_to_show : numpy.array 72 An image of shape [width, height, 3] 73 segmentation : numpy.array 74 Segmentation of shape [width, height] 79 The image overlayed with the segmentation 83 assert image_to_overlay.shape[2] == 3
85 assert segmentation.ndim == 2
or segmentation.shape[2] == 1
93 if segmentation.ndim == 3:
94 segmentation = segmentation.squeeze(axis=2)
97 classes = np.unique(segmentation)
99 img = np.zeros(np.shape(image_to_overlay), dtype=np.uint8)
103 for classification
in self.metadata.class_metadata:
104 id = classification.classification.data
105 color = (classification.color.r,
106 classification.color.g, classification.color.b)
108 img[segmentation == id] = color
111 image_to_show = cv2.addWeighted(
116 def draw_detection(self, image, boxes, scores, labels, detection_size,
117 font=cv2.FONT_HERSHEY_SIMPLEX):
119 :param boxes, shape of [num, 4] 120 :param scores, shape of [num, ] 121 :param labels, shape of [num, ] 123 :param classes, the return list from the function `read_coco_names` 133 a = len(self.metadata.class_names)*1.0
134 hsv_tuples = [(x / a, 0.9, 1.0)
135 for x
in range(len(self.metadata.class_names))]
136 colors = list(map(
lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
138 map(
lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))
141 for i
in range(len(labels)):
143 bbox, score, label = boxes[i], scores[i], self.metadata.class_names[labels[i]]
144 bbox_text =
"%s %.2f" % (label.data, score)
147 text_size = cv2.getTextSize(bbox_text, font, 1, 2)
149 ratio = np.array((self.metadata.image_width, self.metadata.image_height),
150 dtype=float) / np.array(detection_size, dtype=float)
151 bbox = list((bbox.reshape(2, 2) * ratio).reshape(-1))
154 cv2.rectangle(image, (int(bbox[0]), int(bbox[1])), (int(
155 bbox[2]), int(bbox[3])), colors[labels[i]], thickness=3)
156 text_origin = bbox[:2]-np.array([0, text_size[0][1]])
157 cv2.rectangle(image, (int(text_origin[0]), int(text_origin[1])), (int(
158 text_origin[0]+text_size[0][0]), int(text_origin[1]+text_size[0][1])), colors[labels[i]], thickness=-1)
159 cv2.putText(image, bbox_text, (int(bbox[0]), int(
160 bbox[1])), font, 1, (0, 0, 0), thickness=2)
167 with open(class_file_name,
'r') as data: 168 for ID, name
in enumerate(data):
169 names[ID] = name.strip(
'\n')