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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -117
app.py CHANGED
@@ -1,136 +1,154 @@
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 input_image, pose_classification
127
-
128
- iface = gr.Interface(
129
- fn=detect_and_classify_pose,
130
- inputs="image",
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()
 
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