diff --git a/monoloco/activity.py b/monoloco/activity.py index 0065936..5ba7676 100644 --- a/monoloco/activity.py +++ b/monoloco/activity.py @@ -22,9 +22,11 @@ def social_interactions(idx, centers, angles, dds, stds=None, social_distance=Fa # A) Check whether people are close together xx = centers[idx][0] zz = centers[idx][1] - distances = [math.sqrt((xx - centers[i][0]) ** 2 + (zz - centers[i][1]) ** 2) for i, _ in enumerate(centers)] + distances = [math.sqrt((xx - centers[i][0]) ** 2 + (zz - centers[i][1]) ** 2) + for i, _ in enumerate(centers)] sorted_idxs = np.argsort(distances) - indices = [idx_t for idx_t in sorted_idxs[1:] if distances[idx_t] <= threshold_dist] + indices = [idx_t for idx_t in sorted_idxs[1:] + if distances[idx_t] <= threshold_dist] # B) Check whether people are looking inwards and whether there are no intrusions # Deterministic @@ -72,17 +74,14 @@ def is_raising_hand(keypoint): l_hand = 9 r_shoulder = 6 r_hand = 10 - l_ear = 3 - r_ear = 4 - h_offset = 20 - if keypoint[1][l_hand] < keypoint[1][l_shoulder] and keypoint[1][r_hand] < keypoint[1][r_shoulder]: + if keypoint[1][l_hand] < keypoint[1][l_shoulder] and keypoint[1][r_hand] < keypoint[1][r_shoulder]: return 'both' - if keypoint[1][l_hand] < keypoint[1][l_shoulder]: + if keypoint[1][l_hand] < keypoint[1][l_shoulder]: return 'left' - if keypoint[1][r_hand] < keypoint[1][r_shoulder]: + if keypoint[1][r_hand] < keypoint[1][r_shoulder]: return 'right' return 'none' @@ -96,7 +95,8 @@ def check_f_formations(idx, idx_t, centers, angles, radii, social_distance=False """ # Extract centers and angles - other_centers = np.array([cent for l, cent in enumerate(centers) if l not in (idx, idx_t)]) + other_centers = np.array( + [cent for l, cent in enumerate(centers) if l not in (idx, idx_t)]) theta0 = angles[idx] theta1 = angles[idx_t] @@ -115,15 +115,18 @@ def check_f_formations(idx, idx_t, centers, angles, radii, social_distance=False # 1) Verify they are looking inwards. # The distance between mus and the center should be less wrt the original position and the center - d_new = np.linalg.norm(mu_0 - mu_1) / 2 if social_distance else np.linalg.norm(mu_0 - mu_1) + d_new = np.linalg.norm( + mu_0 - mu_1) / 2 if social_distance else np.linalg.norm(mu_0 - mu_1) d_0 = np.linalg.norm(x_0 - o_c) d_1 = np.linalg.norm(x_1 - o_c) # 2) Verify no intrusion for third parties if other_centers.size: - other_distances = np.linalg.norm(other_centers - o_c.reshape(1, -1), axis=1) + other_distances = np.linalg.norm( + other_centers - o_c.reshape(1, -1), axis=1) else: - other_distances = 100 * np.ones((1, 1)) # Condition verified if no other people + # Condition verified if no other people + other_distances = 100 * np.ones((1, 1)) # Binary Classification # if np.min(other_distances) > radius: # Ablation without orientation @@ -163,8 +166,10 @@ def show_activities(args, image_t, output_path, annotations, dic_out): show=args.show, fig_width=10, dpi_factor=1.0) as ax: - keypoint_painter.keypoints(ax, keypoint_sets, colors=colors, raise_hand=r_h) - draw_orientation(ax, uv_centers, sizes, angles, colors, mode='front') + keypoint_painter.keypoints( + ax, keypoint_sets, colors=colors, raise_hand=r_h) + draw_orientation(ax, uv_centers, sizes, + angles, colors, mode='front') if 'bird' in args.output_types: z_max = min(args.z_max, 4 + max([el[1] for el in xz_centers])) diff --git a/monoloco/predict.py b/monoloco/predict.py index 38de93b..b181df6 100644 --- a/monoloco/predict.py +++ b/monoloco/predict.py @@ -46,7 +46,8 @@ def get_torch_checkpoints_dir(): def download_checkpoints(args): torch_dir = get_torch_checkpoints_dir() - pifpaf_model = os.path.join(torch_dir, 'shufflenetv2k30-201104-224654-cocokp-d75ed641.pkl') + pifpaf_model = os.path.join( + torch_dir, 'shufflenetv2k30-201104-224654-cocokp-d75ed641.pkl') dic_models = {'keypoints': pifpaf_model} if not os.path.exists(pifpaf_model): import gdown @@ -154,14 +155,16 @@ def predict(args): # data data = datasets.ImageList(args.images, preprocess=preprocess) if args.mode == 'stereo': - assert len(data.image_paths) % 2 == 0, "Odd number of images in a stereo setting" + assert len( + data.image_paths) % 2 == 0, "Odd number of images in a stereo setting" data_loader = torch.utils.data.DataLoader( data, batch_size=args.batch_size, shuffle=False, pin_memory=False, collate_fn=datasets.collate_images_anns_meta) for batch_i, (image_tensors_batch, _, meta_batch) in enumerate(data_loader): - pred_batch = processor.batch(pifpaf_model, image_tensors_batch, device=args.device) + pred_batch = processor.batch( + pifpaf_model, image_tensors_batch, device=args.device) # unbatch (only for MonStereo) for idx, (pred, meta) in enumerate(zip(pred_batch, meta_batch)): @@ -183,7 +186,8 @@ def predict(args): output_path = os.path.join(splits[0], 'out_' + splits[1]) else: file_name = os.path.basename(meta['file_name']) - output_path = os.path.join(args.output_directory, 'out_' + file_name) + output_path = os.path.join( + args.output_directory, 'out_' + file_name) im_name = os.path.basename(meta['file_name']) print(f'{batch_i} image {im_name} saved as {output_path}') @@ -195,15 +199,18 @@ def predict(args): # 3D Predictions if args.mode != 'keypoints': im_size = (cpu_image.size[0], cpu_image.size[1]) # Original - kk, dic_gt = factory_for_gt(im_size, focal_length=args.focal, name=im_name, path_gt=args.path_gt) + kk, dic_gt = factory_for_gt( + im_size, focal_length=args.focal, name=im_name, path_gt=args.path_gt) # Preprocess pifpaf outputs and run monoloco - boxes, keypoints = preprocess_pifpaf(pifpaf_outs['left'], im_size, enlarge_boxes=False) + boxes, keypoints = preprocess_pifpaf( + pifpaf_outs['left'], im_size, enlarge_boxes=False) if args.mode == 'mono': LOG.info("Prediction with MonoLoco++") dic_out = net.forward(keypoints, kk) - dic_out = net.post_process(dic_out, boxes, keypoints, kk, dic_gt) + dic_out = net.post_process( + dic_out, boxes, keypoints, kk, dic_gt) if args.social_distance or (args.activities and 'social_distance' in args.activities): dic_out = net.social_distance(dic_out, args) if args.activities and 'raise_hand' in args.activities: @@ -211,9 +218,11 @@ def predict(args): else: LOG.info("Prediction with MonStereo") - boxes_r, keypoints_r = preprocess_pifpaf(pifpaf_outs['right'], im_size) + boxes_r, keypoints_r = preprocess_pifpaf( + pifpaf_outs['right'], im_size) dic_out = net.forward(keypoints, kk, keypoints_r=keypoints_r) - dic_out = net.post_process(dic_out, boxes, keypoints, kk, dic_gt) + dic_out = net.post_process( + dic_out, boxes, keypoints, kk, dic_gt) else: dic_out = defaultdict(list) @@ -242,7 +251,8 @@ def factory_outputs(args, pifpaf_outs, dic_out, output_path, kk=None): elif any((xx in args.output_types for xx in ['front', 'bird', 'multi'])): LOG.info(output_path) if args.activities: - show_activities(args, pifpaf_outs['image'], output_path, pifpaf_outs['left'], dic_out) + show_activities( + args, pifpaf_outs['image'], output_path, pifpaf_outs['left'], dic_out) else: printer = Printer(pifpaf_outs['image'], output_path, kk, args) figures, axes = printer.factory_axes(dic_out) @@ -253,4 +263,5 @@ def factory_outputs(args, pifpaf_outs, dic_out, output_path, kk=None): json.dump(dic_out, ff) else: - LOG.info("No output saved, please select one among front, bird, multi, or pifpaf options") + LOG.info( + "No output saved, please select one among front, bird, multi, or pifpaf options")