Piarasingh85 commited on
Commit
4bbd07d
·
verified ·
1 Parent(s): 11e6dfd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -27
app.py CHANGED
@@ -10,9 +10,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) # First point
14
- b = np.array(b) # Mid point
15
- c = np.array(c) # 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)
@@ -29,34 +29,34 @@ def classify_pose(landmarks, output_image, display=False):
29
 
30
  # Calculate the required angles
31
  left_elbow_angle = calculate_angle(
32
- [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y],
33
- [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y],
34
- [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y])
35
 
36
  right_elbow_angle = calculate_angle(
37
- [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y],
38
- [landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y],
39
- [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y])
40
 
41
  left_shoulder_angle = calculate_angle(
42
- [landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y],
43
- [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y],
44
- [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y])
45
 
46
  right_shoulder_angle = calculate_angle(
47
- [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y],
48
- [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y],
49
- [landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y])
50
 
51
  left_knee_angle = calculate_angle(
52
- [landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x, landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y],
53
- [landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y],
54
- [landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].y])
55
 
56
  right_knee_angle = calculate_angle(
57
- [landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y],
58
- [landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y],
59
- [landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x, landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].y])
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 \
@@ -98,16 +98,15 @@ def classify_pose(landmarks, output_image, display=False):
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
- return label
102
 
103
  def detect_and_classify_pose(input_image):
104
- frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
105
- results = pose.process(frame)
106
  pose_classification = "No pose detected"
107
  if results.pose_landmarks:
108
- mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
109
- pose_classification = classify_pose(results.pose_landmarks.landmark, frame)
110
- return frame, pose_classification
111
 
112
  iface = gr.Interface(
113
  fn=detect_and_classify_pose,
 
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)
 
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 \
 
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
+ return output_image, label
102
 
103
  def detect_and_classify_pose(input_image):
104
+ results = pose.process(input_image)
 
105
  pose_classification = "No pose detected"
106
  if results.pose_landmarks:
107
+ mp_drawing.draw_landmarks(input_image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
108
+ input_image, pose_classification = classify_pose(results.pose_landmarks.landmark, input_image)
109
+ return input_image, pose_classification
110
 
111
  iface = gr.Interface(
112
  fn=detect_and_classify_pose,