def conf_thresholding(data_dir, save_dir): # collect all trajectories tracker_id_score = dict() eval_dir = os.path.join(data_dir, 'data') seq_list, num_seq = load_list_from_folder(eval_dir) for seq_file in seq_list: seq_data, num_line = load_txt_file(seq_file) for data_line in seq_data: data_split = data_line.split(' ') score_tmp = float(data_split[-1]) id_tmp = int(data_split[1]) if id_tmp not in tracker_id_score.keys(): tracker_id_score[id_tmp] = list() tracker_id_score[id_tmp].append(score_tmp) # collect the ID to remove based on the confidence to_delete_id = list() for track_id, score_list in tracker_id_score.items(): average_score = sum(score_list) / float(len(score_list)) if average_score < score_threshold: to_delete_id.append(track_id) # remove the ID in the data folder save_dir_tmp = os.path.join(save_dir, 'data'); mkdir_if_missing(save_dir_tmp) for seq_file in seq_list: seq_name = fileparts(seq_file)[1] seq_file_save = os.path.join(save_dir_tmp, seq_name+'.txt'); seq_file_save = open(seq_file_save, 'w') seq_data, num_line = load_txt_file(seq_file) for data_line in seq_data: data_split = data_line.split(' ') id_tmp = int(data_split[1]) if id_tmp not in to_delete_id: seq_file_save.write(data_line + '\n') seq_file_save.close() # remove the ID in the trk with id folder trk_id_dir = os.path.join(data_dir, 'trk_withid') seq_dir_list, num_seq = load_list_from_folder(trk_id_dir) save_dir_tmp = os.path.join(save_dir, 'trk_withid') for seq_dir in seq_dir_list: frame_list, num_frame = load_list_from_folder(seq_dir) seq_name = fileparts(seq_dir)[1] save_frame_dir = os.path.join(save_dir_tmp, seq_name); mkdir_if_missing(save_frame_dir) for frame in frame_list: frame_index = fileparts(frame)[1] frame_file_save = os.path.join(save_frame_dir, frame_index+'.txt'); frame_file_save = open(frame_file_save, 'w') frame_data, num_line = load_txt_file(frame) for data_line in frame_data: data_split = data_line.split(' ') id_tmp = int(data_split[-1]) if id_tmp not in to_delete_id: frame_file_save.write(data_line + '\n') frame_file_save.close()
def vis(result_sha, data_root, result_root): def show_image_with_boxes(img, objects_res, object_gt, calib, save_path, height_threshold=0): img2 = np.copy(img) for obj in objects_res: box3d_pts_2d, _ = compute_box_3d(obj, calib.P) color_tmp = tuple([int(tmp * 255) for tmp in colors[obj.id % max_color]]) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=color_tmp) text = 'ID: %d' % obj.id if box3d_pts_2d is not None: img2 = cv2.putText(img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=color_tmp) img = Image.fromarray(img2) img = img.resize((width, height)) img.save(save_path) for seq in seq_list: image_dir = os.path.join(data_root, 'image_02/%s' % seq) calib_file = os.path.join(data_root, 'calib/%s.txt' % seq) result_dir = os.path.join(result_root, '%s/trk_withid/%s' % (result_sha, seq)) save_3d_bbox_dir = os.path.join(result_dir, '../../trk_image_vis/%s' % seq); mkdir_if_missing(save_3d_bbox_dir) # load the list images_list, num_images = load_list_from_folder(image_dir) print('number of images to visualize is %d' % num_images) start_count = 0 for count in range(start_count, num_images): image_tmp = images_list[count] if not is_path_exists(image_tmp): count += 1 continue image_index = int(fileparts(image_tmp)[1]) image_tmp = np.array(Image.open(image_tmp)) img_height, img_width, img_channel = image_tmp.shape result_tmp = os.path.join(result_dir, '%06d.txt'%image_index) # load the result if not is_path_exists(result_tmp): object_res = [] else: object_res = read_label(result_tmp) print('processing index: %d, %d/%d, results from %s' % (image_index, count+1, num_images, result_tmp)) calib_tmp = Calibration(calib_file) # load the calibration object_res_filtered = [] for object_tmp in object_res: if object_tmp.type not in type_whitelist: continue if hasattr(object_tmp, 'score'): if object_tmp.score < score_threshold: continue center = object_tmp.t object_res_filtered.append(object_tmp) num_instances = len(object_res_filtered) save_image_with_3dbbox_gt_path = os.path.join(save_3d_bbox_dir, '%06d.jpg' % (image_index)) show_image_with_boxes(image_tmp, object_res_filtered, [], calib_tmp, save_path=save_image_with_3dbbox_gt_path) print('number of objects to plot is %d' % (num_instances)) count += 1
print() if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python main.py data.bin vehicle") sys.exit(1) data_type = sys.argv[2] if data_type != 'vehicle' and data_type != 'pedestrian' and data_type != 'cyclist': print("Usage: python main.py data.bin vehicle") sys.exit(1) data_root = "./dataset" data_file_name = fileparts(sys.argv[1])[1] + fileparts(sys.argv[1])[2] result_root = './results' result_file_name = data_file_name[:-4] + "_preds.bin" data_file = os.path.join(data_root, data_file_name) mkdir_if_missing(result_root) dataset = metrics_pb2.Objects() with open(data_file, 'rb') as f: buf = f.read() dataset.ParseFromString(buf) contexts = [] if len(dataset.objects) != 0:
sys.exit(1) result_sha = sys.argv[1] save_root = '/home/eshan/AB3DMOT/results' det_id2str = {1: 'Pedestrian', 2: 'Car', 3: 'Cyclist'} seq_file_list, num_seq = load_list_from_folder( os.path.join('/home/eshan/AB3DMOT/data/KITTI', result_sha)) total_time = 0.0 total_frames = 0 save_dir = os.path.join(save_root, result_sha) mkdir_if_missing(save_dir) eval_dir = os.path.join(save_dir, 'data') mkdir_if_missing(eval_dir) for seq_file in seq_file_list: _, seq_name, _ = fileparts(seq_file) mot_tracker = AB3DMOT() seq_dets = np.loadtxt(seq_file, delimiter=',') # load detections eval_file = os.path.join(eval_dir, seq_name + '.txt') eval_file = open(eval_file, 'w') save_trk_dir = os.path.join(save_dir, 'trk_withid', seq_name) mkdir_if_missing(save_trk_dir) print("Processing %s." % (seq_name)) for frame in range(int(seq_dets[:, 0].min()), int(seq_dets[:, 0].max()) + 1): save_trk_file = os.path.join(save_trk_dir, '%06d.txt' % frame) save_trk_file = open(save_trk_file, 'w') save_det_file = os.path.join(save_trk_dir, 'det%06d.txt' % frame) save_det_file = open(save_det_file, 'w') save_raw_file = os.path.join(save_trk_dir, 'raw%06d.txt' % frame) save_raw_file = open(save_raw_file, 'w')
opt = parser.parse_args() if not os.path.exists(opt.save_dir): os.makedirs(opt.save_dir) test_data = os.path.join(opt.save_dir, "predicted_Img*.png") test_data = sorted(glob.glob(test_data)) n_img = len(test_data) outputs = [] gts = [] cals = [] for index in range(n_img): # load prediction current_pred_name = test_data[index] p, f, _ = utils.fileparts(current_pred_name) current_pred = cv2.imread(current_pred_name, cv2.IMREAD_UNCHANGED) # load ground truth current_gt_name = "Label" + f[13:] + ".png" #f: predicted_Img* current_gt_name = os.path.join(opt.data_dir, "Labels_FP_PRIME-FP20", current_gt_name) current_gt = cv2.imread(current_gt_name, cv2.IMREAD_GRAYSCALE) current_gt = current_gt[:, :].astype(np.float32) / 255 current_pred = current_pred[:, :].astype(np.float32) / 65535 current_gt_binary = current_gt > 0 current_pred_binary = current_pred > 0.5 # eval on every image y_true = current_gt.reshape(-1)
model.eval() test_idx = crs_val_data_lut[opt.pretrained_model_id] with torch.no_grad(): for image_idx in test_idx: data = test_data.get_image_from_cross_val_id(image_idx) image = data['i'].unsqueeze(0) mask = data['m'].unsqueeze(0) all_patches = image2patch.decompose(image) patch_loader = DataLoader(PatchData(all_patches), batch_size=opt.batch_size, shuffle=False) output = [] for ind_patch, patches in enumerate(patch_loader): patches = patches.to(device) output.append(torch.sigmoid(model(patches))) output = torch.cat(output) output = image2patch.compose(output.cpu()) * mask output = output.detach().numpy() save_name = test_data.get_filename_from_cross_val_id(image_idx)[0] _, save_name, _ = utils.fileparts(save_name) save_name_img = os.path.join(opt.save_dir, "predicted_" + save_name + ".png") image_np = 65535 * output.squeeze(0).squeeze(0) cv2.imwrite(save_name_img, image_np.astype(np.uint16))
def vis(result_sha, data_root, result_root): def show_image_with_boxes(img, velo, objects_res, objects_res_det, objects_res_raw, labeldata, object_gt, calib, save_path, height_threshold=0, show_lidar=True, save_image=False): img2 = np.copy(img) for obj in objects_res: box3d_pts_2d, _ = compute_box_3d(obj, calib.P) color_tmp = tuple( [int(tmp * 255) for tmp in colors[obj.id % max_color]]) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(0, 0, 255)) text = 'Tracked ID: %d, Type: %s' % (obj.id, obj.type) if box3d_pts_2d is not None: img2 = cv2.putText( img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=(0, 0, 255)) for obj in objects_res_det: box3d_pts_2d, _ = compute_box_3d(obj, calib.P) color_tmp = tuple( [int(tmp * 255) for tmp in colors[obj.id % max_color]]) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(0, 255, 0)) text = 'Detection ID: %d, Type: %s' % (obj.id, obj.type) if box3d_pts_2d is not None: img2 = cv2.putText( img2, text, (int(box3d_pts_2d[3, 0]), int(box3d_pts_2d[3, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=(0, 255, 0)) import itertools labeldata, labeldata2 = itertools.tee(labeldata) for obj in labeldata: # print("here") box3d_pts_2d, _ = compute_box_3d(obj, calib.P) img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(255, 0, 0)) text = 'GT, Type: %s' % (obj.type) if box3d_pts_2d is not None: # print("also") print(text) img2 = cv2.putText( img2, text, (int(box3d_pts_2d[4, 0]), int(box3d_pts_2d[4, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=(255, 0, 0)) # for obj in objects_res_raw: # box3d_pts_2d, _ = compute_box_3d(obj, calib.P) # color_tmp = tuple([int(tmp * 255) # for tmp in colors[obj.id % max_color]]) # img2 = draw_projected_box3d(img2, box3d_pts_2d, color=(255,0,0)) # text = 'Estimate ID: %d' % obj.id # if box3d_pts_2d is not None: # img2 = cv2.putText(img2, text, (int(box3d_pts_2d[2, 0]), int( # box3d_pts_2d[2, 1]) - 8), cv2.FONT_HERSHEY_TRIPLEX, 0.5, color=(255,0,0)) if show_lidar: show_lidar_with_boxes(velo, objects=labeldata2, calib=calib, objects_pred=objects_res) img = Image.fromarray(img2) img = img.resize((width, height)) cv2.imshow("Image", img2) cv2.waitKey() if save_image: print("Saving Image at", save_path) img.save(save_path) return img2 for seq in seq_list: image_dir = os.path.join(data_root, 'image_02/%s' % seq) calib_file = os.path.join(data_root, 'calib/%s.txt' % seq) label_file = os.path.join(data_root, 'label_02/%s.txt' % seq) velo_dir = os.path.join(data_root, 'velodyne/%s' % seq) result_dir = [ os.path.join(result_root, '%s/trk_withid/%s' % (result_sha[0], seq)), os.path.join(result_root, '%s/trk_withid/%s' % (result_sha[1], seq)), os.path.join(result_root, '%s/trk_withid/%s' % (result_sha[2], seq)) ] save_3d_bbox_dir = os.path.join( result_root, '%s/trk_image_vis/%s' % ("Combined_Final_WithLabel", seq)) mkdir_if_missing(save_3d_bbox_dir) # load the list images_list, num_images = load_list_from_folder(image_dir) velo_list, num_velo = load_list_from_folder(velo_dir) print('number of images to visualize is %d' % num_images) start_count = 0 filecontent = np.array([f.split() for f in open(label_file, 'r')]) # alllabels = np.unique(filecontent[:,2]) # labels = ['Car', 'Pedestrian', 'Cyclist'] # finallabelset = [x for x in alllabels if x not in labels] # print(alllabels) # print(finallabelset) # for val in finallabelset: # filecontent = filecontent[filecontent[:,2]!=val,:] # print(np.unique(filecontent[:,2])) size = (width, height) out = cv2.VideoWriter(f'{result_root}/{seq}.avi', cv2.VideoWriter_fourcc(*'DIVX'), 15, size) num_images = 1 for count in range(start_count, num_images): image_tmp = images_list[count] velo_tmp = velo_list[count] if not is_path_exists(image_tmp): count += 1 continue image_index = int(fileparts(image_tmp)[1]) image_tmp = np.array(Image.open(image_tmp)) img_height, img_width, img_channel = image_tmp.shape filecontentframe = filecontent[filecontent[:, 0] == str(image_index), :] print(len(filecontentframe)) print(f"Labels for frame {image_index}", np.unique(filecontentframe[:, 2])) labeldata = (Object3d(getstringfromarray(line[2:])) for line in filecontentframe) object_res = [] object_res_det = [] object_res_raw = [] for dirt in result_dir: result_tmp = os.path.join(dirt, '%06d.txt' % image_index) # load the result if is_path_exists(result_tmp): object_res = object_res + read_label(result_tmp) result_tmp_det = os.path.join(dirt, 'det%06d.txt' % image_index) # load the result if is_path_exists(result_tmp_det): object_res_det = object_res_det + \ read_label(result_tmp_det) result_tmp_raw = os.path.join(dirt, 'raw%06d.txt' % image_index) # load the result if is_path_exists(result_tmp_raw): object_res_raw = object_res_raw + \ read_label(result_tmp_raw) print('processing index: %d, %d/%d, results from %s' % (image_index, count + 1, num_images, result_tmp)) calib_tmp = Calibration(calib_file) # load the calibration object_res_filtered = [] for object_tmp in object_res: if object_tmp.type not in type_whitelist: continue if hasattr(object_tmp, 'score'): if object_tmp.score < score_threshold: continue center = object_tmp.t object_res_filtered.append(object_tmp) object_res_filtered_det = [] for object_tmp in object_res_det: if object_tmp.type not in type_whitelist: continue if hasattr(object_tmp, 'score'): if object_tmp.score < score_threshold: continue center = object_tmp.t object_res_filtered_det.append(object_tmp) object_res_filtered_raw = [] for object_tmp in object_res_raw: if object_tmp.type not in type_whitelist: continue # if hasattr(object_tmp, 'score'): # if object_tmp.score < score_threshold: # continue center = object_tmp.t object_res_filtered_raw.append(object_tmp) num_instances = len(object_res_filtered) save_image_with_3dbbox_gt_path = os.path.join( save_3d_bbox_dir, '%06d.jpg' % (image_index)) velodyne_scan = load_velo_scan(velo_tmp, np.float32, n_vec=4)[:, 0:4] img = show_image_with_boxes( image_tmp, velodyne_scan, object_res_filtered, object_res_filtered_det, object_res_filtered_raw, labeldata, [], calib_tmp, save_path=save_image_with_3dbbox_gt_path) print('number of objects to plot is %d, %d, %d' % (num_instances, len(object_res_filtered_det), len(object_res_filtered_raw))) count += 1 out.write(img) out.release()
def vis(data_root, result_root): with open(os.path.join( '/home/ld/RepPoints/final/epoch13 thres0.3/offset_tracking_result.json' ), 'r', encoding='utf-8') as f: data = json.load(f) video_names = [] for data_info_tmp in data: if data_info_tmp['video_id'] not in video_names: video_names.append(data_info_tmp['video_id']) # num_images = len(data) # 21 videos for this_video_name in video_names: this_video = [] for tmp_frame in data: if tmp_frame['video_id'] == this_video_name: this_video.append(tmp_frame) num_images = len(this_video) print('number of images to visualize is %d' % num_images) start_count = 0 for count in range(start_count, num_images): this_img_info = this_video[count] image_path_tmp = data_root + '/' + this_img_info['filename'] image_index = int(fileparts(image_path_tmp)[1]) image_tmp = np.array(Image.open(image_path_tmp)) img_height, img_width, img_channel = image_tmp.shape vis_dir = os.path.join(result_root, 'vis/%s' % this_video_name) save_3d_bbox_dir = os.path.join(vis_dir) mkdir_if_missing(vis_dir) # result_tmp = os.path.join(result_dir) # load the result # if not is_path_exists(result_tmp): object_res = [] # else: object_res = read_label(result_tmp) # print('processing index: %d, %d/%d, results from %s' % (image_index, count+1, num_images, result_tmp)) appear = 0 ID_pool = [] object_res_filtered = [] object_special_former = [] for tmp_index, labels_index in enumerate( this_img_info['ann']['track_id']): # if labels_index not in type_whitelist: continue # if hasattr(this_img_info, 'score'): # if this_img_info.score < score_threshold: continue ID_pool.append(this_img_info['ann']['track_id'][tmp_index]) if image_index != 0: if this_img_info['ann']['track_id'][ tmp_index] not in ID_pool_former: appear = 1 buf = { 'box2d': this_img_info['ann']['bboxes'][tmp_index], 'track_id': this_img_info['ann']['track_id'][tmp_index], 'appear': appear } appear = 0 object_res_filtered.append(buf) if image_index != 0: for obj_tmp in object_res_filtered_former: if obj_tmp['track_id'] not in ID_pool: object_special_former.append(obj_tmp) num_instances = len(object_res_filtered) save_image_with_3dbbox_gt_path = os.path.join( save_3d_bbox_dir, '%06d.jpg' % (image_index)) show_image_with_boxes(image_tmp, object_res_filtered, object_special_former, [], [], save_image_with_3dbbox_gt_path, img_height, img_width) object_res_filtered_former = object_res_filtered.copy() ID_pool_former = ID_pool.copy() print('number of objects to plot is %d' % (num_instances)) count += 1