Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,35 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import pathlib
|
| 6 |
-
import math
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
import cv2
|
| 10 |
import mediapipe as mp
|
| 11 |
import numpy as np
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
mp_drawing_styles = mp.solutions.drawing_styles
|
| 15 |
mp_pose = mp.solutions.pose
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def classifyPose(landmarks, output_image, display=False):
|
| 22 |
'''
|
|
@@ -30,44 +43,33 @@ def classifyPose(landmarks, output_image, display=False):
|
|
| 30 |
output_image: The image with the detected pose landmarks drawn and pose label written.
|
| 31 |
label: The classified pose label of the person in the output_image.
|
| 32 |
'''
|
| 33 |
-
# Initialize the label of the pose. It is not known at this stage.
|
| 34 |
label = 'Unknown Pose'
|
| 35 |
-
|
| 36 |
-
# Specify the color (Red) with which the label will be written on the image.
|
| 37 |
color = (0, 0, 255)
|
| 38 |
|
| 39 |
-
# Calculate the required angles.
|
| 40 |
-
# Get the angle between the left shoulder, elbow and wrist points.
|
| 41 |
left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 42 |
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 43 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
|
| 44 |
|
| 45 |
-
# Get the angle between the right shoulder, elbow and wrist points.
|
| 46 |
right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 47 |
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
|
| 48 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
|
| 49 |
|
| 50 |
-
# Get the angle between the left elbow, shoulder and hip points.
|
| 51 |
left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 52 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 53 |
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
| 54 |
|
| 55 |
-
# Get the angle between the right hip, shoulder and elbow points.
|
| 56 |
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 57 |
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 58 |
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
| 59 |
|
| 60 |
-
# Get the angle between the left hip, knee and ankle points.
|
| 61 |
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
| 62 |
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
| 63 |
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
| 64 |
|
| 65 |
-
# Get the angle between the right hip, knee and ankle points
|
| 66 |
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 67 |
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 68 |
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 69 |
|
| 70 |
-
# Print calculated angles for debugging
|
| 71 |
print(f"Left Elbow Angle: {left_elbow_angle}")
|
| 72 |
print(f"Right Elbow Angle: {right_elbow_angle}")
|
| 73 |
print(f"Left Shoulder Angle: {left_shoulder_angle}")
|
|
@@ -75,14 +77,12 @@ def classifyPose(landmarks, output_image, display=False):
|
|
| 75 |
print(f"Left Knee Angle: {left_knee_angle}")
|
| 76 |
print(f"Right Knee Angle: {right_knee_angle}")
|
| 77 |
|
| 78 |
-
# Check for Five-Pointed Star Pose
|
| 79 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
|
| 80 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
|
| 81 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
|
| 82 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
|
| 83 |
-
label = "Five-Pointed Star Pose"
|
| 84 |
-
|
| 85 |
-
# Check if it is the warrior II pose or the T pose.
|
| 86 |
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
|
| 87 |
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
|
| 88 |
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
|
@@ -91,12 +91,10 @@ def classifyPose(landmarks, output_image, display=False):
|
|
| 91 |
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
|
| 92 |
label = 'T Pose'
|
| 93 |
|
| 94 |
-
# Check if it is the tree pose.
|
| 95 |
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
| 96 |
if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45:
|
| 97 |
label = 'Tree Pose'
|
| 98 |
|
| 99 |
-
# Check for Upward Salute Pose
|
| 100 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \
|
| 101 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \
|
| 102 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \
|
|
@@ -104,14 +102,12 @@ def classifyPose(landmarks, output_image, display=False):
|
|
| 104 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05:
|
| 105 |
label = "Upward Salute Pose"
|
| 106 |
|
| 107 |
-
# Check for Hands Under Feet Pose
|
| 108 |
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \
|
| 109 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
|
| 110 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
|
| 111 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
|
| 112 |
-
label = "Hands Under Feet Pose"
|
| 113 |
|
| 114 |
-
# If the pose is classified successfully
|
| 115 |
if label != 'Unknown Pose':
|
| 116 |
color = (0, 255, 0)
|
| 117 |
|
|
@@ -125,72 +121,18 @@ def classifyPose(landmarks, output_image, display=False):
|
|
| 125 |
else:
|
| 126 |
return output_image, label
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
image: np.ndarray,
|
| 132 |
-
model_complexity: int,
|
| 133 |
-
enable_segmentation: bool,
|
| 134 |
-
min_detection_confidence: float,
|
| 135 |
-
background_color: str,
|
| 136 |
-
) -> np.ndarray:
|
| 137 |
-
with mp_pose.Pose(
|
| 138 |
-
static_image_mode=True,
|
| 139 |
-
model_complexity=model_complexity,
|
| 140 |
-
enable_segmentation=enable_segmentation,
|
| 141 |
-
min_detection_confidence=min_detection_confidence,
|
| 142 |
-
) as pose:
|
| 143 |
-
results = pose.process(image)
|
| 144 |
-
|
| 145 |
-
res = image[:, :, ::-1].copy()
|
| 146 |
-
if enable_segmentation:
|
| 147 |
-
if background_color == "white":
|
| 148 |
-
bg_color = 255
|
| 149 |
-
elif background_color == "black":
|
| 150 |
-
bg_color = 0
|
| 151 |
-
elif background_color == "green":
|
| 152 |
-
bg_color = (0, 255, 0) # type: ignore
|
| 153 |
-
else:
|
| 154 |
-
raise ValueError
|
| 155 |
-
|
| 156 |
-
if results.segmentation_mask is not None:
|
| 157 |
-
res[results.segmentation_mask <= 0.1] = bg_color
|
| 158 |
-
else:
|
| 159 |
-
res[:] = bg_color
|
| 160 |
-
|
| 161 |
-
mp_drawing.draw_landmarks(
|
| 162 |
-
res,
|
| 163 |
-
results.pose_landmarks,
|
| 164 |
-
mp_pose.POSE_CONNECTIONS,
|
| 165 |
-
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style(),
|
| 166 |
-
)
|
| 167 |
|
| 168 |
if results.pose_landmarks:
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
model_complexities = list(range(3))
|
| 175 |
-
background_colors = ["white", "black", "green"]
|
| 176 |
-
|
| 177 |
-
image_paths = sorted(pathlib.Path("images").rglob("*.jpg"))
|
| 178 |
-
examples = [[str(path), model_complexities[1], True, 0.5, background_colors[0]] for path in image_paths]
|
| 179 |
|
| 180 |
-
demo = gr.Interface(
|
| 181 |
-
fn=run,
|
| 182 |
-
inputs=[
|
| 183 |
-
gr.Image(label="Input", type="numpy"),
|
| 184 |
-
gr.Radio(label="Model Complexity", choices=model_complexities, type="index", value=model_complexities[1]),
|
| 185 |
-
gr.Checkbox(label="Enable Segmentation", value=True),
|
| 186 |
-
gr.Slider(label="Minimum Detection Confidence", minimum=0, maximum=1, step=0.05, value=0.5),
|
| 187 |
-
gr.Radio(label="Background Color", choices=background_colors, type="value", value=background_colors[0]),
|
| 188 |
-
],
|
| 189 |
-
outputs=gr.Image(label="Output"),
|
| 190 |
-
examples=examples,
|
| 191 |
-
title=TITLE,
|
| 192 |
-
description=DESCRIPTION,
|
| 193 |
-
)
|
| 194 |
|
| 195 |
if __name__ == "__main__":
|
| 196 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import mediapipe as mp
|
| 3 |
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# Initialize MediaPipe Pose class.
|
|
|
|
| 7 |
mp_pose = mp.solutions.pose
|
| 8 |
+
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
|
| 9 |
|
| 10 |
+
# Initialize drawing class
|
| 11 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 12 |
|
| 13 |
+
def calculateAngle(landmark1, landmark2, landmark3):
|
| 14 |
+
'''
|
| 15 |
+
This function calculates the angle between three landmarks.
|
| 16 |
+
Args:
|
| 17 |
+
landmark1: The first landmark containing the x, y, and z coordinates.
|
| 18 |
+
landmark2: The second landmark containing the x, y, and z coordinates.
|
| 19 |
+
landmark3: The third landmark containing the x, y, and z coordinates.
|
| 20 |
+
Returns:
|
| 21 |
+
angle: The calculated angle between the three landmarks.
|
| 22 |
+
'''
|
| 23 |
+
x1, y1, _ = landmark1
|
| 24 |
+
x2, y2, _ = landmark2
|
| 25 |
+
x3, y3, _ = landmark3
|
| 26 |
+
|
| 27 |
+
angle = np.degrees(np.arctan2(y3 - y2, x3 - x2) - np.arctan2(y1 - y2, x1 - x2))
|
| 28 |
+
|
| 29 |
+
if angle < 0:
|
| 30 |
+
angle += 360
|
| 31 |
+
|
| 32 |
+
return angle
|
| 33 |
|
| 34 |
def classifyPose(landmarks, output_image, display=False):
|
| 35 |
'''
|
|
|
|
| 43 |
output_image: The image with the detected pose landmarks drawn and pose label written.
|
| 44 |
label: The classified pose label of the person in the output_image.
|
| 45 |
'''
|
|
|
|
| 46 |
label = 'Unknown Pose'
|
|
|
|
|
|
|
| 47 |
color = (0, 0, 255)
|
| 48 |
|
|
|
|
|
|
|
| 49 |
left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 50 |
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 51 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value])
|
| 52 |
|
|
|
|
| 53 |
right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 54 |
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value],
|
| 55 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value])
|
| 56 |
|
|
|
|
| 57 |
left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value],
|
| 58 |
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value],
|
| 59 |
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
| 60 |
|
|
|
|
| 61 |
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 62 |
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value],
|
| 63 |
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value])
|
| 64 |
|
|
|
|
| 65 |
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value],
|
| 66 |
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value],
|
| 67 |
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value])
|
| 68 |
|
|
|
|
| 69 |
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value],
|
| 70 |
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value],
|
| 71 |
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value])
|
| 72 |
|
|
|
|
| 73 |
print(f"Left Elbow Angle: {left_elbow_angle}")
|
| 74 |
print(f"Right Elbow Angle: {right_elbow_angle}")
|
| 75 |
print(f"Left Shoulder Angle: {left_shoulder_angle}")
|
|
|
|
| 77 |
print(f"Left Knee Angle: {left_knee_angle}")
|
| 78 |
print(f"Right Knee Angle: {right_knee_angle}")
|
| 79 |
|
|
|
|
| 80 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \
|
| 81 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \
|
| 82 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \
|
| 83 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2:
|
| 84 |
+
label = "Five-Pointed Star Pose"
|
| 85 |
+
|
|
|
|
| 86 |
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195:
|
| 87 |
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110:
|
| 88 |
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
|
|
|
| 91 |
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195:
|
| 92 |
label = 'T Pose'
|
| 93 |
|
|
|
|
| 94 |
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195:
|
| 95 |
if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45:
|
| 96 |
label = 'Tree Pose'
|
| 97 |
|
|
|
|
| 98 |
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \
|
| 99 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \
|
| 100 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \
|
|
|
|
| 102 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05:
|
| 103 |
label = "Upward Salute Pose"
|
| 104 |
|
|
|
|
| 105 |
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \
|
| 106 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \
|
| 107 |
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \
|
| 108 |
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05:
|
| 109 |
+
label = "Hands Under Feet Pose"
|
| 110 |
|
|
|
|
| 111 |
if label != 'Unknown Pose':
|
| 112 |
color = (0, 255, 0)
|
| 113 |
|
|
|
|
| 121 |
else:
|
| 122 |
return output_image, label
|
| 123 |
|
| 124 |
+
def run(image):
|
| 125 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 126 |
+
results = pose.process(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
if results.pose_landmarks:
|
| 129 |
+
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 130 |
+
image, classification = classifyPose(results.pose_landmarks.landmark, image)
|
| 131 |
+
return image, classification
|
| 132 |
+
else:
|
| 133 |
+
return image, "No Pose Detected"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
demo = gr.Interface(fn=run, inputs="image", outputs=["image", "text"], live=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
+
demo.launch()
|