Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import cv2 | |
| import mediapipe as mp | |
| import numpy as np | |
| # Initialize mediapipe pose class | |
| mp_pose = mp.solutions.pose | |
| pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, model_complexity=1) | |
| mp_drawing = mp.solutions.drawing_utils | |
| # Define a function to classify yoga poses | |
| 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. | |
| ''' | |
| # Initialize the label of the pose. It is not known at this stage. | |
| label = 'Unknown Pose' | |
| # Specify the color (Red) with which the label will be written on the image. | |
| color = (0, 0, 255) | |
| # Calculate the required angles. | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Get the angle between the left shoulder, elbow and wrist points. | |
| 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]) | |
| # Get the angle between the right shoulder, elbow and wrist points. | |
| 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]) | |
| # Get the angle between the left elbow, shoulder and hip points. | |
| 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]) | |
| # Get the angle between the right hip, shoulder and elbow points. | |
| 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]) | |
| # Get the angle between the left hip, knee and ankle points. | |
| 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]) | |
| # Get the angle between the right hip, knee and ankle points | |
| 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]) | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check for Five-Pointed Star Pose | |
| if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][1]) < 100 and \ | |
| abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][1]) < 100 and \ | |
| abs(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) > 200 and \ | |
| abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0]) > 200: | |
| label = "Five-Pointed Star Pose" | |
| # Check if it is the warrior II pose or the T pose. | |
| # As for both of them, both arms should be straight and shoulders should be at the specific angle. | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if the both arms are straight. | |
| if left_elbow_angle > 165 and left_elbow_angle < 195 and right_elbow_angle > 165 and right_elbow_angle < 195: | |
| # Check if shoulders are at the required angle. | |
| if left_shoulder_angle > 80 and left_shoulder_angle < 110 and right_shoulder_angle > 80 and right_shoulder_angle < 110: | |
| # Check if it is the warrior II pose. | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if one leg is straight. | |
| if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195: | |
| # Check if the other leg is bended at the required angle. | |
| if left_knee_angle > 90 and left_knee_angle < 120 or right_knee_angle > 90 and right_knee_angle < 120: | |
| # Specify the label of the pose that is Warrior II pose. | |
| label = 'Warrior II Pose' | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if it is the T pose. | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if both legs are straight | |
| if left_knee_angle > 160 and left_knee_angle < 195 and right_knee_angle > 160 and right_knee_angle < 195: | |
| # Specify the label of the pose that is tree pose. | |
| label = 'T Pose' | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if it is the tree pose. | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if one leg is straight | |
| if left_knee_angle > 165 and left_knee_angle < 195 or right_knee_angle > 165 and right_knee_angle < 195: | |
| # Check if the other leg is bended at the required angle. | |
| if left_knee_angle > 315 and left_knee_angle < 335 or right_knee_angle > 25 and right_knee_angle < 45: | |
| # Specify the label of the pose that is tree pose. | |
| label = 'Tree Pose' | |
| # Check for Upward Salute Pose | |
| if abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_HIP.value][0]) < 100 and \ | |
| abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value][0]) < 100 and \ | |
| landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] and \ | |
| landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] < landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1] and \ | |
| abs(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value][1] - landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value][1]) < 50: | |
| label = "Upward Salute Pose" | |
| # Check for Hands Under Feet Pose | |
| if landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.LEFT_KNEE.value][1] and \ | |
| landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][1] > landmarks[mp_pose.PoseLandmark.RIGHT_KNEE.value][1] and \ | |
| abs(landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.LEFT_ANKLE.value][0]) < 50 and \ | |
| abs(landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value][0] - landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE.value][0]) < 50: | |
| label = "Hands Under Feet Pose" | |
| #---------------------------------------------------------------------------------------------------------------- | |
| # Check if the pose is classified successfully | |
| if label != 'Unknown Pose': | |
| def detect_and_classify_pose(input_image): | |
| frame = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB) | |
| results = pose.process(frame) | |
| pose_classification = "No pose detected" | |
| if results.pose_landmarks: | |
| mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) | |
| pose_classification = classify_pose(results.pose_landmarks.landmark) | |
| return frame, pose_classification | |
| iface = gr.Interface( | |
| fn=detect_and_classify_pose, | |
| inputs="image", | |
| outputs=["image", "text"], | |
| title="Live Yoga Pose Detection and Classification", | |
| description="This app detects and classifies yoga poses from the live camera feed using MediaPipe.", | |
| ) | |
| iface.launch() | |