Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,93 @@
|
|
|
|
|
| 1 |
import tensorflow as tf
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
an_distance = tf.reduce_sum(tf.square(anchor - negative), -1)
|
| 13 |
-
return (ap_distance, an_distance)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import os
|
| 6 |
+
from scipy.spatial.distance import cosine
|
| 7 |
+
from keras_facenet import FaceNet
|
| 8 |
|
| 9 |
+
# Load the FaceNet model
|
| 10 |
+
def load_facenet_model():
|
| 11 |
+
facenet = FaceNet()
|
| 12 |
+
model = facenet.model # Access the Keras model in FaceNet
|
| 13 |
+
return model
|
| 14 |
|
| 15 |
+
embedding_model = load_facenet_model()
|
| 16 |
+
embedding_model.load_weights('facenet_embedding.h5')
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Database to store embeddings and user IDs
|
| 19 |
+
user_embeddings = []
|
| 20 |
+
user_ids = []
|
| 21 |
|
| 22 |
+
# Threshold
|
| 23 |
+
RECOGNITION_THRESHOLD = 0.1 # Adjust as needed
|
| 24 |
+
|
| 25 |
+
# Preprocess the image for FaceNet
|
| 26 |
+
def preprocess_image(image):
|
| 27 |
+
image = cv2.resize(image, (160, 160)) # Resize image to 160x160 for FaceNet
|
| 28 |
+
image = image.astype('float32')
|
| 29 |
+
mean, std = image.mean(), image.std()
|
| 30 |
+
image = (image - mean) / std
|
| 31 |
+
return np.expand_dims(image, axis=0)
|
| 32 |
+
|
| 33 |
+
# Generate embedding using FaceNet
|
| 34 |
+
def generate_embedding(image):
|
| 35 |
+
preprocessed_image = preprocess_image(image)
|
| 36 |
+
return embedding_model.predict(preprocessed_image)[0]
|
| 37 |
+
|
| 38 |
+
# Register new user
|
| 39 |
+
def register_user(image, user_id):
|
| 40 |
+
try:
|
| 41 |
+
embedding = generate_embedding(image)
|
| 42 |
+
user_embeddings.append(embedding)
|
| 43 |
+
user_ids.append(user_id)
|
| 44 |
+
return f"User {user_id} registered successfully."
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error during registration: {str(e)}"
|
| 47 |
+
|
| 48 |
+
# Recognize user
|
| 49 |
+
def recognize_user(image):
|
| 50 |
+
try:
|
| 51 |
+
new_embedding = generate_embedding(image)
|
| 52 |
+
closest_user_id = None
|
| 53 |
+
closest_distance = float('inf')
|
| 54 |
+
|
| 55 |
+
for user_id, embedding in zip(user_ids, user_embeddings):
|
| 56 |
+
distance = cosine(new_embedding, embedding)
|
| 57 |
+
print(f"Distance for {user_id}: {distance}") # Debug: Print distances for each user
|
| 58 |
+
if distance < closest_distance:
|
| 59 |
+
closest_distance = distance
|
| 60 |
+
closest_user_id = user_id
|
| 61 |
+
|
| 62 |
+
print(f"Min distance: {closest_distance}") # Debug: Print minimum distance
|
| 63 |
+
|
| 64 |
+
if closest_distance <= RECOGNITION_THRESHOLD:
|
| 65 |
+
return f"Recognized User: {closest_user_id}"
|
| 66 |
+
else:
|
| 67 |
+
return f"User not recognized. Closest Distance: {closest_distance}"
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"Error during recognition: {str(e)}"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def main():
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("Facial Recognition System")
|
| 75 |
+
with gr.Tab("Register"):
|
| 76 |
+
with gr.Row():
|
| 77 |
+
img_register = gr.Image()
|
| 78 |
+
user_id = gr.Textbox(label="User ID")
|
| 79 |
+
register_button = gr.Button("Register")
|
| 80 |
+
register_output = gr.Textbox()
|
| 81 |
+
register_button.click(register_user, inputs=[img_register, user_id], outputs=register_output)
|
| 82 |
+
|
| 83 |
+
with gr.Tab("Recognize"):
|
| 84 |
+
with gr.Row():
|
| 85 |
+
img_recognize = gr.Image()
|
| 86 |
+
recognize_button = gr.Button("Recognize")
|
| 87 |
+
recognize_output = gr.Textbox()
|
| 88 |
+
recognize_button.click(recognize_user, inputs=[img_recognize], outputs=recognize_output)
|
| 89 |
+
|
| 90 |
+
demo.launch(share=True)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
main()
|