From 0008c7ebcb3e48f603a90945597ed04f4c2b9968 Mon Sep 17 00:00:00 2001 From: davidpagnon Date: Sun, 24 Mar 2024 22:38:30 +0100 Subject: [PATCH] small fixes --- Sports2D/Utilities/skeletons.py | 12 ++++++-- Sports2D/compute_angles.py | 7 +++-- Sports2D/detect_pose.py | 52 +++++++++++++++++++++------------ setup.cfg | 4 +-- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/Sports2D/Utilities/skeletons.py b/Sports2D/Utilities/skeletons.py index 325697e..104e145 100644 --- a/Sports2D/Utilities/skeletons.py +++ b/Sports2D/Utilities/skeletons.py @@ -217,9 +217,17 @@ ]), ]), ]), - Node("Nose", id=0, children=[ - Node("REye", id=5), + Node("Nose", id=0, children=[ + Node("LEyeInner", id=1), Node("LEye", id=2), + Node("LEyeOuter", id=3), + Node("REyeInner", id=4), + Node("REye", id=5), + Node("REyeOuter", id=6), + Node("LEar", id=7), + Node("REar", id=8), + Node("LMouth", id=9), + Node("RMouth", id=10), ]), Node("RShoulder", id=12, children=[ Node("RElbow", id=14, children=[ diff --git a/Sports2D/compute_angles.py b/Sports2D/compute_angles.py index 3f216a1..f92408b 100644 --- a/Sports2D/compute_angles.py +++ b/Sports2D/compute_angles.py @@ -249,7 +249,7 @@ def segment_angles_series_from_points(df_points, angle_params, segment): ang_series = points2D_to_angles(points_list) ang_series += angle_params[2] ang_series *= angle_params[3] - ang_series = np.where(ang_series>180,ang_series-360,ang_series) + # ang_series = np.where(ang_series>180,ang_series-360,ang_series) # For trunk: mean between angles RHip-RShoulder and LHip-LShoulder if segment == 'Trunk': @@ -264,7 +264,10 @@ def segment_angles_series_from_points(df_points, angle_params, segment): ang_series += angle_params[2] ang_series *= angle_params[3] ang_series = np.mean((ang_seriesR, ang_series), axis=0) - ang_series = np.where(ang_series>180,ang_series-360,ang_series) + # ang_series = np.where(ang_series>180,ang_series-360,ang_series) + + if ang_series.mean() > 180: ang_series -= 360 + if ang_series.mean() < -180: ang_series += 360 return ang_series diff --git a/Sports2D/detect_pose.py b/Sports2D/detect_pose.py index 29abee9..27f4704 100644 --- a/Sports2D/detect_pose.py +++ b/Sports2D/detect_pose.py @@ -213,7 +213,10 @@ def min_with_single_indices(L, T): - T_minL: list of tuples associated with smallest values of L ''' - minL = [np.min(L)] + try: + minL = [np.min(L)] + except: + return [], [], [] argminL = [np.argmin(L)] T_minL = [T[argminL[0]]] @@ -492,9 +495,13 @@ def save_imgvid_reID(video_path, video_result_path, save_vid=1, save_img=1, *pos # Open csv files coords = [] - for c in csv_paths: - with open(c) as c_f: - coords += [pd.read_csv(c_f, header=[0,1,2,3])] + try: + for c in csv_paths: + with open(c) as c_f: + coords += [pd.read_csv(c_f, header=[0,1,2,3])] + except: + logging.warning('No csv files found.') + return # Open video frame by frame cap = cv2.VideoCapture(str(video_path)) @@ -535,7 +542,8 @@ def save_imgvid_reID(video_path, video_result_path, save_vid=1, save_img=1, *pos break f += 1 cap.release() - writer.release() + if save_vid: + writer.release() def detect_pose_fun(config_dict, video_file): @@ -625,21 +633,29 @@ def detect_pose_fun(config_dict, video_file): elif platform == "linux" or platform=="linux2": run_openpose_linux(video_path, json_path, pose_model) os.chdir(root_dir) - + + + elif pose_algo == 'BLAZEPOSE': + pose_model = pose_algo + json_path = result_dir / '_'.join((video_file_stem,pose_algo,'json')) + model_complexity = config_dict.get('pose').get('BLAZEPOSE').get('model_complexity') + Blazepose_runsave.blazepose_detec_func(input_file=video_path, save_images=False, to_json=True, save_video=False, to_csv=False, output_folder=result_dir, model_complexity=model_complexity) + # Sort people and save to csv, optionally display plot + try: json_to_csv(json_path, frame_rate, pose_model, interp_gap_smaller_than, filter_options, show_plots) + except: + logging.warning('No person detected or persons could not be associated across frames.') + return # Save images and files after reindentification - if save_img and save_vid: - logging.info(f'Saving images and video in {result_dir}.') - if save_img and not save_vid: - logging.info(f'Saving images in {result_dir}.') - if not save_img and save_vid: - logging.info(f'Saving video in {result_dir}.') - if save_vid or save_img: - save_imgvid_reID(video_path, video_result_path, save_vid, save_img, pose_model) + if save_img and save_vid: + logging.info(f'Saving images and video in {result_dir}.') + if save_img and not save_vid: + logging.info(f'Saving images in {result_dir}.') + if not save_img and save_vid: + logging.info(f'Saving video in {result_dir}.') + if save_vid or save_img: + save_imgvid_reID(video_path, video_result_path, save_vid, save_img, pose_model) - - elif pose_algo == 'BLAZEPOSE': - model_complexity = config_dict.get('pose').get('BLAZEPOSE').get('model_complexity') - Blazepose_runsave.blazepose_detec_func(input_file=video_path, save_images=save_img, to_json=True, save_video=save_vid, to_csv=True, output_folder=result_dir, model_complexity=model_complexity) + diff --git a/setup.cfg b/setup.cfg index 3168cb6..ddc953c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = sports2d -version = 0.3.3 +version = 0.3.4 author = David Pagnon author_email = contact@david-pagnon.com description = Detect pose and compute 2D joint angles from a video. @@ -38,7 +38,7 @@ install_requires = scipy anytree opencv-python - mediapipe==0.10.0 + mediapipe matplotlib PyQt5 statsmodels