Open danielfbg opened 5 years ago
Found the below solution in one of the issues while looking for the same thing. In /utils/visualization_utils.py update the following piece of code
def return_coordinates( image, boxes, classes, scores, category_index, instance_masks=None, instance_boundaries=None, keypoints=None, use_normalized_coordinates=False, max_boxes_to_draw=20, min_score_thresh=.5, agnostic_mode=False, line_thickness=4, groundtruth_box_visualization_color='black', skip_scores=False, skip_labels=False):
box_to_display_str_map = collections.defaultdict(list)
box_to_color_map = collections.defaultdict(str)
box_to_instance_masks_map = {}
box_to_instance_boundaries_map = {}
box_to_score_map = {}
box_to_keypoints_map = collections.defaultdict(list)
if not max_boxes_to_draw:
max_boxes_to_draw = boxes.shape[0]
for i in range(min(max_boxes_to_draw, boxes.shape[0])):
if scores is None or scores[i] > min_score_thresh:
box = tuple(boxes[i].tolist())
if instance_masks is not None:
box_to_instance_masks_map[box] = instance_masks[i]
if instance_boundaries is not None:
box_to_instance_boundaries_map[box] = instance_boundaries[i]
if keypoints is not None:
box_to_keypoints_map[box].extend(keypoints[i])
if scores is None:
box_to_color_map[box] = groundtruth_box_visualization_color
else:
display_str = ''
if not skip_labels:
if not agnostic_mode:
if classes[i] in category_index.keys():
class_name = category_index[classes[i]]['name']
else:
class_name = 'N/A'
display_str = str(class_name)
if not skip_scores:
if not display_str:
display_str = '{}%'.format(int(100*scores[i]))
else:
display_str = '{}: {}%'.format(display_str, int(100*scores[i]))
box_to_display_str_map[box].append(display_str)
box_to_score_map[box] = scores[i]
if agnostic_mode:
box_to_color_map[box] = 'DarkOrange'
else:
box_to_color_map[box] = STANDARD_COLORS[
classes[i] % len(STANDARD_COLORS)]
# Draw all boxes onto image.
coordinates_list = []
counter_for = 0
for box, color in box_to_color_map.items():
ymin, xmin, ymax, xmax = box
height, width, channels = image.shape
ymin = int(ymin*height)
ymax = int(ymax*height)
xmin = int(xmin*width)
xmax = int(xmax*width)
coordinates_list.append([ymin, ymax, xmin, xmax, (box_to_score_map[box]*100), class_name])
counter_for = counter_for + 1
return coordinates_list
Update Object_detection_image.py with -
im = 0 for coordinate in coordinates: (y1, y2, x1, x2, acc, classification) = coordinate height = y2-y1 width = x2-x1 crop = image[y1:y1+height, x1:x1+width] EXT_PATH = os.path.join(CWD_PATH, 'extracted_images') name = str(classification)+str(im)+'.png' cv2.imwrite(os.path.join(EXT_PATH, name), crop) im += 1
a = extractor.list_files(CWD_PATH)
data_dict = {} # Dictionary to collect the data in the image as key:value pairs
for i in a:
k = str(i[:4])
c = extractor.tess(os.path.join(CWD_PATH, 'extracted_images', i))
data_dict[k] = c
data_json = json.dumps(data_dict)
for k,v in data_dict.items():
print(k,v)
Hi,
extractor is python package? If I run the object_detection_image.py file after update the above codes the visualization_utils return_coordinate function returns only a empty list.
please give any solution for this error...
Hi,
for my project I want to save the parts of an image enclosed by the Bounding Boxes as .jpg for feeding in another CNN for further classification.
How can I adjust the code so i get the sub-images out of my input- image ?