|
|
import cv2 |
|
|
import mediapipe as mp |
|
|
import numpy as np |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
mp_pose = mp.solutions.pose |
|
|
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5) |
|
|
|
|
|
|
|
|
mp_drawing = mp.solutions.drawing_utils |
|
|
|
|
|
def calculateAngle(landmark1, landmark2, landmark3): |
|
|
''' |
|
|
This function calculates the angle between three landmarks. |
|
|
Args: |
|
|
landmark1: The first landmark containing the x, y, and z coordinates. |
|
|
landmark2: The second landmark containing the x, y, and z coordinates. |
|
|
landmark3: The third landmark containing the x, y, and z coordinates. |
|
|
Returns: |
|
|
angle: The calculated angle between the three landmarks. |
|
|
''' |
|
|
x1, y1, _ = landmark1 |
|
|
x2, y2, _ = landmark2 |
|
|
x3, y3, _ = landmark3 |
|
|
|
|
|
angle = np.degrees(np.arctan2(y3 - y2, x3 - x2) - np.arctan2(y1 - y2, x1 - x2)) |
|
|
|
|
|
if angle < 0: |
|
|
angle += 360 |
|
|
|
|
|
return angle |
|
|
|
|
|
def classifyPose(landmarks, output_image, display=False): |
|
|
''' |
|
|
This function classifies yoga poses depending upon the angles of various body joints. |
|
|
Args: |
|
|
landmarks: A list of detected landmarks of the person whose pose needs to be classified. |
|
|
output_image: A image of the person with the detected pose landmarks drawn. |
|
|
display: A boolean value that is if set to true the function displays the resultant image with the pose label |
|
|
written on it and returns nothing. |
|
|
Returns: |
|
|
output_image: The image with the detected pose landmarks drawn and pose label written. |
|
|
label: The classified pose label of the person in the output_image. |
|
|
''' |
|
|
label = 'Unknown Pose' |
|
|
color = (0, 0, 255) |
|
|
|
|
|
left_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value]) |
|
|
|
|
|
right_elbow_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value]) |
|
|
|
|
|
left_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_HIP.value]) |
|
|
|
|
|
right_shoulder_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value]) |
|
|
|
|
|
left_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value], |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value]) |
|
|
|
|
|
right_knee_angle = calculateAngle(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value], |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value]) |
|
|
|
|
|
print(f"Left Elbow Angle: {left_elbow_angle}") |
|
|
print(f"Right Elbow Angle: {right_elbow_angle}") |
|
|
print(f"Left Shoulder Angle: {left_shoulder_angle}") |
|
|
print(f"Right Shoulder Angle: {right_shoulder_angle}") |
|
|
print(f"Left Knee Angle: {left_knee_angle}") |
|
|
print(f"Right Knee Angle: {right_knee_angle}") |
|
|
|
|
|
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].y) < 0.1 and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].y) < 0.1 and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) > 0.2 and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x) > 0.2: |
|
|
label = "Five-Pointed Star Pose" |
|
|
|
|
|
if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195: |
|
|
if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110: |
|
|
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195: |
|
|
if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120: |
|
|
label = 'Warrior II Pose' |
|
|
if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195: |
|
|
label = 'T Pose' |
|
|
|
|
|
if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195: |
|
|
if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45: |
|
|
label = 'Tree Pose' |
|
|
|
|
|
if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value].x) < 0.1 and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value].x) < 0.1 and \ |
|
|
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y and \ |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y) < 0.05: |
|
|
label = "Upward Salute Pose" |
|
|
|
|
|
if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value].y and \ |
|
|
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value].y and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value].x) < 0.05 and \ |
|
|
abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value].x) < 0.05: |
|
|
label = "Hands Under Feet Pose" |
|
|
|
|
|
if label != 'Unknown Pose': |
|
|
color = (0, 255, 0) |
|
|
|
|
|
cv2.putText(output_image, label, (10, 30), cv2.FONT_HERSHEY_PLAIN, 2, color, 2) |
|
|
|
|
|
if display: |
|
|
plt.figure(figsize=[10,10]) |
|
|
plt.imshow(output_image[:,:,::-1]) |
|
|
plt.title("Output Image") |
|
|
plt.axis('off') |
|
|
else: |
|
|
return output_image, label |
|
|
|
|
|
def run(image): |
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
results = pose.process(image) |
|
|
|
|
|
if results.pose_landmarks: |
|
|
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) |
|
|
image, classification = classifyPose(results.pose_landmarks.landmark, image) |
|
|
return image, classification |
|
|
else: |
|
|
return image, "No Pose Detected" |
|
|
|
|
|
demo = gr.Interface(fn=run, inputs="image", outputs=["image", "text"], live=True) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|