Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from scipy.spatial.distance import cosine
|
| 5 |
+
import cv2
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
RECOGNITION_THRESHOLD = 0.3
|
| 9 |
+
|
| 10 |
+
# Load the embedding model
|
| 11 |
+
embedding_model = tf.keras.models.load_model('full_mode2.pth')
|
| 12 |
+
|
| 13 |
+
# Database to store embeddings and user IDs
|
| 14 |
+
user_embeddings = {}
|
| 15 |
+
|
| 16 |
+
# Preprocess the image
|
| 17 |
+
def preprocess_image(image):
|
| 18 |
+
image = cv2.resize(image, (375, 375)) # Resize image
|
| 19 |
+
image = tf.keras.applications.resnet50.preprocess_input(image)
|
| 20 |
+
return np.expand_dims(image, axis=0)
|
| 21 |
+
|
| 22 |
+
# Generate embedding
|
| 23 |
+
def generate_embedding(image):
|
| 24 |
+
preprocessed_image = preprocess_image(image)
|
| 25 |
+
return embedding_model.predict(preprocessed_image)[0]
|
| 26 |
+
|
| 27 |
+
# Register new user
|
| 28 |
+
def register_user(image, user_id):
|
| 29 |
+
try:
|
| 30 |
+
embedding = generate_embedding(image)
|
| 31 |
+
user_embeddings[user_id] = embedding
|
| 32 |
+
return f"User {user_id} registered successfully."
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error during registration: {str(e)}"
|
| 35 |
+
|
| 36 |
+
# Recognize user
|
| 37 |
+
def recognize_user(image):
|
| 38 |
+
try:
|
| 39 |
+
new_embedding = generate_embedding(image)
|
| 40 |
+
min_distance = float('inf')
|
| 41 |
+
recognized_user_id = "Unknown"
|
| 42 |
+
|
| 43 |
+
for user_id, embedding in user_embeddings.items():
|
| 44 |
+
distance = cosine(new_embedding, embedding)
|
| 45 |
+
print(f"Distance for {user_id}: {distance}") # Debug: Print distances
|
| 46 |
+
if distance < min_distance:
|
| 47 |
+
min_distance = distance
|
| 48 |
+
recognized_user_id = user_id
|
| 49 |
+
|
| 50 |
+
print(f"Min distance: {min_distance}") # Debug: Print minimum distance
|
| 51 |
+
if min_distance > RECOGNITION_THRESHOLD:
|
| 52 |
+
return "User not recognized."
|
| 53 |
+
else:
|
| 54 |
+
return f"Recognized User: {recognized_user_id}"
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error during recognition: {str(e)}"
|
| 57 |
+
|
| 58 |
+
def main():
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("Facial Recognition System")
|
| 61 |
+
|
| 62 |
+
with gr.Tab("Register"):
|
| 63 |
+
with gr.Row():
|
| 64 |
+
img_register = gr.Image()
|
| 65 |
+
user_id = gr.Textbox(label="User ID")
|
| 66 |
+
register_button = gr.Button("Register")
|
| 67 |
+
register_output = gr.Textbox()
|
| 68 |
+
register_button.click(register_user, inputs=[img_register, user_id], outputs=register_output)
|
| 69 |
+
|
| 70 |
+
with gr.Tab("Recognize"):
|
| 71 |
+
with gr.Row():
|
| 72 |
+
img_recognize = gr.Image()
|
| 73 |
+
recognize_button = gr.Button("Recognize")
|
| 74 |
+
recognize_output = gr.Textbox()
|
| 75 |
+
recognize_button.click(recognize_user, inputs=[img_recognize], outputs=recognize_output)
|
| 76 |
+
|
| 77 |
+
demo.launch(share=True)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|