def visualize_boxes_and_labels_on_image_array(current_frame_number, image, mode, color_recognition_status, boxes, classes, scores, category_index, y_reference=None, deviation=None, instance_masks=None, keypoints=None, use_normalized_coordinates=False, max_boxes_to_draw=20, min_score_thresh=.5, agnostic_mode=False, line_thickness=4): """Overlay labeled boxes on an image with formatted scores and label names. This function groups boxes that correspond to the same location and creates a display string for each detection and overlays these on the image. Note that this function modifies the image in place, and returns that same image. Args: image: uint8 numpy array with shape (img_height, img_width, 3) boxes: a numpy array of shape [N, 4] classes: a numpy array of shape [N]. Note that class indices are 1-based, and match the keys in the label map. scores: a numpy array of shape [N] or None. If scores=None, then this function assumes that the boxes to be plotted are groundtruth boxes and plot all boxes as black with no classes or scores. category_index: a dict containing category dictionaries (each holding category index `id` and category name `name`) keyed by category indices. instance_masks: a numpy array of shape [N, image_height, image_width], can be None keypoints: a numpy array of shape [N, num_keypoints, 2], can be None use_normalized_coordinates: whether boxes is to be interpreted as normalized coordinates or not. max_boxes_to_draw: maximum number of boxes to visualize. If None, draw all boxes. min_score_thresh: minimum score threshold for a box to be visualized agnostic_mode: boolean (default: False) controlling whether to evaluate in class-agnostic mode or not. This mode will display scores but ignore classes. line_thickness: integer (default: 4) controlling line width of the boxes. Returns: uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes. """ # Create a display string (and color) for every box location, group any boxes # that correspond to the same location. csv_line_util = "not_available" counter = 0 ROI_POSITION.insert(0, y_reference) DEVIATION.insert(0, deviation) is_vehicle_detected = [] mode_number.insert(0, mode) is_color_recognition_enable.insert(0, color_recognition_status) box_to_display_str_map = collections.defaultdict(list) box_to_color_map = collections.defaultdict(str) box_to_instance_masks_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 keypoints is not None: box_to_keypoints_map[box].extend(keypoints[i]) if scores is None: box_to_color_map[box] = 'black' else: 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 = '{}: {}%'.format(class_name, int(100 * scores[i])) else: display_str = 'score: {}%'.format(int(100 * scores[i])) box_to_display_str_map[box].append(display_str) if agnostic_mode: box_to_color_map[box] = 'DarkOrange' else: box_to_color_map[box] = STANDARD_COLORS[ classes[i] % len(STANDARD_COLORS)] if (mode == 1): counting_mode = "" # Draw all boxes onto image. for box, color in box_to_color_map.items(): ymin, xmin, ymax, xmax = box if instance_masks is not None: draw_mask_on_image_array(image, box_to_instance_masks_map[box], color=color) display_str_list = box_to_display_str_map[box] if (mode == 1): counting_mode = counting_mode + str(display_str_list) # we are interested just vehicles (i.e. cars and trucks) if (("car" not in display_str_list[0]) or ("truck" not in display_str_list[0]) or ("bus" not in display_str_list[0])): is_vehicle_detected, csv_line, update_csv = draw_bounding_box_on_image_array( current_frame_number, image, ymin, xmin, ymax, xmax, color=color, thickness=line_thickness, display_str_list=box_to_display_str_map[box], use_normalized_coordinates=use_normalized_coordinates) if keypoints is not None: draw_keypoints_on_image_array( image, box_to_keypoints_map[box], color=color, radius=line_thickness / 2, use_normalized_coordinates=use_normalized_coordinates) if (1 in is_vehicle_detected): counter = 1 del is_vehicle_detected[:] is_vehicle_detected = [] if (class_name == "boat"): class_name = "truck" csv_line_util = class_name + "," + csv_line if (mode == 1): counting_mode = counting_mode.replace("['", " ").replace("']", " ").replace( "%", "") counting_mode = ''.join([ i for i in counting_mode.replace("['", " ").replace( "']", " ").replace("%", "") if not i.isdigit() ]) counting_mode = str(custom_string_util.word_count(counting_mode)) counting_mode = counting_mode.replace("{", "").replace("}", "") return counter, csv_line_util, counting_mode else: return counter, csv_line_util
def visualize_boxes_and_labels_on_image_array(bboxes,current_frame_number, image, mode, boxes, classes, scores, category_index, targeted_objects=None, y_reference=None, deviation=None, instance_masks=None, keypoints=None, use_normalized_coordinates=False, max_boxes_to_draw=None, min_score_thresh=.5, agnostic_mode=False, line_thickness=4): """Overlay labeled boxes on an image with formatted scores and label names. This function groups boxes that correspond to the same location and creates a display string for each detection and overlays these on the image. Note that this function modifies the image in place, and returns that same image. Args: image: uint8 numpy array with shape (img_height, img_width, 3) boxes: a numpy array of shape [N, 4] classes: a numpy array of shape [N]. Note that class indices are 1-based, and match the keys in the label map. scores: a numpy array of shape [N] or None. If scores=None, then this function assumes that the boxes to be plotted are groundtruth boxes and plot all boxes as black with no classes or scores. category_index: a dict containing category dictionaries (each holding category index `id` and category name `name`) keyed by category indices. instance_masks: a numpy array of shape [N, image_height, image_width], can be None keypoints: a numpy array of shape [N, num_keypoints, 2], can be None use_normalized_coordinates: whether boxes is to be interpreted as normalized coordinates or not. max_boxes_to_draw: maximum number of boxes to visualize. If None, draw all boxes. min_score_thresh: minimum score threshold for a box to be visualized agnostic_mode: boolean (default: False) controlling whether to evaluate in class-agnostic mode or not. This mode will display scores but ignore classes. line_thickness: integer (default: 4) controlling line width of the boxes. Returns: uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes. """ # Create a display string (and color) for every box location, group any boxes # that correspond to the same location. csv_line_util = "not_available" counter = 0 ROI_POSITION.insert(0,y_reference) DEVIATION.insert(0,deviation) is_vehicle_detected = [] mode_number.insert(0,mode) box_to_display_str_map = collections.defaultdict(list) box_to_color_map = collections.defaultdict(str) box_to_instance_masks_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 keypoints is not None: box_to_keypoints_map[box].extend(keypoints[i]) if scores is None: box_to_color_map[box] = 'black' else: 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 = '{}: {}%'.format(class_name,int(100*scores[i])) else: display_str = 'score: {}%'.format(int(100 * scores[i])) box_to_display_str_map[box].append(display_str) if agnostic_mode: box_to_color_map[box] = 'DarkOrange' else: box_to_color_map[box] = STANDARD_COLORS[ classes[i] % len(STANDARD_COLORS)] if(mode == 1): counting_mode = "" count=["","","","","",""] # Draw all boxes onto image. for box, color in box_to_color_map.items(): ymin, xmin, ymax, xmax = box '''if instance_masks is not None: draw_mask_on_image_array( image, box_to_instance_masks_map[box], color=color )''' im_height, im_width, channels = image.shape #print(xmin*im_width,ymin*im_height,xmax*im_width,ymax*im_height) for i in range(len(bboxes)): if(xmin*im_width>=bboxes[i][0] and ymin*im_height>=bboxes[i][1] and xmax*im_width<=bboxes[i][0]+bboxes[i][2] and ymax*im_height<=bboxes[i][1]+bboxes[i][3]): display_str_list=box_to_display_str_map[box] count[i] = count[i] + str(display_str_list) track_low.append(int((xmin*im_width+xmax*im_width)/2)) track_high.append(int((ymin*im_height+ymax*im_height)/2)) for j in range(len(track_low)): #cv2.line(first_frame,track_low[j],track_low[j-1],4, (0, 0, 255), -1) cv2.circle(first_frame,(track_low[j],track_high[j]), 4, (0, 0, 255), -1) else: display_str_list="" if(mode == 1 and targeted_objects == None): counting_mode = counting_mode + str(display_str_list) #cv2.imshow('Path Tracker',first_frame) if (targeted_objects == None): is_vehicle_detected, csv_line, update_csv = draw_bounding_box_on_image_array(bboxes,current_frame_number, image, ymin, xmin, ymax, xmax, color=color, thickness=line_thickness, display_str_list=box_to_display_str_map[box], use_normalized_coordinates=use_normalized_coordinates) if(1 in is_vehicle_detected): counter = 1 del is_vehicle_detected[:] is_vehicle_detected = [] csv_line_util = class_name + "," + csv_line if(mode == 1): counting_mode = counting_mode.replace("['", " ").replace("']", " ").replace("%", "") counting_mode = ''.join([i for i in counting_mode.replace("['", " ").replace("']", " ").replace("%", "") if not i.isdigit()]) counting_mode = str(custom_string_util.word_count(counting_mode)) counting_mode = counting_mode.replace("{", "").replace("}", "") for i in range(len(bboxes)): count[i] = count[i].replace("['", " ").replace("']", " ").replace("%", "") count[i] = ''.join([i for i in count[i].replace("['", " ").replace("']", " ").replace("%", "") if not i.isdigit()]) count[i] = str(custom_string_util.word_count(count[i])) count[i] = count[i].replace("{", "").replace("}", "") return counter, csv_line_util, counting_mode ,count else: return counter, csv_line_util