Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import numpy as np
|
| 4 |
from scipy.spatial.distance import cosine
|
| 5 |
import cv2
|
|
@@ -7,8 +7,10 @@ import os
|
|
| 7 |
|
| 8 |
RECOGNITION_THRESHOLD = 0.3
|
| 9 |
|
|
|
|
| 10 |
# Load the embedding model
|
| 11 |
-
embedding_model =
|
|
|
|
| 12 |
|
| 13 |
# Database to store embeddings and user IDs
|
| 14 |
user_embeddings = {}
|
|
@@ -16,13 +18,15 @@ user_embeddings = {}
|
|
| 16 |
# Preprocess the image
|
| 17 |
def preprocess_image(image):
|
| 18 |
image = cv2.resize(image, (375, 375)) # Resize image
|
| 19 |
-
image =
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
# Generate embedding
|
| 23 |
def generate_embedding(image):
|
| 24 |
preprocessed_image = preprocess_image(image)
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
# Register new user
|
| 28 |
def register_user(image, user_id):
|
|
@@ -55,6 +59,10 @@ def recognize_user(image):
|
|
| 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")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import numpy as np
|
| 4 |
from scipy.spatial.distance import cosine
|
| 5 |
import cv2
|
|
|
|
| 7 |
|
| 8 |
RECOGNITION_THRESHOLD = 0.3
|
| 9 |
|
| 10 |
+
# Assuming the PyTorch model is a ResNet (or similar) and has been trained accordingly
|
| 11 |
# Load the embedding model
|
| 12 |
+
embedding_model = torch.load('full_mode2.pth')
|
| 13 |
+
embedding_model.eval() # Set the model to evaluation mode
|
| 14 |
|
| 15 |
# Database to store embeddings and user IDs
|
| 16 |
user_embeddings = {}
|
|
|
|
| 18 |
# Preprocess the image
|
| 19 |
def preprocess_image(image):
|
| 20 |
image = cv2.resize(image, (375, 375)) # Resize image
|
| 21 |
+
image = image / 255.0 # Normalize pixel values
|
| 22 |
+
image = np.transpose(image, (2, 0, 1)) # Change from HWC to CHW format
|
| 23 |
+
return torch.tensor(image, dtype=torch.float32).unsqueeze(0) # Add batch dimension
|
| 24 |
|
| 25 |
# Generate embedding
|
| 26 |
def generate_embedding(image):
|
| 27 |
preprocessed_image = preprocess_image(image)
|
| 28 |
+
with torch.no_grad(): # No need to track gradients
|
| 29 |
+
return embedding_model(preprocessed_image).numpy()[0]
|
| 30 |
|
| 31 |
# Register new user
|
| 32 |
def register_user(image, user_id):
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
return f"Error during recognition: {str(e)}"
|
| 61 |
|
| 62 |
+
def main():
|
| 63 |
+
# ... (rest of your Gradio setup code)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
def main():
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("Facial Recognition System")
|