phitran commited on
Commit
6196c52
·
1 Parent(s): 29adedc

initial commit

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import mediapipe as mp
5
+
6
+ # Initialize MediaPipe Pose
7
+ mp_pose = mp.solutions.pose
8
+ pose = mp_pose.Pose(static_image_mode=True)
9
+ mp_drawing = mp.solutions.drawing_utils
10
+ mp_pose_landmark = mp_pose.PoseLandmark
11
+
12
+ def detect_pose(image):
13
+ # Convert to RGB
14
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
15
+
16
+ # Run pose detection
17
+ result = pose.process(image_rgb)
18
+
19
+ keypoints = {}
20
+
21
+ if result.pose_landmarks:
22
+ # Draw landmarks on image
23
+ mp_drawing.draw_landmarks(image, result.pose_landmarks, mp_pose.POSE_CONNECTIONS)
24
+
25
+ # Get image dimensions
26
+ height, width, _ = image.shape
27
+
28
+ # Extract specific landmarks
29
+ landmark_indices = {
30
+ 'left_shoulder': mp_pose_landmark.LEFT_SHOULDER,
31
+ 'right_shoulder': mp_pose_landmark.RIGHT_SHOULDER,
32
+ 'left_hip': mp_pose_landmark.LEFT_HIP,
33
+ 'right_hip': mp_pose_landmark.RIGHT_HIP
34
+ }
35
+
36
+ for name, index in landmark_indices.items():
37
+ lm = result.pose_landmarks.landmark[index]
38
+ x, y = int(lm.x * width), int(lm.y * height)
39
+ keypoints[name] = (x, y)
40
+
41
+ # Draw a circle + label for debug
42
+ cv2.circle(image, (x, y), 5, (0, 255, 0), -1)
43
+ cv2.putText(image, name, (x + 5, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
44
+
45
+ return image, keypoints
46
+
47
+ # Gradio interface
48
+ iface = gr.Interface(
49
+ fn=detect_pose,
50
+ inputs=gr.Image(type="numpy", label="Upload Full-Body Image"),
51
+ outputs=[
52
+ gr.Image(type="numpy", label="Pose Visualization"),
53
+ gr.JSON(label="Extracted Keypoints")
54
+ ],
55
+ title="Virtual Try-On - Pose Detection",
56
+ description="Detects body keypoints using MediaPipe Pose and visualizes them. Shoulders and hips are labeled."
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ iface.launch()