Update pose_analysis.py
Browse files- pose_analysis.py +135 -134
pose_analysis.py
CHANGED
@@ -1,135 +1,136 @@
|
|
1 |
-
import math
|
2 |
-
import cv2
|
3 |
-
import mediapipe as mp
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
(1 - abs(
|
48 |
-
(1 -
|
49 |
-
(1 -
|
50 |
-
(1 -
|
51 |
-
(1 -
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
mp_pose.PoseLandmark.
|
62 |
-
mp_pose.PoseLandmark.
|
63 |
-
mp_pose.PoseLandmark.
|
64 |
-
mp_pose.PoseLandmark.
|
65 |
-
mp_pose.PoseLandmark.
|
66 |
-
mp_pose.PoseLandmark.
|
67 |
-
mp_pose.PoseLandmark.
|
68 |
-
mp_pose.PoseLandmark.
|
69 |
-
mp_pose.PoseLandmark.
|
70 |
-
mp_pose.PoseLandmark.
|
71 |
-
mp_pose.PoseLandmark.
|
72 |
-
mp_pose.PoseLandmark.
|
73 |
-
mp_pose.PoseLandmark.
|
74 |
-
mp_pose.PoseLandmark.
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
(mp_pose.PoseLandmark.
|
81 |
-
(mp_pose.PoseLandmark.
|
82 |
-
(mp_pose.PoseLandmark.NOSE, mp_pose.PoseLandmark.
|
83 |
-
(mp_pose.PoseLandmark.
|
84 |
-
(mp_pose.PoseLandmark.LEFT_SHOULDER, mp_pose.PoseLandmark.
|
85 |
-
(mp_pose.PoseLandmark.
|
86 |
-
(mp_pose.PoseLandmark.
|
87 |
-
(mp_pose.PoseLandmark.
|
88 |
-
(mp_pose.PoseLandmark.
|
89 |
-
(mp_pose.PoseLandmark.
|
90 |
-
(mp_pose.PoseLandmark.
|
91 |
-
(mp_pose.PoseLandmark.LEFT_HIP, mp_pose.PoseLandmark.
|
92 |
-
(mp_pose.PoseLandmark.
|
93 |
-
(mp_pose.PoseLandmark.
|
94 |
-
(mp_pose.PoseLandmark.
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
135 |
return annotated_frame
|
|
|
1 |
+
import math
|
2 |
+
import cv2
|
3 |
+
import mediapipe as mp
|
4 |
+
|
5 |
+
@spaces.GPU(duration=300)
|
6 |
+
mp_pose = mp.solutions.pose
|
7 |
+
mp_drawing = mp.solutions.drawing_utils
|
8 |
+
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.7, min_tracking_confidence=0.7)
|
9 |
+
|
10 |
+
def calculate_posture_score(frame):
|
11 |
+
image_height, image_width, _ = frame.shape
|
12 |
+
results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
13 |
+
|
14 |
+
if not results.pose_landmarks:
|
15 |
+
return None, None
|
16 |
+
|
17 |
+
landmarks = results.pose_landmarks.landmark
|
18 |
+
|
19 |
+
# Use only body landmarks
|
20 |
+
left_shoulder = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
|
21 |
+
right_shoulder = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]
|
22 |
+
left_hip = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value]
|
23 |
+
right_hip = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value]
|
24 |
+
left_knee = landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value]
|
25 |
+
right_knee = landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value]
|
26 |
+
|
27 |
+
# Calculate angles
|
28 |
+
shoulder_angle = abs(math.degrees(math.atan2(right_shoulder.y - left_shoulder.y, right_shoulder.x - left_shoulder.x)))
|
29 |
+
hip_angle = abs(math.degrees(math.atan2(right_hip.y - left_hip.y, right_hip.x - left_hip.x)))
|
30 |
+
knee_angle = abs(math.degrees(math.atan2(right_knee.y - left_knee.y, right_knee.x - left_knee.x)))
|
31 |
+
|
32 |
+
# Calculate vertical alignment
|
33 |
+
shoulder_hip_alignment = abs((left_shoulder.y + right_shoulder.y) / 2 - (left_hip.y + right_hip.y) / 2)
|
34 |
+
hip_knee_alignment = abs((left_hip.y + right_hip.y) / 2 - (left_knee.y + right_knee.y) / 2)
|
35 |
+
# Add head landmarks
|
36 |
+
nose = landmarks[mp_pose.PoseLandmark.NOSE.value]
|
37 |
+
left_ear = landmarks[mp_pose.PoseLandmark.LEFT_EAR.value]
|
38 |
+
right_ear = landmarks[mp_pose.PoseLandmark.RIGHT_EAR.value]
|
39 |
+
# Calculate head tilt
|
40 |
+
head_tilt = abs(math.degrees(math.atan2(right_ear.y - left_ear.y, right_ear.x - left_ear.x)))
|
41 |
+
# Calculate head position relative to shoulders
|
42 |
+
head_position = abs((nose.y - (left_shoulder.y + right_shoulder.y) / 2) /
|
43 |
+
((left_shoulder.y + right_shoulder.y) / 2 - (left_hip.y + right_hip.y) / 2))
|
44 |
+
|
45 |
+
# Combine metrics into a single posture score (you may need to adjust these weights)
|
46 |
+
posture_score = (
|
47 |
+
(1 - abs(shoulder_angle - hip_angle) / 90) * 0.3 +
|
48 |
+
(1 - abs(hip_angle - knee_angle) / 90) * 0.2 +
|
49 |
+
(1 - shoulder_hip_alignment) * 0.1 +
|
50 |
+
(1 - hip_knee_alignment) * 0.1 +
|
51 |
+
(1 - abs(head_tilt - 90) / 90) * 0.15 +
|
52 |
+
(1 - head_position) * 0.15
|
53 |
+
)
|
54 |
+
|
55 |
+
return posture_score, results.pose_landmarks
|
56 |
+
|
57 |
+
def draw_pose_landmarks(frame, landmarks):
|
58 |
+
annotated_frame = frame.copy()
|
59 |
+
# Include relevant landmarks for head position and body
|
60 |
+
body_landmarks = [
|
61 |
+
mp_pose.PoseLandmark.NOSE,
|
62 |
+
mp_pose.PoseLandmark.LEFT_SHOULDER,
|
63 |
+
mp_pose.PoseLandmark.RIGHT_SHOULDER,
|
64 |
+
mp_pose.PoseLandmark.LEFT_EAR,
|
65 |
+
mp_pose.PoseLandmark.RIGHT_EAR,
|
66 |
+
mp_pose.PoseLandmark.LEFT_ELBOW,
|
67 |
+
mp_pose.PoseLandmark.RIGHT_ELBOW,
|
68 |
+
mp_pose.PoseLandmark.LEFT_WRIST,
|
69 |
+
mp_pose.PoseLandmark.RIGHT_WRIST,
|
70 |
+
mp_pose.PoseLandmark.LEFT_HIP,
|
71 |
+
mp_pose.PoseLandmark.RIGHT_HIP,
|
72 |
+
mp_pose.PoseLandmark.LEFT_KNEE,
|
73 |
+
mp_pose.PoseLandmark.RIGHT_KNEE,
|
74 |
+
mp_pose.PoseLandmark.LEFT_ANKLE,
|
75 |
+
mp_pose.PoseLandmark.RIGHT_ANKLE
|
76 |
+
]
|
77 |
+
|
78 |
+
# Connections for head position and body
|
79 |
+
body_connections = [
|
80 |
+
(mp_pose.PoseLandmark.LEFT_EAR, mp_pose.PoseLandmark.LEFT_SHOULDER),
|
81 |
+
(mp_pose.PoseLandmark.RIGHT_EAR, mp_pose.PoseLandmark.RIGHT_SHOULDER),
|
82 |
+
(mp_pose.PoseLandmark.NOSE, mp_pose.PoseLandmark.LEFT_SHOULDER),
|
83 |
+
(mp_pose.PoseLandmark.NOSE, mp_pose.PoseLandmark.RIGHT_SHOULDER),
|
84 |
+
(mp_pose.PoseLandmark.LEFT_SHOULDER, mp_pose.PoseLandmark.RIGHT_SHOULDER),
|
85 |
+
(mp_pose.PoseLandmark.LEFT_SHOULDER, mp_pose.PoseLandmark.LEFT_ELBOW),
|
86 |
+
(mp_pose.PoseLandmark.RIGHT_SHOULDER, mp_pose.PoseLandmark.RIGHT_ELBOW),
|
87 |
+
(mp_pose.PoseLandmark.LEFT_ELBOW, mp_pose.PoseLandmark.LEFT_WRIST),
|
88 |
+
(mp_pose.PoseLandmark.RIGHT_ELBOW, mp_pose.PoseLandmark.RIGHT_WRIST),
|
89 |
+
(mp_pose.PoseLandmark.LEFT_SHOULDER, mp_pose.PoseLandmark.LEFT_HIP),
|
90 |
+
(mp_pose.PoseLandmark.RIGHT_SHOULDER, mp_pose.PoseLandmark.RIGHT_HIP),
|
91 |
+
(mp_pose.PoseLandmark.LEFT_HIP, mp_pose.PoseLandmark.RIGHT_HIP),
|
92 |
+
(mp_pose.PoseLandmark.LEFT_HIP, mp_pose.PoseLandmark.LEFT_KNEE),
|
93 |
+
(mp_pose.PoseLandmark.RIGHT_HIP, mp_pose.PoseLandmark.RIGHT_KNEE),
|
94 |
+
(mp_pose.PoseLandmark.LEFT_KNEE, mp_pose.PoseLandmark.LEFT_ANKLE),
|
95 |
+
(mp_pose.PoseLandmark.RIGHT_KNEE, mp_pose.PoseLandmark.RIGHT_ANKLE)
|
96 |
+
]
|
97 |
+
|
98 |
+
# Draw landmarks
|
99 |
+
for landmark in body_landmarks:
|
100 |
+
if landmark in landmarks.landmark:
|
101 |
+
lm = landmarks.landmark[landmark]
|
102 |
+
h, w, _ = annotated_frame.shape
|
103 |
+
cx, cy = int(lm.x * w), int(lm.y * h)
|
104 |
+
cv2.circle(annotated_frame, (cx, cy), 5, (245, 117, 66), -1)
|
105 |
+
|
106 |
+
# Draw connections
|
107 |
+
for connection in body_connections:
|
108 |
+
start_lm = landmarks.landmark[connection[0]]
|
109 |
+
end_lm = landmarks.landmark[connection[1]]
|
110 |
+
h, w, _ = annotated_frame.shape
|
111 |
+
start_point = (int(start_lm.x * w), int(start_lm.y * h))
|
112 |
+
end_point = (int(end_lm.x * w), int(end_lm.y * h))
|
113 |
+
cv2.line(annotated_frame, start_point, end_point, (245, 66, 230), 2)
|
114 |
+
|
115 |
+
# Highlight head tilt
|
116 |
+
left_ear = landmarks.landmark[mp_pose.PoseLandmark.LEFT_EAR]
|
117 |
+
right_ear = landmarks.landmark[mp_pose.PoseLandmark.RIGHT_EAR]
|
118 |
+
nose = landmarks.landmark[mp_pose.PoseLandmark.NOSE]
|
119 |
+
|
120 |
+
h, w, _ = annotated_frame.shape
|
121 |
+
left_ear_point = (int(left_ear.x * w), int(left_ear.y * h))
|
122 |
+
right_ear_point = (int(right_ear.x * w), int(right_ear.y * h))
|
123 |
+
nose_point = (int(nose.x * w), int(nose.y * h))
|
124 |
+
|
125 |
+
# Draw a line between ears to show head tilt
|
126 |
+
cv2.line(annotated_frame, left_ear_point, right_ear_point, (0, 255, 0), 2)
|
127 |
+
|
128 |
+
# Draw a line from nose to the midpoint between shoulders to show head forward/backward tilt
|
129 |
+
left_shoulder = landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
|
130 |
+
right_shoulder = landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
|
131 |
+
shoulder_mid_x = (left_shoulder.x + right_shoulder.x) / 2
|
132 |
+
shoulder_mid_y = (left_shoulder.y + right_shoulder.y) / 2
|
133 |
+
shoulder_mid_point = (int(shoulder_mid_x * w), int(shoulder_mid_y * h))
|
134 |
+
cv2.line(annotated_frame, nose_point, shoulder_mid_point, (0, 255, 0), 2)
|
135 |
+
|
136 |
return annotated_frame
|