def update_track(det_frame, idd, det_bb, lst_det, frame): """ Cheecks if a bounding box is already labeled in the past 3 frames or in the first 5 frames. """ n_past_frames = np.min([2,frame]) for past_frame in range(frame-n_past_frames, frame): # If the past bb is labeled with 0 it is a removed bb on the remove_overlaps past_frame_bb = [det_bb[i] for i,num in enumerate(lst_det) if (num == past_frame and det_bb[i][2]!=0)] ious =[] for i_past_frame_bb in past_frame_bb: iou = bbox_iou(det_frame[3:7], i_past_frame_bb[3:7]) ious.append(iou) if len(ious)==0: continue else: arg_max = np.argmax(ious) if np.max(ious)>=0.4: det_frame[2] = past_frame_bb[arg_max][2] past_frame_bb.pop(arg_max) break else: continue # We also check if the bb is in the first 5 frames as they are bg and they should # appear in all the frames mm = np.min([5,frame]) for bg_frame in range(0,mm): bg_frame_bb = [det_bb[i] for i,num in enumerate(lst_det) if (num == bg_frame and det_bb[i][2]!=0)] ious = [] for i_bg_frame_bb in bg_frame_bb: iou = bbox_iou(det_frame[3:7], i_bg_frame_bb[3:7]) ious.append(iou) if len(ious)==0: continue else: arg_max = np.argmax(ious) if np.max(ious)>=0.4: det_frame[2] = bg_frame_bb[arg_max][2] bg_frame_bb.pop(arg_max) break else: continue # If after checking with the past frames it still does not have a label then # we consider it corresponds to a new track. if det_frame[2] == 0: det_frame[2] = idd idd += 1 return det_frame, idd
def remove_overlaps(frame_n_bb): # First we check if there are any overlaped detections and remove them for i in range(0, len(frame_n_bb)): length = len(frame_n_bb) for j in range(i + 1, length): if length == j: break iou = bbox_iou(frame_n_bb[i][3:7], frame_n_bb[j][3:7]) if iou > 0.9: frame_n_bb.pop(j) length = length - 1 return frame_n_bb
def remove_overlaps(frame_n_bb): """ This function detects if we have any overlapped detections, two bboxes for the same object, and removes one. """ # First we check if there are any overlaped detections and remove them for i in range(0, len(frame_n_bb)): length = len(frame_n_bb) for j in range(i+1, length): if length == j: break iou = bbox_iou(frame_n_bb[i][3:7], frame_n_bb[j][3:7]) if iou > 0.9: frame_n_bb.pop(j) length = length - 1 return frame_n_bb
def animate_iou(dict_frame_gt_bb): """ Given a dictionary with frames and ground truth bounding boxes computes mean IoU between the bounding boxes of each frame and the ground truth bounding boxes with a gussian noise added. Returns an animation of the evolution of mean IoU over time """ ious = [] for key, value in sorted(dict_frame_gt_bb.items(), key=lambda x: int(x[0])): value = [v[:-1] for v in value] noisy_bb = add_noise_to_bbox(value, -5, 5) iou = 0.0 for i, gt_bb in enumerate(value): iou += bbox_iou([float(i) for i in gt_bb], noisy_bb[i]) ious.append(iou / len(value)) frames = list(range(0, len(ious))) animation = plot_animation(frames, ious, "Frame", "IoU", [0, 1], 10) return animation
def update_track_of(flow, det_frame, det_bb, idd, lst_det, frame): """ Moves a bb from the actual position to the position it occuied in the past frame. If there is a bb with a large IoU we assign the same track value, it not we create a new track value. """ xtl_1, ytl_1, xbr_1, ybr_1 = np.array(det_frame[3:7]).astype(int) bb_flow = flow[ytl_1:ybr_1, xtl_1:xbr_1, :] xtl = xtl_1 + np.mean(bb_flow[..., 0]) xbr = xbr_1 + np.mean(bb_flow[..., 0]) ytl = ytl_1 - np.mean(bb_flow[..., 1]) ybr = ybr_1 - np.mean(bb_flow[..., 1]) old_det = [xtl, ytl, xbr, ybr] # If the past bb is labeled with 0 it is a removed bb on the remove_overlaps past_frame_bb = [ det_bb[i] for i, num in enumerate(lst_det) if (num == frame - 1 and det_bb[i][2] != 0) ] ious = [] for i_past_frame_bb in past_frame_bb: iou = bbox_iou(old_det, i_past_frame_bb[3:7]) ious.append(iou) if len(ious) == 0: pass else: arg_max = np.argmax(ious) if np.max(ious) >= 0.7: det_frame[2] = past_frame_bb[arg_max][2] else: pass if det_frame[2] == 0: det_frame[2] = idd idd += 1 return det_frame, idd