Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import base64
|
| 3 |
+
import numpy as np
|
| 4 |
+
import face_recognition
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
@spaces.GPU
|
| 9 |
+
def get_face_embedding(base64_image):
|
| 10 |
+
# Decode the base64 image
|
| 11 |
+
img_data = base64.b64decode(base64_image)
|
| 12 |
+
np_arr = np.frombuffer(img_data, np.uint8)
|
| 13 |
+
image = face_recognition.load_image_file(BytesIO(img_data))
|
| 14 |
+
|
| 15 |
+
# Get the face encodings for all faces in the image
|
| 16 |
+
face_encodings = face_recognition.face_encodings(image)
|
| 17 |
+
|
| 18 |
+
# If no faces are detected, return an empty list
|
| 19 |
+
if not face_encodings:
|
| 20 |
+
return []
|
| 21 |
+
|
| 22 |
+
# Return the first face encoding as a list
|
| 23 |
+
return face_encodings[0].tolist()
|
| 24 |
+
|
| 25 |
+
# Define the Gradio interface
|
| 26 |
+
interface = gr.Interface(
|
| 27 |
+
fn=get_face_embedding,
|
| 28 |
+
inputs="text",
|
| 29 |
+
outputs="json",
|
| 30 |
+
title="Face Embedding Extractor",
|
| 31 |
+
description="Input a base64 encoded image to get a 128-dimensional face embedding vector. If no face is detected, an empty list is returned."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
interface.launch(share=True)
|