-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpose_utils.py
More file actions
71 lines (62 loc) · 2.95 KB
/
Copy pathpose_utils.py
File metadata and controls
71 lines (62 loc) · 2.95 KB
1
2
3
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import cv2
import mediapipe as mp
import numpy as np
# Initialize MediaPipe
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
def calculate_angle(a, b, c):
"""Calculate the angle between three 2D points"""
a = np.array(a) # First point
b = np.array(b) # Mid point
c = np.array(c) # End point
radians = np.arctan2(c[1]-b[1], c[0]-b[0]) - np.arctan2(a[1]-b[1], a[0]-b[0])
angle = np.abs(radians * 180.0 / np.pi)
if angle > 180.0:
angle = 360 - angle
return angle
def get_landmark_coordinates(landmarks, landmark_type, side="left"):
"""Get coordinates for a specific landmark with proper left/right detection"""
# MediaPipe landmarks are from the person's perspective, not camera view
landmark_map = {
"SHOULDER": mp_pose.PoseLandmark.LEFT_SHOULDER if side == "left" else mp_pose.PoseLandmark.RIGHT_SHOULDER,
"ELBOW": mp_pose.PoseLandmark.LEFT_ELBOW if side == "left" else mp_pose.PoseLandmark.RIGHT_ELBOW,
"WRIST": mp_pose.PoseLandmark.LEFT_WRIST if side == "left" else mp_pose.PoseLandmark.RIGHT_WRIST,
"HIP": mp_pose.PoseLandmark.LEFT_HIP if side == "left" else mp_pose.PoseLandmark.RIGHT_HIP,
"KNEE": mp_pose.PoseLandmark.LEFT_KNEE if side == "left" else mp_pose.PoseLandmark.RIGHT_KNEE,
"ANKLE": mp_pose.PoseLandmark.LEFT_ANKLE if side == "left" else mp_pose.PoseLandmark.RIGHT_ANKLE
}
if landmark_type in landmark_map:
landmark = landmark_map[landmark_type]
if landmark.value < len(landmarks):
return [
landmarks[landmark.value].x,
landmarks[landmark.value].y,
landmarks[landmark.value].z
]
return None
def draw_progress_bar(image, progress, position, size=(100, 10), color=(0, 255, 0)):
"""Draw a progress bar"""
x, y = position
width, height = size
# Draw background
cv2.rectangle(image, (x, y), (x + width, y + height), (50, 50, 50), -1)
# Draw progress
fill_width = int(width * progress)
if fill_width > 0:
cv2.rectangle(image, (x, y), (x + fill_width, y + height), color, -1)
# Draw border
cv2.rectangle(image, (x, y), (x + width, y + height), (255, 255, 255), 1)
def draw_circular_gauge(image, value, position, radius=20, color=(0, 255, 0)):
"""Draw a circular gauge for symmetry score"""
x, y = position
# Draw background circle
cv2.circle(image, (x, y), radius, (50, 50, 50), -1)
# Draw value arc
if value > 0:
end_angle = int(270 * value / 100) # Convert to 270 degrees for visual appeal
cv2.ellipse(image, (x, y), (radius, radius), 135, 0, end_angle, color, -1)
# Draw outer circle
cv2.circle(image, (x, y), radius, (255, 255, 255), 2)
# Draw center text
cv2.putText(image, f"{int(value)}", (x-10, y+5),
cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1, cv2.LINE_AA)