Skip to content

Commit

Permalink
Forgot to push the file I edited...
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpagnon committed Dec 9, 2024
1 parent 430856f commit ad2e55f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Sports2D/Utilities/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ def get_max_shape(list_of_arrays):
[item for sublist in list_of_arrays for item in sublist])
else:
# Determine the maximum shape across all list_of_arrays at this level
return [len(list_of_arrays)] + [max(arr.shape[i] for arr in list_of_arrays) for i in range(list_of_arrays[0].ndim)]
return [len(list_of_arrays)] + [max(arr.shape[i] for arr in list_of_arrays if arr.size > 0) for i in range(list_of_arrays[0].ndim)]

def pad_with_nans(list_of_arrays, target_shape):
'''
Recursively pad list_of_arrays with nans to match the target shape.
'''
if isinstance(list_of_arrays, np.ndarray):
if list_of_arrays.size == 0: # Handle empty arrays
# Return an array of nans with the correct target shape
return np.full(target_shape, np.nan, dtype=float)
# Pad the current array to the target shape
pad_width = [(0, max_dim - curr_dim) for curr_dim, max_dim in zip(list_of_arrays.shape, target_shape)]
return np.pad(list_of_arrays.astype(float), pad_width, constant_values=np.nan)
Expand Down
18 changes: 15 additions & 3 deletions Sports2D/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def setup_video(video_file_path, save_vid, vid_output_path):
if video_file_path.name == video_file_path.stem:
raise ValueError("Please set video_input to 'webcam' or to a video file (with extension) in Config.toml")
try:
cap = cv2.VideoCapture(video_file_path)
cap = cv2.VideoCapture(str(video_file_path.absolute()))
if not cap.isOpened():
raise
except:
Expand All @@ -210,12 +210,12 @@ def setup_video(video_file_path, save_vid, vid_output_path):
if save_vid:
# try:
# fourcc = cv2.VideoWriter_fourcc(*'avc1') # =h264. better compression and quality but may fail on some systems
# out_vid = cv2.VideoWriter(vid_output_path, fourcc, fps, (cam_width, cam_height))
# out_vid = cv2.VideoWriter(str(vid_output_path.absolute()), fourcc, fps, (cam_width, cam_height))
# if not out_vid.isOpened():
# raise ValueError("Failed to open video writer with 'avc1' (h264)")
# except Exception:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out_vid = cv2.VideoWriter(vid_output_path, fourcc, fps, (cam_width, cam_height))
out_vid = cv2.VideoWriter(str(vid_output_path.absolute()), fourcc, fps, (cam_width, cam_height))
# logging.info("Failed to open video writer with 'avc1' (h264). Using 'mp4v' instead.")

return cap, out_vid, cam_width, cam_height, fps
Expand Down Expand Up @@ -1195,6 +1195,15 @@ def best_coords_for_measurements(Q_coords, keypoints_names, fastest_frames_to_re
- Q_coords_low_speeds_low_angles: pd.DataFrame. The best coordinates for measurements
'''

# Add Hip column if not present
n_markers_init = len(keypoints_names)
if 'Hip' not in keypoints_names:
RHip_df = Q_coords.iloc[:,keypoints_names.index('RHip')*3:keypoints_names.index('RHip')*3+3]
LHip_df = Q_coords.iloc[:,keypoints_names.index('LHip')*3:keypoints_names.index('RHip')*3+3]
Hip_df = RHip_df.add(LHip_df, fill_value=0) /2
Hip_df.columns = [col+ str(int(Q_coords.columns[-1][1:])+1) for col in ['X','Y','Z']]
keypoints_names += ['Hip']
Q_coords = pd.concat([Q_coords, Hip_df], axis=1)
n_markers = len(keypoints_names)

# Using 80% slowest frames
Expand All @@ -1210,6 +1219,9 @@ def best_coords_for_measurements(Q_coords, keypoints_names, fastest_frames_to_re
if len(Q_coords_low_speeds_low_angles) < 50:
Q_coords_low_speeds_low_angles = Q_coords_low_speeds.iloc[pd.Series(ang_mean).nsmallest(50).index]

if n_markers_init < n_markers:
Q_coords_low_speeds_low_angles = Q_coords_low_speeds_low_angles.iloc[:,:-3]

return Q_coords_low_speeds_low_angles


Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = sports2d
version = 0.5.2
version = 0.5.3
author = David Pagnon
author_email = [email protected]
description = Detect pose and compute 2D joint angles from a video.
Expand Down

0 comments on commit ad2e55f

Please sign in to comment.