def ious(atlbrs, btlbrs): """ Compute cost based on IoU :type atlbrs: list[tlbr] | np.ndarray :type atlbrs: list[tlbr] | np.ndarray :rtype ious np.ndarray """ ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float) if ious.size == 0: return ious ious = bbox_ious( np.ascontiguousarray(atlbrs, dtype=np.float), np.ascontiguousarray(btlbrs, dtype=np.float) ) return ious
def ious(atlbrs, btlbrs): """Compute cost based on IoU Arguments: atlbrs: np.array, list of boxes btlbrs: np.array, list of boxes Returns: ious: np.array, matrix of IoUs between each box Raises: """ ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float) if ious.size == 0: return ious ious = bbox_ious( np.ascontiguousarray(atlbrs, dtype=np.float), np.ascontiguousarray(btlbrs, dtype=np.float), ) return ious
def visualize_comparison(dataroot, seq, save_dir, result_dir_1, result_dir_2, compile_images_only=False): import pandas as pd from cython_bbox import bbox_overlaps as bbox_ious from tracker import matching from tracking_utils import visualization as vis res_1 = pd.read_csv(osp.join(result_dir_1, f"{seq}.txt"), sep=',', names=[ 'frame', 'id', 'left', 'top', 'width', 'height', 'conf', 'misc1', 'misc2', 'misc3' ]) res_2 = pd.read_csv(osp.join(result_dir_2, f"{seq}.txt"), sep=',', names=[ 'frame', 'id', 'left', 'top', 'width', 'height', 'conf', 'misc1', 'misc2', 'misc3' ]) img_root = osp.join(dataroot, seq, 'img1') save_root_1 = osp.join(osp.abspath(save_dir), osp.basename(result_dir_1), seq) os.makedirs(save_root_1, exist_ok=True) save_root_2 = osp.join(osp.abspath(save_dir), osp.basename(result_dir_2), seq) os.makedirs(save_root_2, exist_ok=True) if not compile_images_only: # for frame in np.unique(res_1['frame']): for frame in range(np.min(res_1['frame']), np.max(res_1['frame'])): # if frame == 250: # break res_1_frame = res_1[res_1['frame'] == frame] res_2_frame = res_2[res_2['frame'] == frame] res_1_boxes_np = np.array( res_1_frame[['left', 'top', 'width', 'height']]) res_1_boxes_np[:, 2:] += res_1_boxes_np[:, :2] res_2_boxes_np = np.array( res_2_frame[['left', 'top', 'width', 'height']]) res_2_boxes_np[:, 2:] += res_2_boxes_np[:, :2] img1 = cv2.imread(osp.join(img_root, f"{frame:06d}.jpg")) img2 = cv2.imread(osp.join(img_root, f"{frame:06d}.jpg")) if res_1_boxes_np.shape[0] > 0 and res_2_boxes_np.shape[0] > 0: iou = bbox_ious(res_1_boxes_np, res_2_boxes_np) matches, u_res_1, u_res_2 = matching.linear_assignment( -iou, thresh=-0.7) if len(matches) > 0: # Plot the matchings in normal colors matches_res_1 = res_1_frame.iloc[matches[:, 0]] matches_res_2 = res_2_frame.iloc[matches[:, 1]] # matches_res_2['id'].iloc[matches[:, 1]] = matches_res_1['id'].iloc[matches[:, 0]] online_ids = matches_res_1['id'].to_list() # online_ids = matches_res_2['id'].to_list() matches_res_1_np = np.array( matches_res_1[['left', 'top', 'width', 'height']]) matches_res_2_np = np.array( matches_res_2[['left', 'top', 'width', 'height']]) online_im_1 = vis.plot_tracking(img1, matches_res_1_np, online_ids, frame_id=frame, fps=-1, line_thickness=1) online_im_2 = vis.plot_tracking(img2, matches_res_2_np, online_ids, frame_id=frame, fps=-1, line_thickness=1) else: online_im_1 = img1 online_im_2 = img2 # ipdb.set_trace() # Plot the unmatched in thicker colors unmatches_res_1 = res_1_frame.iloc[u_res_1] unmatches_res_1_np = np.array( unmatches_res_1[['left', 'top', 'width', 'height']]) if len(unmatches_res_1) > 0: if len(unmatches_res_1.shape) == 1: unmatched_res_1_ids = [unmatches_res_1['id']] scores = [unmatches_res_1['conf']] unmatches_res_1_np = unmatches_res_1_np.reshape(1, -1) else: unmatched_res_1_ids = unmatches_res_1['id'].to_list() scores = unmatches_res_1['conf'].to_list() online_im_1 = vis.plot_tracking(online_im_1, unmatches_res_1_np, unmatched_res_1_ids, frame_id=frame, fps=-1, line_thickness_unmatched=4) # ipdb.set_trace() print( f"{len(unmatches_res_1)} unmatches found in frame {frame}" ) # Plot the unmatched in thicker colors unmatches_res_2 = res_2_frame.iloc[u_res_2] unmatches_res_2_np = np.array( unmatches_res_2[['left', 'top', 'width', 'height']]) if len(unmatches_res_2) > 0: if len(unmatches_res_2.shape) == 1: unmatched_res_2_ids = [unmatches_res_2['id']] scores = [unmatches_res_2['conf']] unmatches_res_2_np = unmatches_res_2_np.reshape(1, -1) else: unmatched_res_2_ids = unmatches_res_2['id'].to_list() scores = unmatches_res_2['conf'].to_list() online_im_2 = vis.plot_tracking(online_im_2, unmatches_res_2_np, unmatched_res_2_ids, frame_id=frame, fps=-1, line_thickness_unmatched=4) cv2.imwrite(osp.join(save_root_1, f"{frame:06d}.jpg"), online_im_1) cv2.imwrite(osp.join(save_root_2, f"{frame:06d}.jpg"), online_im_2) else: if len(res_1_boxes_np) > 0 and len(res_2_boxes_np) == 0: matches_res_1 = res_1_frame matches_res_1_np = np.array( matches_res_1[['left', 'top', 'width', 'height']]) if len(matches_res_1.shape) == 1: online_ids = [matches_res_1['id']] scores = [matches_res_1['conf']] else: online_ids = matches_res_1['id'].to_list() scores = matches_res_1['conf'].to_list() online_im_1 = vis.plot_tracking(img1, matches_res_1_np, online_ids, frame_id=frame, fps=-1, line_thickness_unmatched=4) cv2.imwrite(osp.join(save_root_1, f"{frame:06d}.jpg"), online_im_1) cv2.imwrite(osp.join(save_root_2, f"{frame:06d}.jpg"), img2) if len(res_1_boxes_np) == 0 and len(res_2_boxes_np) > 0: matches_res_2 = res_2_frame matches_res_2_np = np.array( matches_res_2[['left', 'top', 'width', 'height']]) if len(matches_res_2.shape) == 1: online_ids = [matches_res_2['id']] scores = [matches_res_2['conf']] else: online_ids = matches_res_2['id'].to_list() scores = matches_res_2['conf'].to_list() online_im_2 = vis.plot_tracking(img2, matches_res_2_np, online_ids, frame_id=frame, fps=-1, line_thickness_unmatched=4) cv2.imwrite(osp.join(save_root_1, f"{frame:06d}.jpg"), img1) cv2.imwrite(osp.join(save_root_2, f"{frame:06d}.jpg"), online_im_2) if len(res_1_boxes_np) == 0 and len(res_1_boxes_np) == 0: cv2.imwrite(osp.join(save_root_1, f"{frame:06d}.jpg"), img1) cv2.imwrite(osp.join(save_root_2, f"{frame:06d}.jpg"), img2) cmd_str = 'ffmpeg -framerate 8 -y -f image2 -i {}/%06d.jpg -vcodec libx264 -c:v copy {}'.format( save_root_1, osp.join(save_root_1, f'GNN_{seq}.avi')) os.system(cmd_str) cmd_str = 'ffmpeg -framerate 8 -y -f image2 -i {}/%06d.jpg -vcodec libx264 -c:v copy {}'.format( save_root_2, osp.join(save_root_2, f'noGNN_{seq}.avi')) os.system(cmd_str)