Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,106 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import mediapipe as mp
|
| 3 |
-
import numpy as np
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import tempfile
|
| 6 |
-
|
| 7 |
-
# Load model
|
| 8 |
-
MODEL_PATH = "hand_landmarker.task"
|
| 9 |
-
|
| 10 |
-
BaseOptions = mp.tasks.BaseOptions
|
| 11 |
-
HandLandmarker = mp.tasks.vision.HandLandmarker
|
| 12 |
-
HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
|
| 13 |
-
VisionRunningMode = mp.tasks.vision.RunningMode
|
| 14 |
-
mp_image = mp.Image
|
| 15 |
-
mp_format = mp.ImageFormat
|
| 16 |
-
|
| 17 |
-
# Finger connections and colors
|
| 18 |
-
HAND_CONNECTIONS = [
|
| 19 |
-
(0, 1), (1, 2), (2, 3), (3, 4),
|
| 20 |
-
(0, 5), (5, 6), (6, 7), (7, 8),
|
| 21 |
-
(0, 9), (9,10), (10,11), (11,12),
|
| 22 |
-
(0,13), (13,14), (14,15), (15,16),
|
| 23 |
-
(0,17), (17,18), (18,19), (19,20)
|
| 24 |
-
]
|
| 25 |
-
|
| 26 |
-
FINGER_COLORS = {
|
| 27 |
-
'thumb': (245, 245, 245),
|
| 28 |
-
'index': (128, 0, 128),
|
| 29 |
-
'middle': (0, 255, 0),
|
| 30 |
-
'ring': (0, 165, 255),
|
| 31 |
-
'pinky': (255, 0, 0),
|
| 32 |
-
'palm': (100, 100, 100)
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
def get_finger_color(start_idx):
|
| 36 |
-
if start_idx in range(0, 5):
|
| 37 |
-
return FINGER_COLORS['thumb']
|
| 38 |
-
elif start_idx in range(5, 9):
|
| 39 |
-
return FINGER_COLORS['index']
|
| 40 |
-
elif start_idx in range(9, 13):
|
| 41 |
-
return FINGER_COLORS['middle']
|
| 42 |
-
elif start_idx in range(13, 17):
|
| 43 |
-
return FINGER_COLORS['ring']
|
| 44 |
-
elif start_idx in range(17, 21):
|
| 45 |
-
return FINGER_COLORS['pinky']
|
| 46 |
-
else:
|
| 47 |
-
return FINGER_COLORS['palm']
|
| 48 |
-
|
| 49 |
-
def process_video(video_path):
|
| 50 |
-
cap = cv2.VideoCapture(video_path)
|
| 51 |
-
|
| 52 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 53 |
-
tmp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 54 |
-
out_path = tmp_out.name
|
| 55 |
-
|
| 56 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 57 |
-
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 58 |
-
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 59 |
-
out = cv2.VideoWriter(out_path, fourcc, fps, (w, h))
|
| 60 |
-
|
| 61 |
-
options = HandLandmarkerOptions(
|
| 62 |
-
base_options=BaseOptions(model_asset_path=MODEL_PATH),
|
| 63 |
-
running_mode=VisionRunningMode.IMAGE,
|
| 64 |
-
num_hands=2,
|
| 65 |
-
min_hand_detection_confidence=0.5,
|
| 66 |
-
min_hand_presence_confidence=0.5,
|
| 67 |
-
min_tracking_confidence=0.5
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
with HandLandmarker.create_from_options(options) as landmarker:
|
| 71 |
-
while cap.isOpened():
|
| 72 |
-
ret, frame = cap.read()
|
| 73 |
-
if not ret:
|
| 74 |
-
break
|
| 75 |
-
|
| 76 |
-
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 77 |
-
mp_img = mp_image(image_format=mp_format.SRGB, data=rgb_frame)
|
| 78 |
-
results = landmarker.detect(mp_img)
|
| 79 |
-
|
| 80 |
-
if results.hand_landmarks:
|
| 81 |
-
for hand_landmarks in results.hand_landmarks:
|
| 82 |
-
points = [(int(lm.x * w), int(lm.y * h)) for lm in hand_landmarks]
|
| 83 |
-
|
| 84 |
-
for start, end in HAND_CONNECTIONS:
|
| 85 |
-
color = get_finger_color(start)
|
| 86 |
-
cv2.line(frame, points[start], points[end], color, 2)
|
| 87 |
-
|
| 88 |
-
for i, (x, y) in enumerate(points):
|
| 89 |
-
cv2.circle(frame, (x, y), 4, (0, 255, 255), -1)
|
| 90 |
-
|
| 91 |
-
out.write(frame)
|
| 92 |
-
|
| 93 |
-
cap.release()
|
| 94 |
-
out.release()
|
| 95 |
-
return out_path
|
| 96 |
-
|
| 97 |
-
# Gradio interface
|
| 98 |
-
demo = gr.Interface(
|
| 99 |
-
fn=process_video,
|
| 100 |
-
inputs=gr.Video(label="Upload Video or Use Webcam"),
|
| 101 |
-
outputs=gr.Video(label="Hand Landmark Annotated Video"),
|
| 102 |
-
title="Hand Detection ",
|
| 103 |
-
description="Upload a video or use webcam to detect hands."
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|