Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Load TensorFlow Lite model
|
| 8 |
interpreter = tf.lite.Interpreter(model_path="facenet.tflite")
|
|
@@ -12,22 +16,22 @@ interpreter.allocate_tensors()
|
|
| 12 |
input_details = interpreter.get_input_details()
|
| 13 |
output_details = interpreter.get_output_details()
|
| 14 |
|
| 15 |
-
def preprocess_image(
|
| 16 |
"""
|
| 17 |
Preprocess the input image for the FaceNet model.
|
| 18 |
"""
|
| 19 |
-
image = 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(
|
| 27 |
"""
|
| 28 |
-
Generate a face embedding for the given image.
|
| 29 |
"""
|
| 30 |
-
processed_image = preprocess_image(
|
| 31 |
|
| 32 |
# Run the model
|
| 33 |
interpreter.set_tensor(input_details[0]['index'], processed_image)
|
|
@@ -35,27 +39,31 @@ def create_face_embedding(image):
|
|
| 35 |
|
| 36 |
# Extract the embedding
|
| 37 |
embedding = interpreter.get_tensor(output_details[0]['index'])
|
| 38 |
-
return embedding.flatten()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
def generate_embedding(
|
| 42 |
"""
|
| 43 |
-
|
| 44 |
"""
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
return
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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)
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import tensorflow as tf
|
| 3 |
+
from flask import Flask, request, jsonify
|
| 4 |
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
|
| 11 |
# Load TensorFlow Lite model
|
| 12 |
interpreter = tf.lite.Interpreter(model_path="facenet.tflite")
|
|
|
|
| 16 |
input_details = interpreter.get_input_details()
|
| 17 |
output_details = interpreter.get_output_details()
|
| 18 |
|
| 19 |
+
def preprocess_image(image_data):
|
| 20 |
"""
|
| 21 |
Preprocess the input image for the FaceNet model.
|
| 22 |
"""
|
| 23 |
+
image = Image.open(io.BytesIO(image_data)).convert('RGB')
|
| 24 |
image = image.resize((160, 160)) # Resize to the model's input size
|
| 25 |
image_array = np.asarray(image).astype(np.float32)
|
| 26 |
image_array = (image_array - 127.5) / 127.5 # Normalize to [-1, 1]
|
| 27 |
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 28 |
return image_array
|
| 29 |
|
| 30 |
+
def create_face_embedding(image_data):
|
| 31 |
"""
|
| 32 |
+
Generate a face embedding for the given image data.
|
| 33 |
"""
|
| 34 |
+
processed_image = preprocess_image(image_data)
|
| 35 |
|
| 36 |
# Run the model
|
| 37 |
interpreter.set_tensor(input_details[0]['index'], processed_image)
|
|
|
|
| 39 |
|
| 40 |
# Extract the embedding
|
| 41 |
embedding = interpreter.get_tensor(output_details[0]['index'])
|
| 42 |
+
return embedding.flatten().tolist()
|
| 43 |
|
| 44 |
+
@app.route('/generate-embedding', methods=['POST'])
|
| 45 |
+
def generate_embedding():
|
| 46 |
"""
|
| 47 |
+
Endpoint to process an image and return its face embedding.
|
| 48 |
"""
|
| 49 |
try:
|
| 50 |
+
# Parse incoming JSON with base64-encoded image
|
| 51 |
+
data = request.json
|
| 52 |
+
if "image" not in data:
|
| 53 |
+
return jsonify({"error": "Image data not provided"}), 400
|
| 54 |
+
|
| 55 |
+
# Decode base64 image
|
| 56 |
+
image_data = base64.b64decode(data["image"])
|
| 57 |
+
|
| 58 |
+
# Generate embedding
|
| 59 |
+
embedding = create_face_embedding(image_data)
|
| 60 |
+
return jsonify({"embedding": embedding}), 200
|
| 61 |
except Exception as e:
|
| 62 |
+
return jsonify({"error": str(e)}), 500
|
| 63 |
+
|
| 64 |
+
@app.route('/')
|
| 65 |
+
def home():
|
| 66 |
+
return "Face Embedding Generator API is running!"
|
| 67 |
+
|
| 68 |
+
if __name__ == '__main__':
|
| 69 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|