Upload folder using huggingface_hub
Browse files- .gitattributes +1 -0
- README.md +2 -2
- example/Sprint_Assessment_Workflow.ipynb +3 -0
- example/annotate_video.py +59 -0
- example/inference_yolo_pose.py +77 -0
- example/post_process_keypoints.py +78 -0
- example/run_full_pipeline.py +47 -0
- example/smas_pipeline_v2.py +600 -0
- example/train_yolo_pose.py +27 -0
- example/video_utils.py +48 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
example/Sprint_Assessment_Workflow.ipynb filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -91,6 +91,6 @@ results.save("output_video.mp4")
|
|
| 91 |
|
| 92 |
## License
|
| 93 |
|
| 94 |
-
```
|
| 95 |
-
This project is licensed under the
|
| 96 |
```
|
|
|
|
| 91 |
|
| 92 |
## License
|
| 93 |
|
| 94 |
+
```mit
|
| 95 |
+
This project is licensed under the MIT License.
|
| 96 |
```
|
example/Sprint_Assessment_Workflow.ipynb
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eb6f86383c9517d10213b276f73981f0c5f83634361ba775c826b114ae29673d
|
| 3 |
+
size 22298843
|
example/annotate_video.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import argparse
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Skeleton definition
|
| 8 |
+
SKELETON = [
|
| 9 |
+
("left_ankle", "left_knee"), ("left_knee", "left_hip"),
|
| 10 |
+
("right_ankle", "right_knee"), ("right_knee", "right_hip"),
|
| 11 |
+
("left_hip", "right_hip"), ("left_shoulder", "right_shoulder"),
|
| 12 |
+
("left_shoulder", "left_hip"), ("right_shoulder", "right_hip"),
|
| 13 |
+
("left_shoulder", "left_elbow"), ("left_elbow", "left_wrist"),
|
| 14 |
+
("right_shoulder", "right_elbow"), ("right_elbow", "right_wrist"),
|
| 15 |
+
("left_ankle", "left_heel"), ("left_ankle", "left_big_toe"),
|
| 16 |
+
("right_ankle", "right_heel"), ("right_ankle", "right_big_toe")
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
def annotate_frames(frames_dir, output_dir, csv_path):
|
| 20 |
+
"""Draw keypoints and skeleton on frames."""
|
| 21 |
+
df = pd.read_csv(csv_path)
|
| 22 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 23 |
+
|
| 24 |
+
frame_files = sorted([f for f in os.listdir(frames_dir) if f.endswith('.jpg')])
|
| 25 |
+
|
| 26 |
+
for _, row in df.iterrows():
|
| 27 |
+
f_idx = int(row['frame'])
|
| 28 |
+
if f_idx >= len(frame_files): continue
|
| 29 |
+
|
| 30 |
+
frame_name = frame_files[f_idx]
|
| 31 |
+
img = cv2.imread(os.path.join(frames_dir, frame_name))
|
| 32 |
+
|
| 33 |
+
# Draw Skeleton
|
| 34 |
+
for start_kp, end_kp in SKELETON:
|
| 35 |
+
s_x, s_y = row.get(f"{start_kp}_x"), row.get(f"{start_kp}_y")
|
| 36 |
+
e_x, e_y = row.get(f"{end_kp}_x"), row.get(f"{end_kp}_y")
|
| 37 |
+
if pd.notna(s_x) and pd.notna(e_x):
|
| 38 |
+
cv2.line(img, (int(s_x), int(s_y)), (int(e_x), int(e_y)), (0, 255, 0), 2)
|
| 39 |
+
|
| 40 |
+
# Draw Keypoints
|
| 41 |
+
for col in df.columns:
|
| 42 |
+
if col.endswith('_x'):
|
| 43 |
+
kp_name = col[:-2]
|
| 44 |
+
x, y = row[col], row[f"{kp_name}_y"]
|
| 45 |
+
if pd.notna(x):
|
| 46 |
+
cv2.circle(img, (int(x), int(y)), 4, (0, 0, 255), -1)
|
| 47 |
+
|
| 48 |
+
cv2.imwrite(os.path.join(output_dir, frame_name), img)
|
| 49 |
+
if f_idx % 100 == 0:
|
| 50 |
+
print(f"Annotated {f_idx} frames")
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
parser = argparse.ArgumentParser()
|
| 54 |
+
parser.add_argument("--frames", required=True, help="Input frames directory")
|
| 55 |
+
parser.add_argument("--csv", required=True, help="Input keypoints CSV")
|
| 56 |
+
parser.add_argument("--output", required=True, help="Output frames directory")
|
| 57 |
+
args = parser.parse_args()
|
| 58 |
+
|
| 59 |
+
annotate_frames(args.frames, args.output, args.csv)
|
example/inference_yolo_pose.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import argparse
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import cv2
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Keypoint schema (17 body + 6 feet = 23 total)
|
| 8 |
+
COCO_BODY_17 = [
|
| 9 |
+
"nose", "left_eye", "right_eye", "left_ear", "right_ear",
|
| 10 |
+
"left_shoulder", "right_shoulder", "left_elbow", "right_elbow",
|
| 11 |
+
"left_wrist", "right_wrist", "left_hip", "right_hip",
|
| 12 |
+
"left_knee", "right_knee", "left_ankle", "right_ankle"
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
FEET_6_LABELS = [
|
| 16 |
+
"left_heel", "left_big_toe", "left_little_toe",
|
| 17 |
+
"right_heel", "right_big_toe", "right_little_toe"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
ALL_KEYPOINTS = COCO_BODY_17 + FEET_6_LABELS
|
| 21 |
+
|
| 22 |
+
def run_inference(frames_dir, labels_dir, model_path, img_width=1920, img_height=1080):
|
| 23 |
+
"""Run YOLO pose inference on all frames and save labels."""
|
| 24 |
+
model = YOLO(model_path)
|
| 25 |
+
os.makedirs(labels_dir, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
frame_files = sorted([f for f in os.listdir(frames_dir) if f.endswith('.jpg')])
|
| 28 |
+
print(f"Running inference on {len(frame_files)} frames...")
|
| 29 |
+
|
| 30 |
+
for idx, frame_file in enumerate(frame_files):
|
| 31 |
+
frame_path = os.path.join(frames_dir, frame_file)
|
| 32 |
+
results = model(frame_path, verbose=False)
|
| 33 |
+
|
| 34 |
+
label_file = os.path.join(labels_dir, frame_file.replace('.jpg', '.txt'))
|
| 35 |
+
|
| 36 |
+
with open(label_file, 'w') as f:
|
| 37 |
+
for result in results:
|
| 38 |
+
if result.keypoints is not None:
|
| 39 |
+
for kp in result.keypoints.data:
|
| 40 |
+
kp_np = kp.cpu().numpy()
|
| 41 |
+
|
| 42 |
+
# Get bounding box (approximate from keypoints)
|
| 43 |
+
valid_kp = kp_np[kp_np[:, 2] > 0]
|
| 44 |
+
if len(valid_kp) == 0:
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
x_min, y_min = valid_kp[:, 0].min(), valid_kp[:, 1].min()
|
| 48 |
+
x_max, y_max = valid_kp[:, 0].max(), valid_kp[:, 1].max()
|
| 49 |
+
bbox_x = (x_min + x_max) / 2 / img_width
|
| 50 |
+
bbox_y = (y_min + y_max) / 2 / img_height
|
| 51 |
+
bbox_w = (x_max - x_min) / img_width
|
| 52 |
+
bbox_h = (y_max - y_min) / img_height
|
| 53 |
+
|
| 54 |
+
# Write YOLO format: class_id bbox keypoints
|
| 55 |
+
line = f"0 {bbox_x:.6f} {bbox_y:.6f} {bbox_w:.6f} {bbox_h:.6f}"
|
| 56 |
+
for kp_point in kp_np:
|
| 57 |
+
x_norm = kp_point[0] / img_width
|
| 58 |
+
y_norm = kp_point[1] / img_height
|
| 59 |
+
conf = kp_point[2]
|
| 60 |
+
line += f" {x_norm:.6f} {y_norm:.6f} {conf:.2f}"
|
| 61 |
+
f.write(line + "\n")
|
| 62 |
+
|
| 63 |
+
if (idx + 1) % 50 == 0:
|
| 64 |
+
print(f"Processed {idx + 1}/{len(frame_files)} frames")
|
| 65 |
+
|
| 66 |
+
print(f"✅ Inference complete. Labels saved to {labels_dir}")
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
parser = argparse.ArgumentParser()
|
| 70 |
+
parser.add_argument("--frames", required=True, help="Directory containing JPEG frames")
|
| 71 |
+
parser.add_argument("--labels", required=True, help="Output directory for labels")
|
| 72 |
+
parser.add_argument("--model", required=True, help="Path to YOLO weights (.pt)")
|
| 73 |
+
parser.add_argument("--width", type=int, default=1920)
|
| 74 |
+
parser.add_argument("--height", type=int, default=1080)
|
| 75 |
+
args = parser.parse_args()
|
| 76 |
+
|
| 77 |
+
run_inference(args.frames, args.labels, args.model, args.width, args.height)
|
example/post_process_keypoints.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import argparse
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from scipy.ndimage import gaussian_filter1d
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# Keypoint schema
|
| 9 |
+
COCO_BODY_17 = [
|
| 10 |
+
"nose", "left_eye", "right_eye", "left_ear", "right_ear",
|
| 11 |
+
"left_shoulder", "right_shoulder", "left_elbow", "right_elbow",
|
| 12 |
+
"left_wrist", "right_wrist", "left_hip", "right_hip",
|
| 13 |
+
"left_knee", "right_knee", "left_ankle", "right_ankle"
|
| 14 |
+
]
|
| 15 |
+
FEET_6_LABELS = [
|
| 16 |
+
"left_heel", "left_big_toe", "left_little_toe",
|
| 17 |
+
"right_heel", "right_big_toe", "right_little_toe"
|
| 18 |
+
]
|
| 19 |
+
ALL_KEYPOINTS = COCO_BODY_17 + FEET_6_LABELS
|
| 20 |
+
|
| 21 |
+
def parse_labels_to_df(labels_dir, img_width, img_height):
|
| 22 |
+
records = []
|
| 23 |
+
label_files = sorted([f for f in os.listdir(labels_dir) if f.endswith('.txt')])
|
| 24 |
+
for frame_idx, label_file in enumerate(label_files):
|
| 25 |
+
label_path = os.path.join(labels_dir, label_file)
|
| 26 |
+
with open(label_path, 'r') as f:
|
| 27 |
+
lines = f.readlines()
|
| 28 |
+
for line in lines:
|
| 29 |
+
parts = line.strip().split()
|
| 30 |
+
if len(parts) < 5: continue
|
| 31 |
+
keypoint_data = parts[5:]
|
| 32 |
+
kp_dict = {}
|
| 33 |
+
for i in range(0, len(keypoint_data), 3):
|
| 34 |
+
kp_idx = i // 3
|
| 35 |
+
if kp_idx >= len(ALL_KEYPOINTS): break
|
| 36 |
+
x_norm, y_norm, conf = float(keypoint_data[i]), float(keypoint_data[i+1]), float(keypoint_data[i+2])
|
| 37 |
+
if conf > 0:
|
| 38 |
+
kp_dict[ALL_KEYPOINTS[kp_idx]] = {'x': x_norm * img_width, 'y': y_norm * img_height, 'conf': conf}
|
| 39 |
+
records.append({'frame': frame_idx, 'keypoints': kp_dict})
|
| 40 |
+
return pd.DataFrame(records)
|
| 41 |
+
|
| 42 |
+
def process_keypoints(df, sigma=2.0):
|
| 43 |
+
"""Expand, interpolate, and smooth keypoints."""
|
| 44 |
+
# Expand dictionary into columns
|
| 45 |
+
rows = []
|
| 46 |
+
for _, row in df.iterrows():
|
| 47 |
+
flat = {'frame': row['frame']}
|
| 48 |
+
for kp, vals in row['keypoints'].items():
|
| 49 |
+
flat[f"{kp}_x"] = vals['x']
|
| 50 |
+
flat[f"{kp}_y"] = vals['y']
|
| 51 |
+
flat[f"{kp}_conf"] = vals['conf']
|
| 52 |
+
rows.append(flat)
|
| 53 |
+
|
| 54 |
+
expanded = pd.DataFrame(rows)
|
| 55 |
+
# Temporal interpolation and smoothing
|
| 56 |
+
for kp in ALL_KEYPOINTS:
|
| 57 |
+
for suffix in ['_x', '_y']:
|
| 58 |
+
col = f"{kp}{suffix}"
|
| 59 |
+
if col in expanded.columns:
|
| 60 |
+
expanded[col] = expanded[col].interpolate(method='linear').ffill().bfill()
|
| 61 |
+
expanded[col] = gaussian_filter1d(expanded[col], sigma=sigma)
|
| 62 |
+
|
| 63 |
+
return expanded
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
parser = argparse.ArgumentParser()
|
| 67 |
+
parser.add_argument("--labels", required=True, help="Input labels directory")
|
| 68 |
+
parser.add_argument("--output", required=True, help="Output CSV path")
|
| 69 |
+
parser.add_argument("--width", type=int, default=1920)
|
| 70 |
+
parser.add_argument("--height", type=int, default=1080)
|
| 71 |
+
parser.add_argument("--sigma", type=float, default=2.0)
|
| 72 |
+
args = parser.parse_args()
|
| 73 |
+
|
| 74 |
+
print("Post-processing keypoints...")
|
| 75 |
+
df_raw = parse_labels_to_df(args.labels, args.width, args.height)
|
| 76 |
+
df_clean = process_keypoints(df_raw, sigma=args.sigma)
|
| 77 |
+
df_clean.to_csv(args.output, index=False)
|
| 78 |
+
print(f"✅ Cleaned keypoints saved to {args.output}")
|
example/run_full_pipeline.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import argparse
|
| 4 |
+
from video_utils import extract_frames, create_video_from_frames
|
| 5 |
+
|
| 6 |
+
def run_pipeline(video_path, model_path, output_dir, sigma=2.0):
|
| 7 |
+
"""Orchestrate the full pose estimation pipeline."""
|
| 8 |
+
# 1. Setup paths
|
| 9 |
+
frames_dir = os.path.join(output_dir, "frames")
|
| 10 |
+
labels_dir = os.path.join(output_dir, "labels_raw")
|
| 11 |
+
annotated_dir = os.path.join(output_dir, "annotated_frames")
|
| 12 |
+
keypoints_csv = os.path.join(output_dir, "yolo_keypoints.csv")
|
| 13 |
+
final_video = os.path.join(output_dir, "annotated_output.mp4")
|
| 14 |
+
|
| 15 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 16 |
+
|
| 17 |
+
# 2. Extract frames
|
| 18 |
+
print("--- Step 1: Extracting Frames ---")
|
| 19 |
+
fps, width, height, num_frames = extract_frames(video_path, frames_dir)
|
| 20 |
+
|
| 21 |
+
# 3. Inference
|
| 22 |
+
print("\n--- Step 2: Running YOLO Inference ---")
|
| 23 |
+
os.system(f'{sys.executable} inference_yolo_pose.py --frames "{frames_dir}" --labels "{labels_dir}" --model "{model_path}" --width {width} --height {height}')
|
| 24 |
+
|
| 25 |
+
# 4. Post-processing
|
| 26 |
+
print("\n--- Step 3: Post-processing Keypoints ---")
|
| 27 |
+
os.system(f'{sys.executable} post_process_keypoints.py --labels "{labels_dir}" --output "{keypoints_csv}" --width {width} --height {height} --sigma {sigma}')
|
| 28 |
+
|
| 29 |
+
# 5. Annotation
|
| 30 |
+
print("\n--- Step 4: Annotating Frames ---")
|
| 31 |
+
os.system(f'{sys.executable} annotate_video.py --frames "{frames_dir}" --csv "{keypoints_csv}" --output "{annotated_dir}"')
|
| 32 |
+
|
| 33 |
+
# 6. Final Video
|
| 34 |
+
print("\n--- Step 5: Creating Final Video ---")
|
| 35 |
+
create_video_from_frames(annotated_dir, final_video, fps, width, height)
|
| 36 |
+
|
| 37 |
+
print(f"\n✅ Pipeline complete! Output saved to {output_dir}")
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
parser = argparse.ArgumentParser()
|
| 41 |
+
parser.add_argument("--video", required=True, help="Input video path")
|
| 42 |
+
parser.add_argument("--model", required=True, help="YOLO model weights path")
|
| 43 |
+
parser.add_argument("--output", default="video_output", help="Output directory")
|
| 44 |
+
parser.add_argument("--sigma", type=float, default=2.0, help="Smoothing sigma")
|
| 45 |
+
args = parser.parse_args()
|
| 46 |
+
|
| 47 |
+
run_pipeline(args.video, args.model, args.output, args.sigma)
|
example/smas_pipeline_v2.py
ADDED
|
@@ -0,0 +1,600 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# smas_pipeline.py
|
| 2 |
+
"""S‑MAS Biomechanics Analysis Pipeline
|
| 3 |
+
|
| 4 |
+
Consumes 2‑D key‑point CSV output from the YOLO pose estimation notebook,
|
| 5 |
+
detects sprint gait events, evaluates the 12 Sprint‑Mechanics Assessment Score (S‑MAS)
|
| 6 |
+
criteria, and produces per‑stride and session‑level JSON results.
|
| 7 |
+
|
| 8 |
+
The implementation follows the design described in the approved implementation plan.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import numpy as np
|
| 13 |
+
import pandas as pd
|
| 14 |
+
from dataclasses import dataclass, field
|
| 15 |
+
from typing import List, Dict, Tuple
|
| 16 |
+
|
| 17 |
+
VERSION = "1.3.0-signal-based"
|
| 18 |
+
print(f"--- S-MAS Pipeline version {VERSION} loaded ---")
|
| 19 |
+
|
| 20 |
+
# -----------------------------------------------------------------------------
|
| 21 |
+
# Data structures
|
| 22 |
+
# -----------------------------------------------------------------------------
|
| 23 |
+
@dataclass
|
| 24 |
+
class FrameData:
|
| 25 |
+
idx: int
|
| 26 |
+
timestamp: float
|
| 27 |
+
keypoints: Dict[str, Tuple[float, float, float]] # (x, y, confidence)
|
| 28 |
+
# derived quantities (filled later)
|
| 29 |
+
hip_angle: float = None
|
| 30 |
+
knee_angle: float = None
|
| 31 |
+
ankle_angle: float = None
|
| 32 |
+
trunk_angle: float = None
|
| 33 |
+
shin_angle: float = None
|
| 34 |
+
pelvis_x: float = None
|
| 35 |
+
pelvis_y: float = None
|
| 36 |
+
left_contact: bool = None
|
| 37 |
+
right_contact: bool = None
|
| 38 |
+
foot_contact: bool = None # legacy/summary flag
|
| 39 |
+
|
| 40 |
+
@dataclass
|
| 41 |
+
class StrideResult:
|
| 42 |
+
stride_id: int
|
| 43 |
+
criterion_scores: Dict[str, int]
|
| 44 |
+
total_smas_score: int
|
| 45 |
+
|
| 46 |
+
@dataclass
|
| 47 |
+
class SessionResult:
|
| 48 |
+
mean_score: float
|
| 49 |
+
per_criterion_freq: Dict[str, float]
|
| 50 |
+
stride_variance: float = None
|
| 51 |
+
strides: List[StrideResult] = field(default_factory=list)
|
| 52 |
+
|
| 53 |
+
# -----------------------------------------------------------------------------
|
| 54 |
+
# Helper constants (tunable thresholds)
|
| 55 |
+
# -----------------------------------------------------------------------------
|
| 56 |
+
VERTICAL_THRESHOLD_PX = 5 # for foot‑contact detection
|
| 57 |
+
PELVIS_ANKLE_DIST_PX = 5 # for mid‑stance detection
|
| 58 |
+
HORIZONTAL_MOVEMENT_THRESHOLD_PX = 2.0 # for direction detection
|
| 59 |
+
ANGLE_TOLERANCE_DEG = 5 # generic tolerance
|
| 60 |
+
|
| 61 |
+
# -----------------------------------------------------------------------------
|
| 62 |
+
# 1. Load YOLO key‑point CSV
|
| 63 |
+
# -----------------------------------------------------------------------------
|
| 64 |
+
def load_keypoints(csv_path: str) -> List[FrameData]:
|
| 65 |
+
df = pd.read_csv(csv_path)
|
| 66 |
+
frames: Dict[int, FrameData] = {}
|
| 67 |
+
|
| 68 |
+
# Handle wide format: frame, frame_name, nose_x, ...
|
| 69 |
+
cols = df.columns
|
| 70 |
+
kp_names = set()
|
| 71 |
+
for c in cols:
|
| 72 |
+
if c.endswith('_x'):
|
| 73 |
+
kp_names.add(c[:-2])
|
| 74 |
+
|
| 75 |
+
for _, row in df.iterrows():
|
| 76 |
+
f = int(row['frame'])
|
| 77 |
+
# If we have multiple rows for same frame (multi-person), take the first one
|
| 78 |
+
if f in frames:
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
fr_data = FrameData(
|
| 82 |
+
idx=f,
|
| 83 |
+
timestamp=row.get('time', f / 30.0),
|
| 84 |
+
keypoints={}
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
for name in kp_names:
|
| 88 |
+
x = row.get(f"{name}_x")
|
| 89 |
+
y = row.get(f"{name}_y")
|
| 90 |
+
conf = row.get(f"{name}_conf", 0.5)
|
| 91 |
+
if not np.isnan(x) and not np.isnan(y):
|
| 92 |
+
fr_data.keypoints[name] = (float(x), float(y), float(conf))
|
| 93 |
+
|
| 94 |
+
frames[f] = fr_data
|
| 95 |
+
|
| 96 |
+
return [frames[i] for i in sorted(frames)]
|
| 97 |
+
|
| 98 |
+
# -----------------------------------------------------------------------------
|
| 99 |
+
# 2. Global Direction Detection
|
| 100 |
+
# -----------------------------------------------------------------------------
|
| 101 |
+
def detect_global_direction(frames: List[FrameData]) -> str:
|
| 102 |
+
"""Detect the general running direction based on frame-to-frame displacement (delta-x)."""
|
| 103 |
+
if len(frames) < 2:
|
| 104 |
+
return 'left→right' # Default
|
| 105 |
+
|
| 106 |
+
deltas = []
|
| 107 |
+
prev_x = None
|
| 108 |
+
|
| 109 |
+
for fr in frames:
|
| 110 |
+
# We use a stable point like pelvis or nose.
|
| 111 |
+
# Here we try to see if pelvis_x is already computed, if not we use nose_x if available.
|
| 112 |
+
curr_x = None
|
| 113 |
+
if fr.pelvis_x is not None:
|
| 114 |
+
curr_x = fr.pelvis_x
|
| 115 |
+
elif 'nose' in fr.keypoints:
|
| 116 |
+
curr_x = fr.keypoints['nose'][0]
|
| 117 |
+
|
| 118 |
+
if curr_x is not None:
|
| 119 |
+
if prev_x is not None:
|
| 120 |
+
delta = curr_x - prev_x
|
| 121 |
+
if abs(delta) > HORIZONTAL_MOVEMENT_THRESHOLD_PX:
|
| 122 |
+
deltas.append(delta)
|
| 123 |
+
prev_x = curr_x
|
| 124 |
+
|
| 125 |
+
if not deltas:
|
| 126 |
+
print("DEBUG: No significant horizontal movement detected. Defaulting to left→right.")
|
| 127 |
+
return 'left→right'
|
| 128 |
+
|
| 129 |
+
avg_delta = np.mean(deltas)
|
| 130 |
+
if avg_delta > 0:
|
| 131 |
+
print(f"DEBUG: Detected direction 'left→right' (avg delta_x: {avg_delta:.2f})")
|
| 132 |
+
return 'left→right'
|
| 133 |
+
else:
|
| 134 |
+
print(f"DEBUG: Detected direction 'right→left' (avg delta_x: {avg_delta:.2f})")
|
| 135 |
+
return 'right→left'
|
| 136 |
+
|
| 137 |
+
# -----------------------------------------------------------------------------
|
| 138 |
+
# 2. Compute derived geometric quantities
|
| 139 |
+
# -----------------------------------------------------------------------------
|
| 140 |
+
def _point(name: str, kp: Dict[str, Tuple[float, float, float]]) -> np.ndarray:
|
| 141 |
+
x, y, _ = kp[name]
|
| 142 |
+
return np.array([x, y])
|
| 143 |
+
|
| 144 |
+
def compute_angles(frames: List[FrameData], direction: str = 'left→right') -> None:
|
| 145 |
+
for fr in frames:
|
| 146 |
+
kp = fr.keypoints
|
| 147 |
+
# Core keypoints required for basic pose/angle calculations (standard COCO)
|
| 148 |
+
core_required = ['left_hip', 'right_hip', 'left_shoulder', 'right_shoulder',
|
| 149 |
+
'left_knee', 'right_knee', 'left_ankle', 'right_ankle']
|
| 150 |
+
if not all(r in kp for r in core_required):
|
| 151 |
+
continue
|
| 152 |
+
|
| 153 |
+
# Optional: toe/heel (use ankle if missing)
|
| 154 |
+
# Note: New CSV uses 'left_big_toe' and 'right_big_toe'
|
| 155 |
+
l_toe_name = 'left_big_toe' if 'left_big_toe' in kp else ('left_toe' if 'left_toe' in kp else 'left_ankle')
|
| 156 |
+
r_toe_name = 'right_big_toe' if 'right_big_toe' in kp else ('right_toe' if 'right_toe' in kp else 'right_ankle')
|
| 157 |
+
|
| 158 |
+
l_toe = _point(l_toe_name, kp)
|
| 159 |
+
r_toe = _point(r_toe_name, kp)
|
| 160 |
+
l_heel = _point('left_heel', kp) if 'left_heel' in kp else _point('left_ankle', kp)
|
| 161 |
+
r_heel = _point('right_heel', kp) if 'right_heel' in kp else _point('right_ankle', kp)
|
| 162 |
+
|
| 163 |
+
# Optional nose for proxy head (C7)
|
| 164 |
+
nose = _point('nose', kp) if 'nose' in kp else None
|
| 165 |
+
left_sho = _point('left_shoulder', kp)
|
| 166 |
+
right_sho = _point('right_shoulder', kp)
|
| 167 |
+
c7 = (left_sho + right_sho) / 2 # Proxy for C7
|
| 168 |
+
|
| 169 |
+
# Compute pelvis midpoint, fallback if hips missing
|
| 170 |
+
if 'left_hip' in kp and 'right_hip' in kp:
|
| 171 |
+
pelvis = (_point('left_hip', kp) + _point('right_hip', kp)) / 2
|
| 172 |
+
elif 'left_hip' in kp:
|
| 173 |
+
pelvis = _point('left_hip', kp)
|
| 174 |
+
elif 'right_hip' in kp:
|
| 175 |
+
pelvis = _point('right_hip', kp)
|
| 176 |
+
else:
|
| 177 |
+
pelvis = np.array([0.0, 0.0]) # fallback if both hips missing
|
| 178 |
+
|
| 179 |
+
fr.pelvis_x, fr.pelvis_y = pelvis
|
| 180 |
+
|
| 181 |
+
# Trunk angle (hip midpoint → C7 proxy vs vertical)
|
| 182 |
+
trunk_vec = c7 - pelvis
|
| 183 |
+
fr.trunk_angle = np.degrees(np.arctan2(trunk_vec[0], -trunk_vec[1]))
|
| 184 |
+
|
| 185 |
+
# Trailing side based on direction
|
| 186 |
+
trailing = 'right' if direction == 'left→right' else 'left'
|
| 187 |
+
hip = _point(f'{trailing}_hip', kp)
|
| 188 |
+
knee = _point(f'{trailing}_knee', kp)
|
| 189 |
+
ankle = _point(f'{trailing}_ankle', kp)
|
| 190 |
+
heel = r_heel if trailing == 'right' else l_heel
|
| 191 |
+
|
| 192 |
+
# Inject proxies back into keypoints dict for downstream lookups
|
| 193 |
+
if f'{trailing}_toe' not in kp: kp[f'{trailing}_toe'] = (float(r_toe[0] if trailing=='right' else l_toe[0]), float(r_toe[1] if trailing=='right' else l_toe[1]), 0.5)
|
| 194 |
+
if f'{trailing}_heel' not in kp: kp[f'{trailing}_heel'] = (float(heel[0]), float(heel[1]), 0.5)
|
| 195 |
+
# Also handle the leading side proxies just in case
|
| 196 |
+
leading = 'left' if trailing == 'right' else 'right'
|
| 197 |
+
if f'{leading}_toe' not in kp: kp[f'{leading}_toe'] = (float(l_toe[0] if leading=='left' else r_toe[0]), float(l_toe[1] if leading=='left' else r_toe[1]), 0.5)
|
| 198 |
+
if f'{leading}_heel' not in kp: kp[f'{leading}_heel'] = (float(l_heel[0] if leading=='left' else r_heel[0]), float(l_heel[1] if leading=='left' else r_heel[1]), 0.5)
|
| 199 |
+
|
| 200 |
+
# Proxy for calf (midpoint of knee and ankle)
|
| 201 |
+
calf = (knee + ankle) / 2
|
| 202 |
+
|
| 203 |
+
# Hip angle (hip‑pelvis‑C7)
|
| 204 |
+
hip_vec = c7 - hip
|
| 205 |
+
fr.hip_angle = np.degrees(np.arctan2(hip_vec[0], -hip_vec[1]))
|
| 206 |
+
|
| 207 |
+
# Knee angle (hip‑knee‑ankle)
|
| 208 |
+
thigh = hip - knee
|
| 209 |
+
shank = ankle - knee
|
| 210 |
+
cos_angle = np.clip(np.dot(thigh, shank) / (np.linalg.norm(thigh) * np.linalg.norm(shank)), -1, 1)
|
| 211 |
+
fr.knee_angle = np.degrees(np.arccos(cos_angle))
|
| 212 |
+
|
| 213 |
+
# Ankle dorsiflexion/plantarflexion (shank vs vertical)
|
| 214 |
+
fr.ankle_angle = np.degrees(np.arctan2(ankle[0] - knee[0], -(ankle[1] - knee[1])))
|
| 215 |
+
|
| 216 |
+
# Shin angle (knee‑ankle vs vertical)
|
| 217 |
+
shin_vec = ankle - knee
|
| 218 |
+
fr.shin_angle = np.degrees(np.arctan2(shin_vec[0], -shin_vec[1]))
|
| 219 |
+
|
| 220 |
+
# Simple foot‑contact flag (heel/toe close to estimated ground)
|
| 221 |
+
# We'll use a slightly more robust contact detection
|
| 222 |
+
l_heel = _point('left_heel', kp)
|
| 223 |
+
r_heel = _point('right_heel', kp)
|
| 224 |
+
l_toe = _point('left_toe', kp)
|
| 225 |
+
r_toe = _point('right_toe', kp)
|
| 226 |
+
|
| 227 |
+
# Ground estimation: use the lowest points in the current frame as a proxy
|
| 228 |
+
current_ground = max(l_toe[1], r_toe[1], l_heel[1], r_heel[1],
|
| 229 |
+
_point('left_ankle', kp)[1], _point('right_ankle', kp)[1])
|
| 230 |
+
|
| 231 |
+
# Trailing side heel for contact flag
|
| 232 |
+
heel_y = l_heel[1] if trailing == 'left' else r_heel[1]
|
| 233 |
+
|
| 234 |
+
fr.left_contact = l_toe[1] > current_ground - VERTICAL_THRESHOLD_PX or l_heel[1] > current_ground - VERTICAL_THRESHOLD_PX
|
| 235 |
+
fr.right_contact = r_toe[1] > current_ground - VERTICAL_THRESHOLD_PX or r_heel[1] > current_ground - VERTICAL_THRESHOLD_PX
|
| 236 |
+
fr.foot_contact = fr.left_contact or fr.right_contact
|
| 237 |
+
|
| 238 |
+
# -----------------------------------------------------------------------------
|
| 239 |
+
# 3. Gait‑event detection
|
| 240 |
+
# -----------------------------------------------------------------------------
|
| 241 |
+
def detect_events(frames: List[FrameData], direction: str = 'left→right') -> Dict[str, List[int]]:
|
| 242 |
+
events: Dict[str, List[int]] = {
|
| 243 |
+
'contralateral_toe_off': [],
|
| 244 |
+
'initial_contact_left': [],
|
| 245 |
+
'initial_contact_right': [],
|
| 246 |
+
'midstance_left': [],
|
| 247 |
+
'midstance_right': [],
|
| 248 |
+
'MVP': [],
|
| 249 |
+
'max_knee_ext': [],
|
| 250 |
+
'MVP_to_late_swing': []
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
# Build contact series per foot
|
| 254 |
+
left_contacts = [fr.left_contact for fr in frames]
|
| 255 |
+
right_contacts = [fr.right_contact for fr in frames]
|
| 256 |
+
|
| 257 |
+
for i in range(1, len(frames) - 1):
|
| 258 |
+
# Only detect events on frames that have derived quantities
|
| 259 |
+
if frames[i].pelvis_x is None or frames[i].knee_angle is None:
|
| 260 |
+
continue
|
| 261 |
+
|
| 262 |
+
# Contralateral toe‑off (trailing foot leaves ground)
|
| 263 |
+
if direction == 'left→right':
|
| 264 |
+
# Trailing is Right, so we look for Right toe-off
|
| 265 |
+
if right_contacts[i - 1] and not right_contacts[i]:
|
| 266 |
+
events['contralateral_toe_off'].append(frames[i - 1].idx)
|
| 267 |
+
else:
|
| 268 |
+
# Trailing is Left
|
| 269 |
+
if left_contacts[i - 1] and not left_contacts[i]:
|
| 270 |
+
events['contralateral_toe_off'].append(frames[i - 1].idx)
|
| 271 |
+
|
| 272 |
+
# Initial contact per foot
|
| 273 |
+
if not left_contacts[i - 1] and left_contacts[i]:
|
| 274 |
+
events['initial_contact_left'].append(frames[i].idx)
|
| 275 |
+
if not right_contacts[i - 1] and right_contacts[i]:
|
| 276 |
+
events['initial_contact_right'].append(frames[i].idx)
|
| 277 |
+
|
| 278 |
+
# Mid‑stance (pelvis over supporting ankle)
|
| 279 |
+
for fr in frames:
|
| 280 |
+
if fr.pelvis_x is None:
|
| 281 |
+
continue
|
| 282 |
+
|
| 283 |
+
if direction == 'left→right':
|
| 284 |
+
if 'left_ankle' not in fr.keypoints:
|
| 285 |
+
continue
|
| 286 |
+
ankle_x = fr.keypoints['left_ankle'][0]
|
| 287 |
+
else:
|
| 288 |
+
if 'right_ankle' not in fr.keypoints:
|
| 289 |
+
continue
|
| 290 |
+
ankle_x = fr.keypoints['right_ankle'][0]
|
| 291 |
+
|
| 292 |
+
if ankle_x is None:
|
| 293 |
+
continue
|
| 294 |
+
|
| 295 |
+
if abs(fr.pelvis_x - ankle_x) < PELVIS_ANKLE_DIST_PX:
|
| 296 |
+
key = 'midstance_left' if direction == 'left→right' else 'midstance_right'
|
| 297 |
+
events[key].append(fr.idx)
|
| 298 |
+
|
| 299 |
+
# MVP and Max Knee Extension (detecting local ones between toe-off and IC)
|
| 300 |
+
# We find intervals of "Swing" (No contact on trailing foot)
|
| 301 |
+
trailing_contacts = right_contacts if direction == 'left→right' else left_contacts
|
| 302 |
+
|
| 303 |
+
swing_intervals = []
|
| 304 |
+
start_idx = None
|
| 305 |
+
for i, contact in enumerate(trailing_contacts):
|
| 306 |
+
if not contact and start_idx is None:
|
| 307 |
+
start_idx = i
|
| 308 |
+
elif contact and start_idx is not None:
|
| 309 |
+
if i - start_idx > 3: # Ignore very short glitches
|
| 310 |
+
swing_intervals.append((start_idx, i))
|
| 311 |
+
start_idx = None
|
| 312 |
+
if start_idx is not None:
|
| 313 |
+
swing_intervals.append((start_idx, len(frames)-1))
|
| 314 |
+
|
| 315 |
+
for start, end in swing_intervals:
|
| 316 |
+
window = frames[start:end+1]
|
| 317 |
+
|
| 318 |
+
# 1. MVP (Highest pelvis height = MINIMUM Y in screen coords)
|
| 319 |
+
p_ys = [fr.pelvis_y for fr in window if fr.pelvis_y is not None]
|
| 320 |
+
if p_ys:
|
| 321 |
+
min_y = min(p_ys)
|
| 322 |
+
for fr in window:
|
| 323 |
+
if fr.pelvis_y == min_y:
|
| 324 |
+
events['MVP'].append(fr.idx)
|
| 325 |
+
break
|
| 326 |
+
|
| 327 |
+
# 2. Max Knee Extension (Minimum knee angle)
|
| 328 |
+
k_angs = [fr.knee_angle for fr in window if fr.knee_angle is not None]
|
| 329 |
+
if k_angs:
|
| 330 |
+
min_k = min(k_angs)
|
| 331 |
+
for fr in window:
|
| 332 |
+
if fr.knee_angle == min_k:
|
| 333 |
+
events['max_knee_ext'].append(fr.idx)
|
| 334 |
+
break
|
| 335 |
+
|
| 336 |
+
# Ensure MVP_to_late_swing is populated for each segment (handled in score_stride windowing later)
|
| 337 |
+
# But for backward compatibility we store the FIRST one found if any
|
| 338 |
+
if events['MVP'] and events['max_knee_ext']:
|
| 339 |
+
s = events['MVP'][0]
|
| 340 |
+
e = events['max_knee_ext'][0]
|
| 341 |
+
if s > e: s, e = e, s
|
| 342 |
+
events['MVP_to_late_swing'] = list(range(s, e + 1))
|
| 343 |
+
|
| 344 |
+
return events
|
| 345 |
+
|
| 346 |
+
# -----------------------------------------------------------------------------
|
| 347 |
+
# 4. Signal Processing & Stride Segmentation
|
| 348 |
+
# -----------------------------------------------------------------------------
|
| 349 |
+
def smooth_signal(data: np.ndarray, window: int = 5) -> np.ndarray:
|
| 350 |
+
"""Simple moving average smoothing."""
|
| 351 |
+
if len(data) < window: return data
|
| 352 |
+
return np.convolve(data, np.ones(window)/window, mode='same')
|
| 353 |
+
|
| 354 |
+
def segment_strides(frames: List[FrameData], direction: str = 'left→right') -> List[Dict[str, int]]:
|
| 355 |
+
"""Segment strides based on pelvis vertical oscillation (COM signal)."""
|
| 356 |
+
y_vals = np.array([f.pelvis_y if f.pelvis_y is not None else 0 for f in frames])
|
| 357 |
+
if len(y_vals) < 10:
|
| 358 |
+
return []
|
| 359 |
+
|
| 360 |
+
# Smooth the signal to remove jitter
|
| 361 |
+
y_smooth = smooth_signal(y_vals, window=7)
|
| 362 |
+
|
| 363 |
+
# Locate "Troughs" (Local Maxima in screen-Y = Lowest position in physical space = IC area)
|
| 364 |
+
# We find peaks in the Y signal (max-Y)
|
| 365 |
+
strides = []
|
| 366 |
+
|
| 367 |
+
# Simple peak detection (find local maxima of screen-Y)
|
| 368 |
+
# A peak must be higher than its neighbors by some prominence
|
| 369 |
+
peaks = []
|
| 370 |
+
prominence = 5.0 # px
|
| 371 |
+
for i in range(5, len(y_smooth) - 5):
|
| 372 |
+
val = y_smooth[i]
|
| 373 |
+
# Local max check
|
| 374 |
+
if val == max(y_smooth[i-5:i+6]):
|
| 375 |
+
# Prominence check (simple version: compare to local window min)
|
| 376 |
+
local_min = min(y_smooth[max(0, i-15):min(len(y_smooth), i+16)])
|
| 377 |
+
if val - local_min > prominence:
|
| 378 |
+
peaks.append(i)
|
| 379 |
+
|
| 380 |
+
# One stride = interval between consecutive peaks/troughs
|
| 381 |
+
# We'll use these peaks as "Stride Boundaries" (approximating Initial Contact)
|
| 382 |
+
for j in range(len(peaks) - 1):
|
| 383 |
+
start_idx = peaks[j]
|
| 384 |
+
end_idx = peaks[j+1]
|
| 385 |
+
|
| 386 |
+
# We need to find MVP and Max Knee Ext WITHIN this window
|
| 387 |
+
window_frames = frames[start_idx:end_idx+1]
|
| 388 |
+
|
| 389 |
+
# MVP is the Peak (Minimum Y in screen space)
|
| 390 |
+
win_ys = [fr.pelvis_y for fr in window_frames if fr.pelvis_y is not None]
|
| 391 |
+
if not win_ys: continue
|
| 392 |
+
mvp_y = min(win_ys)
|
| 393 |
+
mvp_idx = next(fr.idx for fr in window_frames if fr.pelvis_y == mvp_y)
|
| 394 |
+
|
| 395 |
+
# Max Knee Extension (Minimum knee angle during swing)
|
| 396 |
+
# Swing usually happens in the second half of this peak-to-peak interval
|
| 397 |
+
k_angs = [fr.knee_angle for fr in window_frames if fr.knee_angle is not None]
|
| 398 |
+
if not k_angs: continue
|
| 399 |
+
mke_angle = min(k_angs)
|
| 400 |
+
mke_idx = next(fr.idx for fr in window_frames if fr.knee_angle == mke_angle)
|
| 401 |
+
|
| 402 |
+
# Toe-off is usually slightly before MVP
|
| 403 |
+
# For simplicity, we define the "stride" as the full window.
|
| 404 |
+
strides.append({
|
| 405 |
+
'contralateral_toe_off': frames[start_idx].idx, # Proxy: start of recovery
|
| 406 |
+
'MVP': mvp_idx,
|
| 407 |
+
'max_knee_ext': mke_idx,
|
| 408 |
+
'initial_contact': frames[end_idx].idx
|
| 409 |
+
})
|
| 410 |
+
|
| 411 |
+
print(f"DEBUG: Signal-based segmenter found {len(strides)} strides.")
|
| 412 |
+
return strides
|
| 413 |
+
|
| 414 |
+
# -----------------------------------------------------------------------------
|
| 415 |
+
# 5. Scoring functions (each returns 0 or 1)
|
| 416 |
+
# -----------------------------------------------------------------------------
|
| 417 |
+
def trailing_limb_extension(trailing_hip_angle: float, trailing_knee_angle: float) -> int:
|
| 418 |
+
return int(trailing_hip_angle >= 45 and trailing_knee_angle <= 5)
|
| 419 |
+
|
| 420 |
+
def back_kick(heel_y: float, calf_y: float, shin_angle: float) -> int:
|
| 421 |
+
heel_above_calf = heel_y < calf_y # smaller y = higher in image coordinates
|
| 422 |
+
shin_not_parallel = shin_angle > 0
|
| 423 |
+
return int(heel_above_calf and shin_not_parallel)
|
| 424 |
+
|
| 425 |
+
def trunk_rotation(event_frames: List[int], frames: List[FrameData]) -> int:
|
| 426 |
+
# Simple proxy: large arm swing crossing the midline
|
| 427 |
+
crossed = False
|
| 428 |
+
for idx in event_frames:
|
| 429 |
+
fr = next(f for f in frames if f.idx == idx)
|
| 430 |
+
left_shoulder_x = fr.keypoints['left_shoulder'][0]
|
| 431 |
+
right_shoulder_x = fr.keypoints['right_shoulder'][0]
|
| 432 |
+
if left_shoulder_x > right_shoulder_x:
|
| 433 |
+
crossed = True
|
| 434 |
+
break
|
| 435 |
+
return int(crossed)
|
| 436 |
+
|
| 437 |
+
def thigh_separation(trailing_knee_x: float, gluteal_line_x: float) -> int:
|
| 438 |
+
return int(trailing_knee_x < gluteal_line_x)
|
| 439 |
+
|
| 440 |
+
def lumbar_extension(fr: FrameData) -> int:
|
| 441 |
+
# proxy: trunk angle more negative than -10° (extension arch)
|
| 442 |
+
return int(fr.trunk_angle < -10)
|
| 443 |
+
|
| 444 |
+
def forward_lean(fr: FrameData) -> int:
|
| 445 |
+
return int(abs(fr.trunk_angle) > 15)
|
| 446 |
+
|
| 447 |
+
def foot_contact_com_distance(fr: FrameData, foot_x: float) -> int:
|
| 448 |
+
# distance between pelvis (CoM) and foot contact x
|
| 449 |
+
return int(abs(fr.pelvis_x - foot_x) > 0.5 * (abs(fr.keypoints['left_ankle'][0] - fr.keypoints['right_ankle'][0])))
|
| 450 |
+
|
| 451 |
+
def shin_angle_criterion(fr: FrameData) -> int:
|
| 452 |
+
# ankle joint center in front of knee → shin angle < 0°
|
| 453 |
+
return int(fr.shin_angle < 0)
|
| 454 |
+
|
| 455 |
+
def foot_inclination(fr: FrameData) -> int:
|
| 456 |
+
# gap between fore‑foot or heel and ground > threshold
|
| 457 |
+
ground_y = max(fr.keypoints['left_toe'][1], fr.keypoints['right_toe'][1])
|
| 458 |
+
fore_gap = fr.keypoints['left_toe'][1] - ground_y
|
| 459 |
+
heel_gap = fr.keypoints['left_heel'][1] - ground_y
|
| 460 |
+
return int(fore_gap > VERTICAL_THRESHOLD_PX or heel_gap > VERTICAL_THRESHOLD_PX)
|
| 461 |
+
|
| 462 |
+
# -----------------------------------------------------------------------------
|
| 463 |
+
# 6. Scoring a single stride
|
| 464 |
+
# -----------------------------------------------------------------------------
|
| 465 |
+
def score_stride(frames: List[FrameData], stride_events: Dict[str, int], direction: str = 'left→right') -> Tuple[Dict[str, int], int]:
|
| 466 |
+
# Helper to fetch a frame by idx
|
| 467 |
+
def get_frame(idx: int) -> FrameData:
|
| 468 |
+
return next((f for f in frames if f.idx == idx), frames[0])
|
| 469 |
+
|
| 470 |
+
# Map event names to frames
|
| 471 |
+
# Rename 'initial_contact' back to specific name for compatibility with existing scoring logic
|
| 472 |
+
ic_key = 'initial_contact_left' if direction == 'left→right' else 'initial_contact_right'
|
| 473 |
+
ev = {
|
| 474 |
+
'contralateral_toe_off': get_frame(stride_events['contralateral_toe_off']),
|
| 475 |
+
'MVP': get_frame(stride_events['MVP']),
|
| 476 |
+
'max_knee_ext': get_frame(stride_events['max_knee_ext']),
|
| 477 |
+
ic_key: get_frame(stride_events['initial_contact']),
|
| 478 |
+
'MVP_to_late_swing': list(range(stride_events['MVP'], stride_events['max_knee_ext'] + 1))
|
| 479 |
+
}
|
| 480 |
+
|
| 481 |
+
REQUIRED_EVENTS = [
|
| 482 |
+
'contralateral_toe_off',
|
| 483 |
+
'MVP',
|
| 484 |
+
'max_knee_ext'
|
| 485 |
+
]
|
| 486 |
+
|
| 487 |
+
for e in REQUIRED_EVENTS:
|
| 488 |
+
if e not in ev:
|
| 489 |
+
raise RuntimeError(f"Missing required gait event: {e}")
|
| 490 |
+
|
| 491 |
+
# Trailing side based on direction
|
| 492 |
+
trailing = 'right' if direction == 'left→right' else 'left'
|
| 493 |
+
leading = 'left' if trailing == 'right' else 'right'
|
| 494 |
+
|
| 495 |
+
# Build criterion dictionary
|
| 496 |
+
scores = {
|
| 497 |
+
'trailing_limb_extension': trailing_limb_extension(
|
| 498 |
+
ev['contralateral_toe_off'].hip_angle,
|
| 499 |
+
ev['contralateral_toe_off'].knee_angle),
|
| 500 |
+
'back_kick': back_kick(
|
| 501 |
+
ev['contralateral_toe_off'].keypoints[f'{trailing}_heel'][1],
|
| 502 |
+
# Use proxy calf (midpoint of knee and ankle)
|
| 503 |
+
(ev['contralateral_toe_off'].keypoints[f'{trailing}_knee'][1] + ev['contralateral_toe_off'].keypoints[f'{trailing}_ankle'][1]) / 2,
|
| 504 |
+
ev['contralateral_toe_off'].shin_angle),
|
| 505 |
+
'trunk_rotation': trunk_rotation(ev['MVP_to_late_swing'], frames),
|
| 506 |
+
'thigh_separation_swing': thigh_separation(
|
| 507 |
+
ev['max_knee_ext'].keypoints[f'{trailing}_knee'][0],
|
| 508 |
+
# Use hip as gluteal line proxy
|
| 509 |
+
ev['max_knee_ext'].keypoints[f'{trailing}_hip'][0]),
|
| 510 |
+
'lumbar_extension_MVP': lumbar_extension(ev['MVP']),
|
| 511 |
+
'forward_lean_IC': forward_lean(ev['initial_contact_left'] if direction == 'left→right' else ev['initial_contact_right']),
|
| 512 |
+
'lumbar_extension_IC': lumbar_extension(ev['initial_contact_left'] if direction == 'left→right' else ev['initial_contact_right']),
|
| 513 |
+
'thigh_separation_IC': thigh_separation(
|
| 514 |
+
ev['initial_contact_left'].keypoints[f'{trailing}_knee'][0] if direction == 'left→right' else ev['initial_contact_right'].keypoints[f'{trailing}_knee'][0],
|
| 515 |
+
ev['initial_contact_left'].keypoints[f'{trailing}_hip'][0] if direction == 'left→right' else ev['initial_contact_right'].keypoints[f'{trailing}_hip'][0]),
|
| 516 |
+
'foot_contact_com_distance': foot_contact_com_distance(
|
| 517 |
+
ev['initial_contact_left'] if direction == 'left→right' else ev['initial_contact_right'],
|
| 518 |
+
ev['initial_contact_left'].keypoints[f'{trailing}_ankle'][0] if direction == 'left→right' else ev['initial_contact_right'].keypoints[f'{trailing}_ankle'][0]),
|
| 519 |
+
'shin_angle_criterion': shin_angle_criterion(ev['max_knee_ext']),
|
| 520 |
+
'foot_inclination': foot_inclination(ev['initial_contact_left'] if direction == 'left→right' else ev['initial_contact_right'])
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
total = sum(scores.values())
|
| 524 |
+
return scores, total
|
| 525 |
+
|
| 526 |
+
# -----------------------------------------------------------------------------
|
| 527 |
+
# 7. Driver function
|
| 528 |
+
# -----------------------------------------------------------------------------
|
| 529 |
+
def run_smas_pipeline(csv_path: str, direction: str = None) -> SessionResult:
|
| 530 |
+
print(f"Running S-MAS pipeline v{VERSION} on {csv_path}...")
|
| 531 |
+
frames = load_keypoints(csv_path)
|
| 532 |
+
|
| 533 |
+
# First compute basics so we have pelvis_x for direction detection
|
| 534 |
+
# We do a partial compute_angles just for pelvis if needed, but easier to just use nose if pelvis not there yet.
|
| 535 |
+
# Actually, let's call a preliminary compute_angles.
|
| 536 |
+
compute_angles(frames, 'left→right') # Direction doesn't impact pelvis_x calculation
|
| 537 |
+
|
| 538 |
+
# Auto-detect direction if not provided
|
| 539 |
+
if direction is None:
|
| 540 |
+
direction = detect_global_direction(frames)
|
| 541 |
+
|
| 542 |
+
# Full angle computation with correct direction
|
| 543 |
+
compute_angles(frames, direction)
|
| 544 |
+
|
| 545 |
+
# Segment events into individual strides using robust signal processing
|
| 546 |
+
stride_segments = segment_strides(frames, direction)
|
| 547 |
+
|
| 548 |
+
# Identify localized events for each stride
|
| 549 |
+
events = detect_events(frames, direction)
|
| 550 |
+
|
| 551 |
+
# Fallback if no full strides detected
|
| 552 |
+
if not stride_segments:
|
| 553 |
+
print("WARNING: No complete strides detected. Falling back to global analysis.")
|
| 554 |
+
# Create a single global segment using best-effort events
|
| 555 |
+
global_seg = {
|
| 556 |
+
'contralateral_toe_off': events['contralateral_toe_off'][0] if events['contralateral_toe_off'] else frames[0].idx,
|
| 557 |
+
'MVP': events['MVP'][0] if events['MVP'] else frames[len(frames)//4].idx,
|
| 558 |
+
'max_knee_ext': events['max_knee_ext'][0] if events['max_knee_ext'] else frames[len(frames)//2].idx,
|
| 559 |
+
'initial_contact': (events['initial_contact_left'] if direction == 'left→right' else events['initial_contact_right'])[0] if (events['initial_contact_left'] if direction == 'left→right' else events['initial_contact_right']) else frames[-1].idx
|
| 560 |
+
}
|
| 561 |
+
stride_segments = [global_seg]
|
| 562 |
+
|
| 563 |
+
strides_results = []
|
| 564 |
+
for i, seg in enumerate(stride_segments):
|
| 565 |
+
crit, tot = score_stride(frames, seg, direction)
|
| 566 |
+
strides_results.append(StrideResult(stride_id=i+1, criterion_scores=crit, total_smas_score=tot))
|
| 567 |
+
|
| 568 |
+
# Aggregation
|
| 569 |
+
scores = [s.total_smas_score for s in strides_results]
|
| 570 |
+
mean_score = np.mean(scores)
|
| 571 |
+
|
| 572 |
+
# Per-criterion frequency calculation
|
| 573 |
+
criterion_counts = {}
|
| 574 |
+
for s in strides_results:
|
| 575 |
+
for k, v in s.criterion_scores.items():
|
| 576 |
+
criterion_counts[k] = criterion_counts.get(k, 0) + v
|
| 577 |
+
|
| 578 |
+
per_criterion_freq = {k: v / len(strides_results) for k, v in criterion_counts.items()}
|
| 579 |
+
|
| 580 |
+
sess = SessionResult(
|
| 581 |
+
mean_score=float(mean_score),
|
| 582 |
+
per_criterion_freq=per_criterion_freq,
|
| 583 |
+
stride_variance=float(np.var(scores)) if len(scores) > 1 else 0.0,
|
| 584 |
+
strides=strides_results
|
| 585 |
+
)
|
| 586 |
+
print(f"Pipeline complete. Analyzed {len(strides_results)} strides. Mean S-MAS: {mean_score:.2f}")
|
| 587 |
+
return sess
|
| 588 |
+
|
| 589 |
+
# -----------------------------------------------------------------------------
|
| 590 |
+
# 7. CLI entry point (optional)
|
| 591 |
+
# -----------------------------------------------------------------------------
|
| 592 |
+
if __name__ == "__main__":
|
| 593 |
+
import argparse, sys
|
| 594 |
+
parser = argparse.ArgumentParser(description="Run S‑MAS pipeline on YOLO key‑point CSV.")
|
| 595 |
+
parser.add_argument('csv_path', help='Path to YOLO key‑point CSV file')
|
| 596 |
+
parser.add_argument('--direction', default='left→right', help='Running direction (left→right or right→left)')
|
| 597 |
+
args = parser.parse_args()
|
| 598 |
+
result = run_smas_pipeline(args.csv_path, args.direction)
|
| 599 |
+
json.dump(result.__dict__, sys.stdout, indent=2, default=lambda o: o.__dict__)
|
| 600 |
+
print()
|
example/train_yolo_pose.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
|
| 4 |
+
def train_model(data_path, epochs=100, imgsz=640, batch=16, model_type='yolo11n-pose.pt', name='sprint_pose'):
|
| 5 |
+
"""Train a YOLO pose model."""
|
| 6 |
+
model = YOLO(model_type)
|
| 7 |
+
|
| 8 |
+
results = model.train(
|
| 9 |
+
data=data_path,
|
| 10 |
+
epochs=epochs,
|
| 11 |
+
imgsz=imgsz,
|
| 12 |
+
batch=batch,
|
| 13 |
+
name=name
|
| 14 |
+
)
|
| 15 |
+
print(f"✅ Training complete. Results saved in runs/pose/{name}")
|
| 16 |
+
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
parser = argparse.ArgumentParser()
|
| 19 |
+
parser.add_argument("--data", required=True, help="Path to data.yaml")
|
| 20 |
+
parser.add_argument("--epochs", type=int, default=100)
|
| 21 |
+
parser.add_argument("--imgsz", type=int, default=640)
|
| 22 |
+
parser.add_argument("--batch", type=int, default=16)
|
| 23 |
+
parser.add_argument("--model", default='yolo11n-pose.pt')
|
| 24 |
+
parser.add_argument("--name", default='sprint_pose')
|
| 25 |
+
args = parser.parse_args()
|
| 26 |
+
|
| 27 |
+
train_model(args.data, args.epochs, args.imgsz, args.batch, args.model, args.name)
|
example/video_utils.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
def extract_frames(video_path, output_dir):
|
| 6 |
+
"""Extract all frames from video."""
|
| 7 |
+
cap = cv2.VideoCapture(video_path)
|
| 8 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 9 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 10 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 11 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 12 |
+
|
| 13 |
+
print(f"Video info: {width}x{height}, {fps:.2f} FPS, {total_frames} frames")
|
| 14 |
+
|
| 15 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 16 |
+
frame_idx = 0
|
| 17 |
+
while True:
|
| 18 |
+
ret, frame = cap.read()
|
| 19 |
+
if not ret:
|
| 20 |
+
break
|
| 21 |
+
|
| 22 |
+
frame_filename = os.path.join(output_dir, f"frame_{frame_idx:06d}.jpg")
|
| 23 |
+
cv2.imwrite(frame_filename, frame)
|
| 24 |
+
frame_idx += 1
|
| 25 |
+
|
| 26 |
+
if frame_idx % 100 == 0:
|
| 27 |
+
print(f"Extracted {frame_idx}/{total_frames} frames")
|
| 28 |
+
|
| 29 |
+
cap.release()
|
| 30 |
+
print(f"✅ Extracted {frame_idx} frames to {output_dir}")
|
| 31 |
+
return fps, width, height, frame_idx
|
| 32 |
+
|
| 33 |
+
def create_video_from_frames(frames_dir, output_path, fps, width, height):
|
| 34 |
+
"""Compile frames into an MP4 video."""
|
| 35 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 36 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 37 |
+
|
| 38 |
+
frame_files = sorted([f for f in os.listdir(frames_dir) if f.endswith('.jpg')])
|
| 39 |
+
print(f"Compiling {len(frame_files)} frames into {output_path}...")
|
| 40 |
+
|
| 41 |
+
for i, frame_file in enumerate(frame_files):
|
| 42 |
+
frame = cv2.imread(os.path.join(frames_dir, frame_file))
|
| 43 |
+
out.write(frame)
|
| 44 |
+
if (i + 1) % 100 == 0:
|
| 45 |
+
print(f"Written {i+1}/{len(frame_files)} frames")
|
| 46 |
+
|
| 47 |
+
out.release()
|
| 48 |
+
print(f"✅ Video saved to {output_path}")
|