nikshep01 commited on
Commit
d276e4f
·
verified ·
1 Parent(s): 4f22c0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py CHANGED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import mediapipe as mp
4
+ import numpy as np
5
+
6
+ # Initialize mediapipe pose class
7
+ mp_pose = mp.solutions.pose
8
+ pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, model_complexity=1)
9
+ mp_drawing = mp.solutions.drawing_utils
10
+
11
+ # Function to calculate the angle between three points
12
+ def calculate_angle(a, b, c):
13
+ a = np.array([a.x, a.y]) # First point
14
+ b = np.array([b.x, b.y]) # Mid point
15
+ c = np.array([c.x, c.y]) # End point
16
+
17
+ radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
18
+ angle = np.abs(radians * 180.0 / np.pi)
19
+
20
+ if angle > 180.0:
21
+ angle = 360 - angle
22
+
23
+ return angle
24
+
25
+ # Define a function to classify yoga poses
26
+ def classify_pose(landmarks, output_image, display=False):
27
+ label = 'Unknown Pose'
28
+ color = (0, 0, 255)
29
+
30
+ # Calculate the required angles
31
+ left_elbow_angle = calculate_angle(
32
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
33
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
34
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
35
+
36
+ right_elbow_angle = calculate_angle(
37
+ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
38
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
39
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
40
+
41
+ left_shoulder_angle = calculate_angle(
42
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
43
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
44
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
45
+
46
+ right_shoulder_angle = calculate_angle(
47
+ landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
48
+ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
49
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
50
+
51
+ left_knee_angle = calculate_angle(
52
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
53
+ landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
54
+ landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
55
+
56
+ right_knee_angle = calculate_angle(
57
+ landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
58
+ landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
59
+ landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
60
+
61
+ # Check for Five-Pointed Star Pose
62
+ if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
63
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
64
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
65
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
66
+ label = "Five-Pointed Star Pose"
67
+
68
+ # Check for Warrior II pose
69
+ if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
70
+ 80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110:
71
+ if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
72
+ (90 < left_knee_angle < 120 or 90 < right_knee_angle < 120):
73
+ label = 'Warrior II Pose'
74
+
75
+ # Check for T pose
76
+ if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
77
+ 80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110 and \
78
+ 160 < left_knee_angle < 195 and 160 < right_knee_angle < 195:
79
+ label = 'T Pose'
80
+
81
+ # Check for Tree Pose
82
+ if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
83
+ (315 < left_knee_angle < 335 or 25 < right_knee_angle < 45):
84
+ label = 'Tree Pose'
85
+
86
+ # Check for Upward Salute Pose
87
+ if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \
88
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \
89
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \
90
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y and \
91
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05:
92
+ label = "Upward Salute Pose"
93
+
94
+ # Check for Hands Under Feet Pose
95
+ if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \
96
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
97
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
98
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
99
+ label = "Hands Under Feet Pose"
100
+
101
+ # Check for Plank Pose
102
+ # The body should be in a straight line from head to heels,
103
+ # so the shoulder and hip angles should be close to 180 degrees
104
+ if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
105
+ right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
106
+ left_knee_angle > 160 and left_knee_angle < 200 and \
107
+ right_knee_angle > 160 and right_knee_angle < 200:
108
+ label = "Plank Pose"
109
+
110
+ # Update the color to green if pose is classified
111
+ if label != 'Unknown Pose':
112
+ color = (0, 255, 0)
113
+
114
+ # Write the label on the output image
115
+ cv2.putText(output_image, label, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
116
+
117
+ return output_image, label
118
+
119
+ def detect_and_classify_pose(input_image):
120
+ input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
121
+ results = pose.process(input_image)
122
+ pose_classification = "No pose detected"
123
+ if results.pose_landmarks:
124
+ mp_drawing.draw_landmarks(input_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
125
+ input_image, pose_classification = classify_pose(results.pose_landmarks.landmark, input_image)
126
+ return cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR), pose_classification
127
+
128
+ iface = gr.Interface(
129
+ fn=detect_and_classify_pose,
130
+ inputs=gr.inputs.Video(source="webcam", streaming=True),
131
+ outputs=["image", "text"],
132
+ title="Live Yoga Pose Detection and Classification",
133
+ description="This app detects and classifies yoga poses from the live camera feed using MediaPipe.",
134
+ )
135
+
136
+ iface.launch()