Piarasingh85 commited on
Commit
096798d
·
verified ·
1 Parent(s): 12df91d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -3
app.py CHANGED
@@ -9,7 +9,71 @@ pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, model
9
  mp_drawing = mp.solutions.drawing_utils
10
 
11
  # Define a function to classify yoga poses
12
- def classify_pose(landmarks):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Check if the both arms are straight.
14
  if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
15
 
@@ -61,8 +125,19 @@ def classify_pose(landmarks):
61
  abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50:
62
  label = "Upward Salute Pose"
63
 
64
- # Add more classification rules here
65
- return "Unknown Pose"
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  def detect_and_classify_pose(input_image):
68
  frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
 
9
  mp_drawing = mp.solutions.drawing_utils
10
 
11
  # Define a function to classify yoga poses
12
+ def classifyPose(landmarks, output_image, display=False):
13
+ '''
14
+ This function classifies yoga poses depending upon the angles of various body joints.
15
+ Args:
16
+ landmarks: A list of detected landmarks of the person whose pose needs to be classified.
17
+ output_image: A image of the person with the detected pose landmarks drawn.
18
+ display: A boolean value that is if set to true the function displays the resultant image with the pose label
19
+ written on it and returns nothing.
20
+ Returns:
21
+ output_image: The image with the detected pose landmarks drawn and pose label written.
22
+ label: The classified pose label of the person in the output_image.
23
+
24
+ '''
25
+
26
+ # Initialize the label of the pose. It is not known at this stage.
27
+ label = 'Unknown Pose'
28
+
29
+ # Specify the color (Red) with which the label will be written on the image.
30
+ color = (0, 0, 255)
31
+
32
+ # Calculate the required angles.
33
+ #----------------------------------------------------------------------------------------------------------------
34
+
35
+ # Get the angle between the left shoulder, elbow and wrist points.
36
+ left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
37
+ landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
38
+ landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
39
+
40
+ # Get the angle between the right shoulder, elbow and wrist points.
41
+ right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
42
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
43
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
44
+
45
+ # Get the angle between the left elbow, shoulder and hip points.
46
+ left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
47
+ landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
48
+ landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
49
+
50
+ # Get the angle between the right hip, shoulder and elbow points.
51
+ right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
52
+ landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
53
+ landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
54
+
55
+ # Get the angle between the left hip, knee and ankle points.
56
+ left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
57
+ landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
58
+ landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
59
+
60
+ # Get the angle between the right hip, knee and ankle points
61
+ right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
62
+ landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
63
+ landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
64
+
65
+ #----------------------------------------------------------------------------------------------------------------
66
+ # Check for Five-Pointed Star Pose
67
+ if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][1]) < 100 and \
68
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][1]) < 100 and \
69
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) > 200 and \
70
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0]) > 200:
71
+ label = "Five-Pointed Star Pose"
72
+
73
+ # Check if it is the warrior II pose or the T pose.
74
+ # As for both of them, both arms should be straight and shoulders should be at the specific angle.
75
+ #----------------------------------------------------------------------------------------------------------------
76
+
77
  # Check if the both arms are straight.
78
  if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
79
 
 
125
  abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50:
126
  label = "Upward Salute Pose"
127
 
128
+ # Check for Hands Under Feet Pose
129
+ if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value][1] and \
130
+ landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value][1] and \
131
+ abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0]) < 50 and \
132
+ abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) < 50:
133
+ label = "Hands Under Feet Pose"
134
+
135
+
136
+ #----------------------------------------------------------------------------------------------------------------
137
+
138
+ # Check if the pose is classified successfully
139
+ if label != 'Unknown Pose':
140
+
141
 
142
  def detect_and_classify_pose(input_image):
143
  frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)