Piarasingh85 commited on
Commit
bebf8ca
·
verified ·
1 Parent(s): 0983a8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -113
app.py CHANGED
@@ -1,154 +1,181 @@
1
- def classifyPose(landmarks, output_image, display=False):
2
- '''
3
- This function classifies yoga poses depending upon the angles of various body joints.
4
- Args:
5
- landmarks: A list of detected landmarks of the person whose pose needs to be classified.
6
- output_image: A image of the person with the detected pose landmarks drawn.
7
- display: A boolean value that is if set to true the function displays the resultant image with the pose label
8
- written on it and returns nothing.
9
- Returns:
10
- output_image: The image with the detected pose landmarks drawn and pose label written.
11
- label: The classified pose label of the person in the output_image.
12
-
13
- '''
14
-
15
- # Initialize the label of the pose. It is not known at this stage.
 
 
 
 
 
 
 
 
 
 
 
16
  label = 'Unknown Pose'
17
 
18
- # Specify the color (Red) with which the label will be written on the image.
19
  color = (0, 0, 255)
20
 
21
- # Calculate the required angles.
22
- #----------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # Get the angle between the left shoulder, elbow and wrist points.
25
- left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
26
- landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
27
- landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
28
 
29
- # Get the angle between the right shoulder, elbow and wrist points.
30
- right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
31
- landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
32
- landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
33
 
34
- # Get the angle between the left elbow, shoulder and hip points.
35
- left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
36
- landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
37
- landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
38
 
39
- # Get the angle between the right hip, shoulder and elbow points.
40
- right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
41
- landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
42
- landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
43
 
44
- # Get the angle between the left hip, knee and ankle points.
45
- left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
46
- landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
47
- landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
48
 
49
- # Get the angle between the right hip, knee and ankle points
50
- right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
51
- landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
52
- landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
53
 
54
- #----------------------------------------------------------------------------------------------------------------
55
  # Check for Five-Pointed Star Pose
56
- if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][1]) < 100 and \
57
- abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][1]) < 100 and \
58
- abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) > 200 and \
59
- abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0]) > 200:
60
- label = "Five-Pointed Star Pose"
61
-
62
- # Check if it is the warrior II pose or the T pose.
63
- # As for both of them, both arms should be straight and shoulders should be at the specific angle.
64
- #----------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- # Check if the both arms are straight.
67
- if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
68
 
69
- # Check if shoulders are at the required angle.
70
- if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
71
 
72
- # Check if it is the warrior II pose.
73
- #----------------------------------------------------------------------------------------------------------------
74
 
75
- # Check if one leg is straight.
76
- if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
77
 
78
- # Check if the other leg is bended at the required angle.
79
- if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120:
80
 
81
- # Specify the label of the pose that is Warrior II pose.
82
- label = 'Warrior II Pose'
83
 
84
- #----------------------------------------------------------------------------------------------------------------
85
 
86
- # Check if it is the T pose.
87
- #----------------------------------------------------------------------------------------------------------------
88
 
89
- # Check if both legs are straight
90
- if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
91
 
92
- # Specify the label of the pose that is tree pose.
93
- label = 'T Pose'
94
-
95
- #----------------------------------------------------------------------------------------------------------------
96
 
97
- # Check if it is the tree pose.
98
- #----------------------------------------------------------------------------------------------------------------
99
 
100
- # Check if one leg is straight
101
- if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
102
 
103
- # Check if the other leg is bended at the required angle.
104
- if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45:
105
 
106
- # Specify the label of the pose that is tree pose.
107
- label = 'Tree Pose'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # Check for Upward Salute Pose
110
- if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][0]) < 100 and \
111
- abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][0]) < 100 and \
112
- landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] and \
113
- landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1] and \
114
- abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50:
115
  label = "Upward Salute Pose"
116
 
117
- # Check for Hands Under Feet Pose
118
- if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value][1] and \
119
- landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value][1] and \
120
- abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0]) < 50 and \
121
- abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) < 50:
122
- label = "Hands Under Feet Pose"
123
-
124
  # Check for Plank Pose
125
- #----------------------------------------------------------------------------------------------------------------
126
  # The body should be in a straight line from head to heels,
127
  # so the shoulder and hip angles should be close to 180 degrees
128
  if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
129
  right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
130
  left_knee_angle > 160 and left_knee_angle < 200 and \
131
  right_knee_angle > 160 and right_knee_angle < 200:
132
- label = "Plank Pose"
133
- #----------------------------------------------------------------------------------------------------------------
 
134
 
135
- # Check if the pose is classified successfully
136
  if label != 'Unknown Pose':
137
 
138
- # Update the color (to green) with which the label will be written on the image.
139
- color = (0, 255, 0)
140
-
141
- # Write the label on the output image.
142
- cv2.putText(output_image, label, (10, 30),cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
143
 
144
- # Check if the resultant image is specified to be displayed.
145
- if display:
146
-
147
- # Display the resultant image.
148
- plt.figure(figsize=[10,10])
149
- plt.imshow(output_image[:,:,::-1]);plt.title("Output Image");plt.axis('off');
150
-
151
- else:
152
-
153
- # Return the output image and the classified label.
154
- return output_image, label
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
29
+
30
  color = (0, 0, 255)
31
 
32
+ # Calculate the required angles
33
+ left_elbow_angle = calculate_angle(
34
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
35
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
36
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
37
+
38
+ right_elbow_angle = calculate_angle(
39
+ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
40
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
41
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
42
+
43
+ left_shoulder_angle = calculate_angle(
44
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
45
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
46
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
47
+
48
+ right_shoulder_angle = calculate_angle(
49
+ landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
50
+ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
51
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
52
+
53
+ left_knee_angle = calculate_angle(
54
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
55
+ landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
56
+ landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
57
+
58
+ right_knee_angle = calculate_angle(
59
+ landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
60
+ landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
61
+ landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
62
 
 
 
 
 
63
 
 
 
 
 
64
 
 
 
 
 
65
 
 
 
 
 
66
 
 
 
 
 
67
 
 
 
 
 
68
 
 
69
  # Check for Five-Pointed Star Pose
70
+ if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
71
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
72
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
73
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
74
+ label = "Five-Pointed Star Pose"
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
 
 
 
95
 
 
 
96
 
 
 
97
 
 
 
98
 
 
 
99
 
 
 
100
 
 
101
 
 
 
102
 
 
 
103
 
 
 
 
 
104
 
 
 
105
 
 
 
106
 
 
 
107
 
108
+
109
+ # Check for Warrior II pose
110
+ if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
111
+ 80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110:
112
+ if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
113
+ (90 < left_knee_angle < 120 or 90 < right_knee_angle < 120):
114
+ label = 'Warrior II Pose'
115
+
116
+ # Check for T pose
117
+ if 165 < left_elbow_angle < 195 and 165 < right_elbow_angle < 195 and \
118
+ 80 < left_shoulder_angle < 110 and 80 < right_shoulder_angle < 110 and \
119
+ 160 < left_knee_angle < 195 and 160 < right_knee_angle < 195:
120
+ label = 'T Pose'
121
+
122
+ # Check for Tree Pose
123
+ if (165 < left_knee_angle < 195 or 165 < right_knee_angle < 195) and \
124
+ (315 < left_knee_angle < 335 or 25 < right_knee_angle < 45):
125
+ label = 'Tree Pose'
126
 
127
  # Check for Upward Salute Pose
128
+ if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \
129
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \
130
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \
131
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y and \
132
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05:
133
  label = "Upward Salute Pose"
134
 
135
+ # Check for Hands Under Feet Pose
136
+ if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \
137
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
138
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
139
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
140
+ label = "Hands Under Feet Pose"
141
+
142
  # Check for Plank Pose
143
+
144
  # The body should be in a straight line from head to heels,
145
  # so the shoulder and hip angles should be close to 180 degrees
146
  if left_shoulder_angle > 160 and left_shoulder_angle < 200 and \
147
  right_shoulder_angle > 160 and right_shoulder_angle < 200 and \
148
  left_knee_angle > 160 and left_knee_angle < 200 and \
149
  right_knee_angle > 160 and right_knee_angle < 200:
150
+ label = "Plank Pose"
151
+
152
+ # Update the color to green if pose is classified
153
 
 
154
  if label != 'Unknown Pose':
155
 
 
 
 
 
 
156
 
157
+ color = (0, 255, 0)
158
+
159
+ # Write the label on the output image
160
+ cv2.putText(output_image, label, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, color, 2)
161
+
162
+ return output_image, label
163
+
164
+ def detect_and_classify_pose(input_image):
165
+ input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
166
+ results = pose.process(input_image)
167
+ pose_classification = "No pose detected"
168
+ if results.pose_landmarks:
169
+ mp_drawing.draw_landmarks(input_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
170
+ input_image, pose_classification = classify_pose(results.pose_landmarks.landmark, input_image)
171
+ return input_image, pose_classification
172
+
173
+ iface = gr.Interface(
174
+ fn=detect_and_classify_pose,
175
+ inputs="image",
176
+ outputs=["image", "text"],
177
+ title="Live Yoga Pose Detection and Classification",
178
+ description="This app detects and classifies yoga poses from the live camera feed using MediaPipe.",
179
+ )
180
+
181
+ iface.launch()