Q&A
object detection api のSSDモデルで物体検出を行っています。
物体の位置情報とラベルをセットで取得したいのですが、どの部分をいじれば良いですか?
位置情報とラベルを別々で取得することはできるのですが...
関係がなさそうな部分は削ってあります。
python
1def draw_bounding_box_on_image_array(image, 2 ymin, 3 xmin, 4 ymax, 5 xmax, 6 color='red', 7 thickness=4, 8 display_str_list=(), 9 use_normalized_coordinates=True): 10 11 image_pil = Image.fromarray(np.uint8(image)).convert('RGB') 12 draw_bounding_box_on_image(image_pil, ymin, xmin, ymax, xmax, color, 13 thickness, display_str_list, 14 use_normalized_coordinates) 15 np.copyto(image, np.array(image_pil)) 16 17 18def draw_bounding_box_on_image(image, 19 ymin, 20 xmin, 21 ymax, 22 xmax, 23 color='red', 24 thickness=4, 25 display_str_list=(), 26 use_normalized_coordinates=True): 27 28 draw = ImageDraw.Draw(image) 29 im_width, im_height = image.size 30 if use_normalized_coordinates: 31 (left, right, top, bottom) = (xmin * im_width, xmax * im_width, 32 ymin * im_height, ymax * im_height) 33 else: 34 (left, right, top, bottom) = (xmin, xmax, ymin, ymax) 35 draw.line([(left, top), (left, bottom), (right, bottom), 36 (right, top), (left, top)], width=thickness, fill=color) 37 38 print (left, right, top, bottom,":") 39 40 try: 41 font = ImageFont.truetype('arial.ttf', 24) 42 except IOError: 43 font = ImageFont.load_default() 44 45 46 # If the total height of the display strings added to the top of the bounding 47 # box exceeds the top of the image, stack the strings below the bounding box 48 # instead of above. 49 display_str_heights = [font.getsize(ds)[1] for ds in display_str_list] 50 # Each display_str has a top and bottom margin of 0.05x. 51 total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights) 52 53 if top > total_display_str_height: 54 text_bottom = top 55 else: 56 text_bottom = bottom + total_display_str_height 57 # Reverse list and print from bottom to top. 58 for display_str in display_str_list[::-1]: 59 text_width, text_height = font.getsize(display_str) 60 margin = np.ceil(0.05 * text_height) 61 draw.rectangle( 62 [(left, text_bottom - text_height - 2 * margin), (left + text_width, 63 text_bottom)], 64 fill=color) 65 draw.text( 66 (left + margin, text_bottom - text_height - margin), 67 display_str, 68 fill='black', 69 font=font) 70 text_bottom -= text_height - 2 * margin 71 72 73def draw_bounding_boxes_on_image_array(image, 74 boxes, 75 color='red', 76 thickness=4, 77 display_str_list_list=()): 78 79 image_pil = Image.fromarray(image) 80 draw_bounding_boxes_on_image(image_pil, boxes, color, thickness, 81 display_str_list_list) 82 np.copyto(image, np.array(image_pil)) 83 84 85def draw_bounding_boxes_on_image(image, 86 boxes, 87 color='red', 88 thickness=4, 89 display_str_list_list=()): 90 91 boxes_shape = boxes.shape 92 if not boxes_shape: 93 return 94 if len(boxes_shape) != 2 or boxes_shape[1] != 4: 95 raise ValueError('Input must be of size [N, 4]') 96 for i in range(boxes_shape[0]): 97 display_str_list = () 98 if display_str_list_list: 99 display_str_list = display_str_list_list[i] 100 draw_bounding_box_on_image(image, boxes[i, 0], boxes[i, 1], boxes[i, 2], 101 boxes[i, 3], color, thickness, display_str_list) 102 103 104def draw_bounding_boxes_on_image_tensors(images, 105 boxes, 106 classes, 107 scores, 108 category_index, 109 max_boxes_to_draw=20, 110 min_score_thresh=0.2): 111 112 visualize_boxes_fn = functools.partial( 113 visualize_boxes_and_labels_on_image_array, 114 category_index=category_index, 115 instance_masks=None, 116 keypoints=None, 117 use_normalized_coordinates=True, 118 max_boxes_to_draw=max_boxes_to_draw, 119 min_score_thresh=min_score_thresh, 120 agnostic_mode=False, 121 line_thickness=4) 122 123 def draw_boxes(image_boxes_classes_scores): 124 """Draws boxes on image.""" 125 (image, boxes, classes, scores) = image_boxes_classes_scores 126 image_with_boxes = tf.py_func(visualize_boxes_fn, 127 [image, boxes, classes, scores], tf.uint8) 128 129 130 131 return image_with_boxes 132 133 images = tf.map_fn( 134 draw_boxes, (images, boxes, classes, scores), 135 dtype=tf.uint8, 136 back_prop=False) 137 return images 138 139 140def draw_keypoints_on_image_array(image, 141 keypoints, 142 color='red', 143 radius=2, 144 use_normalized_coordinates=True): 145 146 image_pil = Image.fromarray(np.uint8(image)).convert('RGB') 147 draw_keypoints_on_image(image_pil, keypoints, color, radius, 148 use_normalized_coordinates) 149 np.copyto(image, np.array(image_pil)) 150 151 152def visualize_boxes_and_labels_on_image_array(image, 153 boxes, 154 classes, 155 scores, 156 category_index, 157 instance_masks=None, 158 keypoints=None, 159 use_normalized_coordinates=False, 160 max_boxes_to_draw=20, 161 min_score_thresh=.15, 162 agnostic_mode=False, 163 line_thickness=4): 164 165 # Create a display string (and color) for every box location, group any boxes 166 # that correspond to the same location. 167 box_to_display_str_map = collections.defaultdict(list) 168 box_to_color_map = collections.defaultdict(str) 169 box_to_instance_masks_map = {} 170 box_to_keypoints_map = collections.defaultdict(list) 171 if not max_boxes_to_draw: 172 max_boxes_to_draw = boxes.shape[0] 173 for i in range(min(max_boxes_to_draw, boxes.shape[0])): 174 if scores is None or scores[i] > min_score_thresh: 175 box = tuple(boxes[i].tolist()) 176 if instance_masks is not None: 177 box_to_instance_masks_map[box] = instance_masks[i] 178 if keypoints is not None: 179 box_to_keypoints_map[box].extend(keypoints[i]) 180 if scores is None: 181 box_to_color_map[box] = 'black' 182 else: 183 if not agnostic_mode: 184 if classes[i] in category_index.keys(): 185 class_name = category_index[classes[i]]['name'] 186 else: 187 class_name = 'N/A' 188 display_str = '{}: {}%'.format( 189 class_name, 190 int(100*scores[i])) 191 print (class_name) 192 else: 193 display_str = 'score: {}%'.format(int(100 * scores[i])) 194 box_to_display_str_map[box].append(display_str) 195 if agnostic_mode: 196 box_to_color_map[box] = 'DarkOrange' 197 else: 198 box_to_color_map[box] = STANDARD_COLORS[ 199 classes[i] % len(STANDARD_COLORS)] 200 201 print (class_name,scores,":") 202 203 # Draw all boxes onto image. 204 for box, color in box_to_color_map.items(): 205 ymin, xmin, ymax, xmax = box 206 if instance_masks is not None: 207 draw_mask_on_image_array( 208 image, 209 box_to_instance_masks_map[box], 210 color=color 211 ) 212 draw_bounding_box_on_image_array( 213 image, 214 ymin, 215 xmin, 216 ymax, 217 xmax, 218 color=color, 219 thickness=line_thickness, 220 display_str_list=box_to_display_str_map[box], 221 use_normalized_coordinates=use_normalized_coordinates) 222 if keypoints is not None: 223 draw_keypoints_on_image_array( 224 image, 225 box_to_keypoints_map[box], 226 color=color, 227 radius=line_thickness / 2, 228 use_normalized_coordinates=use_normalized_coordinates) 229 return image 230
あなたの回答
tips
プレビュー