beta3 commited on
Commit
99f6f43
·
verified ·
1 Parent(s): 4cebc35

Push Space to HF

Browse files
Files changed (6) hide show
  1. .gitattributes +1 -0
  2. Dockerfile +19 -0
  3. README.md +52 -5
  4. app.py +484 -0
  5. pose_landmarker.task +3 -0
  6. requirements.txt +4 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ pose_landmarker.task filter=lfs diff=lfs merge=lfs -text
Dockerfile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ RUN apt-get update && apt-get install -y \
6
+ ffmpeg \
7
+ libgl1 \
8
+ libglib2.0-0 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ COPY requirements.txt .
12
+
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ COPY . .
16
+
17
+ ENV PORT=7860
18
+
19
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,11 +1,58 @@
1
  ---
2
  title: MediaPipe Pose Estimation
3
- emoji: 🐢
4
- colorFrom: gray
5
- colorTo: red
6
  sdk: docker
7
  pinned: false
8
- short_description: Track 33 body keypoints using MediaPipe
9
  ---
10
 
11
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: MediaPipe Pose Estimation
3
+ emoji: 🧍
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
+ # MediaPipe Pose Estimation
11
+
12
+ Professional pose detection and visualization system powered by Google MediaPipe. Track 33 body keypoints with customizable visualization options.
13
+
14
+ ## Features
15
+
16
+ - Real-time pose estimation with 33 anatomical keypoints
17
+ - Adjustable detection and tracking confidence thresholds
18
+ - Black background or original video overlay options
19
+ - Single color or multicolor body part visualization
20
+ - Professional-grade output with H.264 encoding
21
+
22
+ ## Quick Start
23
+
24
+ 1. Upload a video file (MP4, AVI, MOV, WebM)
25
+ 2. Adjust confidence parameters (default: 0.5 recommended)
26
+ 3. Select background type and color mode
27
+ 4. Process video
28
+
29
+ ## Parameters
30
+
31
+ **Detection Confidence** (0.0-1.0)
32
+ Controls pose detection sensitivity. Higher values reduce false positives.
33
+
34
+ **Tracking Confidence** (0.0-1.0)
35
+ Determines tracking stability across frames. Higher values provide more stable tracking.
36
+
37
+ ## Color Modes
38
+
39
+ **Single Color**: Uniform skeleton visualization with customizable colors
40
+
41
+ **Multicolor**: Body parts colored by region
42
+ - Torso: Yellow
43
+ - Right Arm: Red | Left Arm: Blue
44
+ - Right Leg: Magenta | Left Leg: Green
45
+
46
+ ## Output
47
+
48
+ - Format: MP4 (H.264)
49
+ - Resolution and frame rate match input
50
+ - Processing time scales with video length and resolution
51
+
52
+ ## Technical Stack
53
+
54
+ MediaPipe Pose Landmarker | OpenCV | Gradio | Python
55
+
56
+ ---
57
+
58
+ **Note**: Videos exceeding 2 minutes or high resolution may require extended processing time.
app.py ADDED
@@ -0,0 +1,484 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import gradio as gr
4
+ import mediapipe as mp
5
+ from mediapipe.tasks.python import vision
6
+ from mediapipe.tasks.python import BaseOptions
7
+ from mediapipe.tasks.python.vision import PoseLandmarker, PoseLandmarkerOptions, RunningMode
8
+
9
+ MODEL_PATH = "pose_landmarker.task"
10
+
11
+ # Color palette (BGR format)
12
+ COLORS = {
13
+ "White": (255, 255, 255),
14
+ "Red": (0, 0, 255),
15
+ "Green": (0, 255, 0),
16
+ "Blue": (255, 0, 0),
17
+ "Yellow": (0, 255, 255),
18
+ "Cyan": (255, 255, 0),
19
+ "Magenta": (255, 0, 255),
20
+ "Orange": (0, 165, 255),
21
+ "Purple": (255, 0, 128),
22
+ "Pink": (203, 192, 255),
23
+ }
24
+
25
+ MULTICOLOR_SCHEME = {
26
+ "face": (255, 255, 0), # Cyan
27
+ "torso": (0, 255, 255), # Yellow
28
+ "right_arm": (0, 0, 255), # Red
29
+ "left_arm": (255, 0, 0), # Blue
30
+ "right_leg": (255, 0, 255), # Magenta
31
+ "left_leg": (0, 255, 0), # Green
32
+ }
33
+
34
+ def get_body_part_connections():
35
+ """Define which connections belong to which body part"""
36
+
37
+ connections = {
38
+ "face": [
39
+ (0, 1), (1, 2), (2, 3), (3, 7), # Right eye region
40
+ (0, 4), (4, 5), (5, 6), (6, 8), # Left eye region
41
+ (9, 10), # Mouth
42
+ ],
43
+ "torso": [
44
+ (11, 12), # Shoulders
45
+ (11, 23), (12, 24), # Shoulder to hip
46
+ (23, 24), # Hips
47
+ ],
48
+ "right_arm": [
49
+ (11, 13), (13, 15), # Shoulder to elbow to wrist
50
+ (15, 17), (15, 19), (15, 21), # Wrist connections
51
+ (17, 19), # Hand
52
+ ],
53
+ "left_arm": [
54
+ (12, 14), (14, 16), # Shoulder to elbow to wrist
55
+ (16, 18), (16, 20), (16, 22), # Wrist connections
56
+ (18, 20), # Hand
57
+ ],
58
+ "right_leg": [
59
+ (23, 25), (25, 27), # Hip to knee to ankle
60
+ (27, 29), (27, 31), # Ankle connections
61
+ (29, 31), # Foot
62
+ ],
63
+ "left_leg": [
64
+ (24, 26), (26, 28), # Hip to knee to ankle
65
+ (28, 30), (28, 32), # Ankle connections
66
+ (30, 32), # Foot
67
+ ],
68
+ }
69
+ return connections
70
+
71
+ def draw_pose(
72
+ video_path,
73
+ detection_confidence,
74
+ tracking_confidence,
75
+ background_type,
76
+ color_mode,
77
+ line_color,
78
+ joint_color
79
+ ):
80
+ output_path = "output.mp4"
81
+
82
+ options = PoseLandmarkerOptions(
83
+ base_options=BaseOptions(model_asset_path=MODEL_PATH),
84
+ running_mode=RunningMode.VIDEO,
85
+ num_poses=1,
86
+ min_pose_detection_confidence=detection_confidence,
87
+ min_tracking_confidence=tracking_confidence,
88
+ )
89
+ landmarker = PoseLandmarker.create_from_options(options)
90
+
91
+ cap = cv2.VideoCapture(video_path)
92
+ width = int(cap.get(3))
93
+ height = int(cap.get(4))
94
+ fps = cap.get(5) or 24
95
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
96
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
97
+
98
+ timestamp = 0
99
+
100
+ line_bgr = COLORS[line_color]
101
+ joint_bgr = COLORS[joint_color]
102
+
103
+
104
+ body_parts = get_body_part_connections()
105
+
106
+ while cap.isOpened():
107
+ ret, frame = cap.read()
108
+ if not ret:
109
+ break
110
+
111
+ rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
112
+ mp_image = mp.Image(
113
+ image_format=mp.ImageFormat.SRGB,
114
+ data=rgb_frame
115
+ )
116
+
117
+ result = landmarker.detect_for_video(mp_image, timestamp)
118
+ timestamp += int(1000 / fps)
119
+
120
+ if background_type == "Black Background":
121
+ canvas = np.zeros((height, width, 3), dtype=np.uint8)
122
+ else: # Original Video
123
+ canvas = frame.copy()
124
+
125
+ if result.pose_landmarks:
126
+ for pose_landmarks in result.pose_landmarks:
127
+ points = []
128
+ for lm in pose_landmarks:
129
+ x = int(lm.x * width)
130
+ y = int(lm.y * height)
131
+ points.append((x, y))
132
+
133
+ if color_mode == "Single Color":
134
+ # Original behavior - single color for all connections
135
+ connections = mp.solutions.pose.POSE_CONNECTIONS
136
+ for c in connections:
137
+ cv2.line(
138
+ canvas,
139
+ points[c[0]],
140
+ points[c[1]],
141
+ line_bgr,
142
+ 3
143
+ )
144
+ else: # Multicolor
145
+ for part_name, part_connections in body_parts.items():
146
+ part_color = MULTICOLOR_SCHEME[part_name]
147
+ for c in part_connections:
148
+ if c[0] < len(points) and c[1] < len(points):
149
+ cv2.line(
150
+ canvas,
151
+ points[c[0]],
152
+ points[c[1]],
153
+ part_color,
154
+ 3
155
+ )
156
+
157
+ for p in points:
158
+ cv2.circle(canvas, p, 5, joint_bgr, -1)
159
+
160
+ out.write(canvas)
161
+
162
+ cap.release()
163
+ out.release()
164
+
165
+ return output_path
166
+
167
+ custom_css = """
168
+ * {
169
+ font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
170
+ }
171
+
172
+ .main-header {
173
+ text-align: center;
174
+ padding: 30px 20px;
175
+ border-bottom: 1px solid #404040;
176
+ margin-bottom: 30px;
177
+ }
178
+
179
+ .main-header h1 {
180
+ font-size: 28px;
181
+ font-weight: 600;
182
+ color: #ffffff;
183
+ margin: 0 0 8px 0;
184
+ letter-spacing: -0.5px;
185
+ }
186
+
187
+ .main-header p {
188
+ font-size: 15px;
189
+ color: #b0b0b0;
190
+ margin: 0;
191
+ font-weight: 400;
192
+ }
193
+
194
+ .section-header {
195
+ font-size: 13px;
196
+ font-weight: 600;
197
+ color: #ffffff;
198
+ text-transform: uppercase;
199
+ letter-spacing: 0.5px;
200
+ margin-bottom: 16px;
201
+ padding-bottom: 8px;
202
+ border-bottom: 2px solid #ffffff;
203
+ }
204
+
205
+ .subsection-title {
206
+ font-size: 12px;
207
+ font-weight: 600;
208
+ color: #b0b0b0;
209
+ text-transform: uppercase;
210
+ letter-spacing: 0.3px;
211
+ margin: 20px 0 12px 0;
212
+ }
213
+
214
+ .warning-box {
215
+ background: #1a1a1a;
216
+ border-left: 3px solid #888;
217
+ padding: 16px 20px;
218
+ margin: 20px 0;
219
+ font-size: 14px;
220
+ color: #d0d0d0;
221
+ line-height: 1.6;
222
+ }
223
+
224
+ .warning-box strong {
225
+ font-weight: 600;
226
+ color: #ffffff;
227
+ }
228
+
229
+ .info-box {
230
+ background: #1a1a1a;
231
+ border: 1px solid #404040;
232
+ padding: 16px;
233
+ border-radius: 4px;
234
+ margin-top: 16px;
235
+ }
236
+
237
+ .info-box p {
238
+ margin: 6px 0;
239
+ font-size: 13px;
240
+ color: #b0b0b0;
241
+ line-height: 1.5;
242
+ }
243
+
244
+ .info-box strong {
245
+ color: #ffffff;
246
+ }
247
+
248
+ .footer {
249
+ text-align: center;
250
+ padding: 30px 20px;
251
+ border-top: 1px solid #404040;
252
+ margin-top: 40px;
253
+ color: #888;
254
+ font-size: 13px;
255
+ }
256
+
257
+ .footer h3 {
258
+ font-size: 14px;
259
+ font-weight: 600;
260
+ color: #ffffff;
261
+ margin-bottom: 12px;
262
+ }
263
+
264
+ .footer p {
265
+ color: #b0b0b0;
266
+ }
267
+
268
+ #submit-btn {
269
+ margin-top: 24px;
270
+ background: #ffffff;
271
+ color: #000000;
272
+ border: none;
273
+ font-weight: 500;
274
+ letter-spacing: 0.3px;
275
+ }
276
+
277
+ #submit-btn:hover {
278
+ background: #e0e0e0;
279
+ }
280
+
281
+ .gr-box {
282
+ border-radius: 4px;
283
+ }
284
+
285
+ .gr-input, .gr-dropdown, .gr-radio {
286
+ border-radius: 4px;
287
+ }
288
+
289
+ .gr-accordion {
290
+ border: 1px solid #404040;
291
+ border-radius: 4px;
292
+ }
293
+
294
+ label {
295
+ color: #d0d0d0 !important;
296
+ }
297
+
298
+ .gr-text-input, .gr-dropdown {
299
+ background: #1a1a1a;
300
+ border: 1px solid #404040;
301
+ color: #ffffff;
302
+ }
303
+
304
+ .color-legend {
305
+ background: #1a1a1a;
306
+ border: 1px solid #404040;
307
+ padding: 12px;
308
+ border-radius: 4px;
309
+ margin-top: 12px;
310
+ font-size: 12px;
311
+ }
312
+
313
+ .color-legend p {
314
+ margin: 4px 0;
315
+ color: #b0b0b0;
316
+ }
317
+
318
+ .color-item {
319
+ display: inline-block;
320
+ width: 12px;
321
+ height: 12px;
322
+ margin-right: 6px;
323
+ border-radius: 2px;
324
+ }
325
+ """
326
+
327
+ with gr.Blocks(title="MediaPipe Pose Estimation", theme=gr.themes.Default(), css=custom_css) as demo:
328
+
329
+ # Header
330
+ gr.HTML(
331
+ """
332
+ <div class="main-header">
333
+ <h1>MediaPipe Pose Estimation</h1>
334
+ <p>Advanced pose detection and visualization system</p>
335
+ </div>
336
+ """
337
+ )
338
+
339
+ with gr.Accordion("Instructions", open=False):
340
+ gr.Markdown(
341
+ """
342
+ 1. Upload a video file containing human subjects
343
+ 2. Adjust detection and tracking confidence parameters as needed
344
+ 3. Select the desired background type for the output
345
+ 4. Choose between single color or multicolor visualization
346
+ 5. If single color is selected, customize the skeleton colors
347
+ 6. Click Process Video to generate the result
348
+ """
349
+ )
350
+
351
+ with gr.Accordion("Parameter Guidelines", open=False):
352
+ gr.Markdown(
353
+ """
354
+ **Detection Confidence**
355
+
356
+ Controls the minimum confidence threshold for initial pose detection. Higher values (0.7-0.9) reduce false positives but may miss some poses. Lower values (0.3-0.5) detect more poses but may include incorrect detections. Default: 0.5
357
+
358
+ **Tracking Confidence**
359
+
360
+ Determines the reliability threshold for tracking poses across consecutive frames. Higher values provide more stable tracking but may lose poses more easily. Lower values maintain tracking longer but may be less stable. Default: 0.5
361
+
362
+ **Color Modes**
363
+
364
+ Single Color: All skeleton lines use the same color. Multicolor: Different body parts are colored differently (face, torso, arms, legs).
365
+ """
366
+ )
367
+
368
+ # Warning message
369
+ gr.HTML(
370
+ """
371
+ <div class="warning-box">
372
+ <strong>Processing Time Notice:</strong> Processing duration is proportional to video length and resolution. Videos exceeding 2 minutes or high-resolution files may require several minutes to process. Please wait while the system completes the analysis.
373
+ </div>
374
+ """
375
+ )
376
+
377
+ with gr.Row():
378
+ with gr.Column():
379
+ gr.HTML('<div class="section-header">Input</div>')
380
+
381
+ video_input = gr.Video(label="Video File")
382
+
383
+ gr.HTML('<div class="subsection-title">Confidence Parameters</div>')
384
+ detection_conf = gr.Slider(
385
+ minimum=0.0,
386
+ maximum=1.0,
387
+ value=0.5,
388
+ step=0.05,
389
+ label="Detection Confidence"
390
+ )
391
+ tracking_conf = gr.Slider(
392
+ minimum=0.0,
393
+ maximum=1.0,
394
+ value=0.5,
395
+ step=0.05,
396
+ label="Tracking Confidence"
397
+ )
398
+
399
+ gr.HTML('<div class="subsection-title">Visualization Options</div>')
400
+ background_type = gr.Radio(
401
+ choices=["Black Background", "Original Video"],
402
+ value="Black Background",
403
+ label="Background Type"
404
+ )
405
+
406
+ color_mode = gr.Radio(
407
+ choices=["Single Color", "Multicolor"],
408
+ value="Single Color",
409
+ label="Color Mode"
410
+ )
411
+
412
+ with gr.Row():
413
+ line_color = gr.Dropdown(
414
+ choices=list(COLORS.keys()),
415
+ value="White",
416
+ label="Line Color",
417
+ info="Only applies in Single Color mode"
418
+ )
419
+ joint_color = gr.Dropdown(
420
+ choices=list(COLORS.keys()),
421
+ value="Red",
422
+ label="Joint Color",
423
+ info='Single & Multicolor'
424
+ )
425
+
426
+ gr.HTML(
427
+ """
428
+ <div class="color-legend">
429
+ <p><strong>Multicolor Legend:</strong></p>
430
+ <p><span class="color-item" style="background: rgb(255, 255, 0);"></span>Torso: Yellow</p>
431
+ <p><span class="color-item" style="background: rgb(255, 0, 0);"></span>Right Arm: Red</p>
432
+ <p><span class="color-item" style="background: rgb(0, 0, 255);"></span>Left Arm: Blue</p>
433
+ <p><span class="color-item" style="background: rgb(255, 0, 255);"></span>Right Leg: Magenta</p>
434
+ <p><span class="color-item" style="background: rgb(0, 255, 0);"></span>Left Leg: Green</p>
435
+ </div>
436
+ """
437
+ )
438
+
439
+ submit_btn = gr.Button("Process Video", variant="primary", elem_id="submit-btn")
440
+
441
+ with gr.Column():
442
+ gr.HTML('<div class="section-header">Output</div>')
443
+ video_output = gr.Video(label="Processed Video")
444
+
445
+ gr.HTML(
446
+ """
447
+ <div class="info-box">
448
+ <p><strong>Output Specifications:</strong></p>
449
+ <p>Format: MP4 (H.264 encoding)</p>
450
+ <p>Resolution: Matches input resolution</p>
451
+ <p>Frame Rate: Matches input frame rate</p>
452
+ <p>Keypoints: 33 body landmarks tracked per frame</p>
453
+ </div>
454
+ """
455
+ )
456
+
457
+ # Footer
458
+ gr.HTML(
459
+ """
460
+ <div class="footer">
461
+ <h3>Technical Information</h3>
462
+ <p>This application utilizes MediaPipe Pose Landmarker for real-time pose detection and tracking.</p>
463
+ <p>The system identifies 33 anatomical keypoints and visualizes skeletal structure with customizable styling.</p>
464
+ <p style="margin-top: 16px;">Supported formats: MP4, AVI, MOV, WebM</p>
465
+ <p style="margin-top: 16px; color: #666;">Powered by Google MediaPipe</p>
466
+ </div>
467
+ """
468
+ )
469
+
470
+ submit_btn.click(
471
+ fn=draw_pose,
472
+ inputs=[
473
+ video_input,
474
+ detection_conf,
475
+ tracking_conf,
476
+ background_type,
477
+ color_mode,
478
+ line_color,
479
+ joint_color
480
+ ],
481
+ outputs=video_output
482
+ )
483
+
484
+ demo.launch(server_name="0.0.0.0", server_port=7860)
pose_landmarker.task ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5134a3aad27a58b93da0088d431f366da362b44e3ccfbe3462b3827a839011b1
3
+ size 9398198
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ mediapipe==0.10.21
2
+ opencv-python-headless==4.11.0.86
3
+ gradio
4
+ numpy==1.26.4