Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize Mediapipe Pose Estimation
|
| 7 |
+
mp_pose = mp.solutions.pose
|
| 8 |
+
pose = mp_pose.Pose(static_image_mode=True, model_complexity=2)
|
| 9 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
+
|
| 11 |
+
def estimate_pose(image):
|
| 12 |
+
# Convert image from BGR (OpenCV) to RGB
|
| 13 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 14 |
+
# Perform pose detection
|
| 15 |
+
results = pose.process(image_rgb)
|
| 16 |
+
|
| 17 |
+
if not results.pose_landmarks:
|
| 18 |
+
return image # No pose found, return the original image
|
| 19 |
+
|
| 20 |
+
# Draw pose landmarks on the image
|
| 21 |
+
annotated_image = image.copy()
|
| 22 |
+
mp_drawing.draw_landmarks(
|
| 23 |
+
annotated_image,
|
| 24 |
+
results.pose_landmarks,
|
| 25 |
+
mp_pose.POSE_CONNECTIONS,
|
| 26 |
+
landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
|
| 27 |
+
connection_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2),
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return annotated_image
|
| 31 |
+
|
| 32 |
+
# Gradio Interface
|
| 33 |
+
interface = gr.Interface(
|
| 34 |
+
fn=estimate_pose,
|
| 35 |
+
inputs=gr.Image(type="numpy", label="Upload an Image"),
|
| 36 |
+
outputs=gr.Image(type="numpy", label="Pose Landmarks Image"),
|
| 37 |
+
title="Human Pose Estimation",
|
| 38 |
+
description="Upload an image to detect and visualize human pose landmarks.",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the Gradio app
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
interface.launch()
|