Spaces:
Sleeping
Sleeping
Théo Rousseaux
commited on
Commit
·
aa80475
1
Parent(s):
e87f4b7
Pose Estimation script V1
Browse files
Modules/PoseEstimation/pose_estimation.ipynb
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Modules/PoseEstimation/pose_estimation.py
DELETED
|
File without changes
|
Modules/PoseEstimation/pose_estimator.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
id_joints_dict = {0: 'nose',
|
| 5 |
+
1: 'left_eye',
|
| 6 |
+
2: 'right_eye',
|
| 7 |
+
3: 'left_ear',
|
| 8 |
+
4: 'right_ear',
|
| 9 |
+
5: 'left_shoulder',
|
| 10 |
+
6: 'right_shoulder',
|
| 11 |
+
7: 'left_elbow',
|
| 12 |
+
8: 'right_elbow',
|
| 13 |
+
9: 'left_wrist',
|
| 14 |
+
10: 'right_wrist',
|
| 15 |
+
11: 'left_hip',
|
| 16 |
+
12: 'right_hip',
|
| 17 |
+
13: 'left_knee',
|
| 18 |
+
14: 'right_knee',
|
| 19 |
+
15: 'left_ankle',
|
| 20 |
+
16: 'right_ankle'}
|
| 21 |
+
joints_id_dict = {v: k for k, v in id_joints_dict.items()}
|
| 22 |
+
|
| 23 |
+
model = YOLO('yolov8n-pose.pt')
|
| 24 |
+
|
| 25 |
+
def get_keypoints_from_keypoints(model, video_path):
|
| 26 |
+
|
| 27 |
+
keypoints = []
|
| 28 |
+
results = model(video_path, save=True, show_conf=False, show_boxes=False)
|
| 29 |
+
for frame in results:
|
| 30 |
+
keypoints.append(frame.keypoints.xy)
|
| 31 |
+
|
| 32 |
+
return keypoints
|
| 33 |
+
|
| 34 |
+
keypoints = get_keypoints_from_keypoints(model, '../../data/pose/squat.mp4')
|
| 35 |
+
|
| 36 |
+
def calculate_angle(a, b, c):
|
| 37 |
+
|
| 38 |
+
"""
|
| 39 |
+
Calculates the angle between three joints.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
a (tuple): coordinates of the first joint
|
| 43 |
+
b (tuple): coordinates of the second joint
|
| 44 |
+
c (tuple): coordinates of the third joint
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
angle (float): angle between the three joints
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
ba = np.array(a) - np.array(b)
|
| 51 |
+
bc = np.array(c) - np.array(b)
|
| 52 |
+
|
| 53 |
+
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
|
| 54 |
+
angle = np.arccos(cosine_angle)
|
| 55 |
+
|
| 56 |
+
return np.degrees(angle)
|
| 57 |
+
|
| 58 |
+
def compute_left_knee_angle(pose):
|
| 59 |
+
|
| 60 |
+
"""
|
| 61 |
+
Computes the knee angle.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
pose (dict): pose dictionary
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
knee_angle (float): knee angle
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
left_hip = pose[0][joints_id_dict['left_hip']]
|
| 71 |
+
left_knee = pose[0][joints_id_dict['left_knee']]
|
| 72 |
+
left_ankle = pose[0][joints_id_dict['left_ankle']]
|
| 73 |
+
|
| 74 |
+
knee_angle = calculate_angle(left_hip, left_knee, left_ankle)
|
| 75 |
+
|
| 76 |
+
return knee_angle
|
| 77 |
+
|
| 78 |
+
def compute_right_knee_angle(pose):
|
| 79 |
+
|
| 80 |
+
"""
|
| 81 |
+
Computes the knee angle.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
pose (dict): pose dictionary
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
knee_angle (float): knee angle
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
right_hip = pose[0][joints_id_dict['right_hip']]
|
| 91 |
+
right_knee = pose[0][joints_id_dict['right_knee']]
|
| 92 |
+
right_ankle = pose[0][joints_id_dict['right_ankle']]
|
| 93 |
+
|
| 94 |
+
knee_angle = calculate_angle(right_hip, right_knee, right_ankle)
|
| 95 |
+
|
| 96 |
+
return knee_angle
|
| 97 |
+
|
| 98 |
+
def moving_average(data, window_size):
|
| 99 |
+
|
| 100 |
+
"""
|
| 101 |
+
Computes the moving average of a list.
|
| 102 |
+
|
| 103 |
+
Args:
|
| 104 |
+
data (list): list of values
|
| 105 |
+
window_size (int): size of the window
|
| 106 |
+
|
| 107 |
+
Returns:
|
| 108 |
+
avg (list): list of moving average values
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
avg = []
|
| 112 |
+
for i in range(len(data) - window_size + 1):
|
| 113 |
+
avg.append(sum(data[i:i + window_size]) / window_size)
|
| 114 |
+
|
| 115 |
+
return avg
|
| 116 |
+
|
config.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Pose estimation
|
| 2 |
+
|
| 3 |
+
pose_mode_size = 'm'
|