Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Initialize Mediapipe solutions
|
| 7 |
+
mp_hands = mp.solutions.hands
|
| 8 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 9 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 10 |
+
|
| 11 |
+
# Function to process video frames and draw landmarks
|
| 12 |
+
def process_video(input_video):
|
| 13 |
+
# Create a VideoCapture object from the input video
|
| 14 |
+
cap = cv2.VideoCapture(input_video)
|
| 15 |
+
processed_frames = []
|
| 16 |
+
|
| 17 |
+
# Initialize Hands and Face Mesh models
|
| 18 |
+
with mp_hands.Hands(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands, \
|
| 19 |
+
mp_face_mesh.FaceMesh(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh:
|
| 20 |
+
|
| 21 |
+
while True:
|
| 22 |
+
ret, frame = cap.read()
|
| 23 |
+
if not ret:
|
| 24 |
+
break
|
| 25 |
+
|
| 26 |
+
# Convert frame to RGB for Mediapipe
|
| 27 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 28 |
+
|
| 29 |
+
# Process the frame for hands
|
| 30 |
+
hand_results = hands.process(rgb_frame)
|
| 31 |
+
# Process the frame for face mesh
|
| 32 |
+
face_results = face_mesh.process(rgb_frame)
|
| 33 |
+
|
| 34 |
+
# Draw Hand Landmarks
|
| 35 |
+
if hand_results.multi_hand_landmarks:
|
| 36 |
+
for hand_landmarks in hand_results.multi_hand_landmarks:
|
| 37 |
+
mp_drawing.draw_landmarks(
|
| 38 |
+
frame, hand_landmarks, mp_hands.HAND_CONNECTIONS,
|
| 39 |
+
mp_drawing.DrawingSpec(color=(121, 22, 76), thickness=2, circle_radius=4),
|
| 40 |
+
mp_drawing.DrawingSpec(color=(250, 44, 250), thickness=2, circle_radius=2)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Draw Face Mesh Landmarks
|
| 44 |
+
if face_results.multi_face_landmarks:
|
| 45 |
+
for face_landmarks in face_results.multi_face_landmarks:
|
| 46 |
+
mp_drawing.draw_landmarks(
|
| 47 |
+
frame, face_landmarks, mp_face_mesh.FACEMESH_TESSELATION,
|
| 48 |
+
mp_drawing.DrawingSpec(color=(80, 110, 10), thickness=1, circle_radius=1),
|
| 49 |
+
mp_drawing.DrawingSpec(color=(80, 256, 121), thickness=1, circle_radius=1)
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Store processed frame
|
| 53 |
+
processed_frames.append(frame)
|
| 54 |
+
|
| 55 |
+
cap.release()
|
| 56 |
+
|
| 57 |
+
# Compile processed frames into a video
|
| 58 |
+
height, width, _ = processed_frames[0].shape
|
| 59 |
+
output_video_path = "processed_video.mp4"
|
| 60 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 61 |
+
out = cv2.VideoWriter(output_video_path, fourcc, 30.0, (width, height))
|
| 62 |
+
for frame in processed_frames:
|
| 63 |
+
out.write(frame)
|
| 64 |
+
out.release()
|
| 65 |
+
|
| 66 |
+
return output_video_path
|
| 67 |
+
|
| 68 |
+
# Define Gradio interface
|
| 69 |
+
def gradio_video_interface(input_video):
|
| 70 |
+
# Process the video to detect landmarks
|
| 71 |
+
output_video = process_video(input_video)
|
| 72 |
+
return output_video
|
| 73 |
+
|
| 74 |
+
# Gradio interface for video input and output
|
| 75 |
+
iface = gr.Interface(
|
| 76 |
+
fn=gradio_video_interface,
|
| 77 |
+
inputs=gr.Video(type="file"),
|
| 78 |
+
outputs=gr.Video(type="file"),
|
| 79 |
+
title="Face and Hand Landmarks Detection - Video",
|
| 80 |
+
description="Upload a video to detect face and hand landmarks using Mediapipe and OpenCV."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Launch Gradio app
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
iface.launch()
|