Spaces:
Runtime error
Runtime error
ibaiGorordo
commited on
Commit
•
f7971e3
1
Parent(s):
ec458e0
initial commit
Browse files- README.md +4 -4
- app.py +58 -0
- images/hugging-face.png +0 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
---
|
2 |
-
title: Hugging Face
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
8 |
pinned: false
|
|
|
1 |
---
|
2 |
+
title: Hugging Face Me
|
3 |
+
emoji: 🤗
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
app_file: app.py
|
8 |
pinned: false
|
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import mediapipe as mp
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
|
6 |
+
title = "Hugging Face Me"
|
7 |
+
description = "demo for overlaying the Hugging Face logo on your face using the Mediapipe Face Detection model."
|
8 |
+
article = "<p style='text-align: center'><a href='https://google.github.io/mediapipe/solutions/face_detection.html target='_blank'>Mediapipe Face Detection</a> | <a href='https://github.com/google/mediapipe' target='_blank'>Github Repo</a></p>"
|
9 |
+
|
10 |
+
mp_face_detection = mp.solutions.face_detection
|
11 |
+
mp_drawing = mp.solutions.drawing_utils
|
12 |
+
|
13 |
+
def draw_huggingfaces(image, results):
|
14 |
+
|
15 |
+
height, width, _ = image.shape
|
16 |
+
|
17 |
+
output_img = image.copy()
|
18 |
+
if results.detections:
|
19 |
+
for detection in results.detections:
|
20 |
+
face_coordinates = np.array([[detection.location_data.relative_keypoints[i].x*width,
|
21 |
+
detection.location_data.relative_keypoints[i].y*height]
|
22 |
+
for i in [0,1,3]], dtype=np.float32)
|
23 |
+
|
24 |
+
M = cv2.getAffineTransform(huggingface_landmarks, face_coordinates)
|
25 |
+
transformed_huggingface = cv2.warpAffine(huggingface_image, M, (width, height))
|
26 |
+
transformed_huggingface_mask = transformed_huggingface[:,:,3] != 0
|
27 |
+
output_img[transformed_huggingface_mask] = transformed_huggingface[transformed_huggingface_mask,:3]
|
28 |
+
|
29 |
+
return output_img
|
30 |
+
|
31 |
+
|
32 |
+
def huggingface_me(image):
|
33 |
+
|
34 |
+
with mp_face_detection.FaceDetection(
|
35 |
+
model_selection=0,
|
36 |
+
min_detection_confidence=0.5) as face_detection:
|
37 |
+
|
38 |
+
# Convert the BGR image to RGB and process it with MediaPipe Face Mesh.
|
39 |
+
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
40 |
+
|
41 |
+
return draw_huggingfaces(image, results)
|
42 |
+
|
43 |
+
# Load hugging face logo and landmark coordinates
|
44 |
+
huggingface_image = cv2.imread("images/hugging-face.png", cv2.IMREAD_UNCHANGED)
|
45 |
+
huggingface_image = cv2.cvtColor(huggingface_image, cv2.COLOR_BGRA2RGBA)
|
46 |
+
huggingface_landmarks = np.array([[747,697],[1289,697],[1022,1116]], dtype=np.float32)
|
47 |
+
|
48 |
+
webcam_image = gr.inputs.Image(label="Input Image", source="webcam")
|
49 |
+
output_image = gr.outputs.Image(label="Output Image")
|
50 |
+
|
51 |
+
gr.Interface(huggingface_me,
|
52 |
+
live=True,
|
53 |
+
inputs=webcam_image,
|
54 |
+
outputs=output_image,
|
55 |
+
title=title,
|
56 |
+
description=description,
|
57 |
+
article=article, ).launch()
|
58 |
+
|
images/hugging-face.png
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
mediapipe
|
2 |
+
numpy
|
3 |
+
opencv-python-headless
|