Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,61 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
import tensorflow as tf
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import cv2
|
| 6 |
-
|
| 7 |
-
# Load TensorFlow Lite model
|
| 8 |
-
interpreter = tf.lite.Interpreter(model_path="facenet.tflite")
|
| 9 |
-
interpreter.allocate_tensors()
|
| 10 |
-
|
| 11 |
-
# Get input and output details
|
| 12 |
-
input_details = interpreter.get_input_details()
|
| 13 |
-
output_details = interpreter.get_output_details()
|
| 14 |
-
|
| 15 |
-
def preprocess_image(image):
|
| 16 |
-
"""
|
| 17 |
-
Preprocess the input image for the FaceNet model.
|
| 18 |
-
"""
|
| 19 |
-
image = Image.fromarray(image)
|
| 20 |
-
image = image.resize((160, 160)) # Resize to the model's input size
|
| 21 |
-
image_array = np.asarray(image).astype(np.float32)
|
| 22 |
-
image_array = (image_array - 127.5) / 127.5 # Normalize to [-1, 1]
|
| 23 |
-
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 24 |
-
return image_array
|
| 25 |
-
|
| 26 |
-
def create_face_embedding(image):
|
| 27 |
-
"""
|
| 28 |
-
Generate a face embedding for the given image.
|
| 29 |
-
"""
|
| 30 |
-
processed_image = preprocess_image(image)
|
| 31 |
-
|
| 32 |
-
# Run the model
|
| 33 |
-
interpreter.set_tensor(input_details[0]['index'], processed_image)
|
| 34 |
-
interpreter.invoke()
|
| 35 |
-
|
| 36 |
-
# Extract the embedding
|
| 37 |
-
embedding = interpreter.get_tensor(output_details[0]['index'])
|
| 38 |
-
return embedding.flatten()
|
| 39 |
-
|
| 40 |
-
# Gradio interface
|
| 41 |
-
def generate_embedding(image):
|
| 42 |
-
"""
|
| 43 |
-
Gradio function to process the image and return full embeddings.
|
| 44 |
-
"""
|
| 45 |
-
try:
|
| 46 |
-
embedding = create_face_embedding(image)
|
| 47 |
-
return embedding.tolist() # Convert numpy array to list
|
| 48 |
-
except Exception as e:
|
| 49 |
-
return f"Error: {e}"
|
| 50 |
-
|
| 51 |
-
# Gradio interface setup
|
| 52 |
-
iface = gr.Interface(
|
| 53 |
-
fn=generate_embedding,
|
| 54 |
-
inputs=gr.Image(type="numpy", label="Upload Face Image"),
|
| 55 |
-
outputs=gr.JSON(label="Face Embedding"),
|
| 56 |
-
title="Face Embedding Generator",
|
| 57 |
-
description="Upload a face image to generate a 512-dimensional embedding using the FaceNet model."
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
if __name__ == "__main__":
|
| 61 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load TensorFlow Lite model
|
| 8 |
+
interpreter = tf.lite.Interpreter(model_path="facenet.tflite")
|
| 9 |
+
interpreter.allocate_tensors()
|
| 10 |
+
|
| 11 |
+
# Get input and output details
|
| 12 |
+
input_details = interpreter.get_input_details()
|
| 13 |
+
output_details = interpreter.get_output_details()
|
| 14 |
+
|
| 15 |
+
def preprocess_image(image):
|
| 16 |
+
"""
|
| 17 |
+
Preprocess the input image for the FaceNet model.
|
| 18 |
+
"""
|
| 19 |
+
image = Image.fromarray(image)
|
| 20 |
+
image = image.resize((160, 160)) # Resize to the model's input size
|
| 21 |
+
image_array = np.asarray(image).astype(np.float32)
|
| 22 |
+
image_array = (image_array - 127.5) / 127.5 # Normalize to [-1, 1]
|
| 23 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 24 |
+
return image_array
|
| 25 |
+
|
| 26 |
+
def create_face_embedding(image):
|
| 27 |
+
"""
|
| 28 |
+
Generate a face embedding for the given image.
|
| 29 |
+
"""
|
| 30 |
+
processed_image = preprocess_image(image)
|
| 31 |
+
|
| 32 |
+
# Run the model
|
| 33 |
+
interpreter.set_tensor(input_details[0]['index'], processed_image)
|
| 34 |
+
interpreter.invoke()
|
| 35 |
+
|
| 36 |
+
# Extract the embedding
|
| 37 |
+
embedding = interpreter.get_tensor(output_details[0]['index'])
|
| 38 |
+
return embedding.flatten()
|
| 39 |
+
|
| 40 |
+
# Gradio interface
|
| 41 |
+
def generate_embedding(image):
|
| 42 |
+
"""
|
| 43 |
+
Gradio function to process the image and return full embeddings.
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
embedding = create_face_embedding(image)
|
| 47 |
+
return embedding.tolist() # Convert numpy array to list
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error: {e}"
|
| 50 |
+
|
| 51 |
+
# Gradio interface setup
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=generate_embedding,
|
| 54 |
+
inputs=gr.Image(type="numpy", label="Upload Face Image"),
|
| 55 |
+
outputs=gr.JSON(label="Face Embedding"),
|
| 56 |
+
title="Face Embedding Generator",
|
| 57 |
+
description="Upload a face image to generate a 512-dimensional embedding using the FaceNet model."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch(share=True)
|