osamaifti commited on
Commit
b83e535
·
1 Parent(s): a6f78ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -1,35 +1,37 @@
1
  import gradio as gr
2
  import torch
3
- import torchvision
4
- import transformers
5
  import numpy as np
6
  from scipy.spatial.distance import cosine
7
- import cv2
8
- import os
9
-
10
 
 
11
  RECOGNITION_THRESHOLD = 0.3
12
 
13
-
14
- # Load the embedding model
15
- embedding_model = torch.load('final_modelnew.pth', map_location=torch.device('cpu'))
16
- embedding_model.eval() # Set the model to evaluation mode
17
 
18
  # Database to store embeddings and user IDs
19
  user_embeddings = {}
20
 
21
  # Preprocess the image
22
  def preprocess_image(image):
23
- image = cv2.resize(image, (375, 375)) # Resize image
24
- image = image / 255.0 # Normalize pixel values
25
- image = np.transpose(image, (2, 0, 1)) # Change from HWC to CHW format
26
- return torch.tensor(image, dtype=torch.float32).unsqueeze(0) # Add batch dimension
 
 
 
27
 
28
  # Generate embedding
29
  def generate_embedding(image):
30
  preprocessed_image = preprocess_image(image)
31
  with torch.no_grad(): # No need to track gradients
32
- return embedding_model(preprocessed_image).numpy()[0]
 
33
 
34
  # Register new user
35
  def register_user(image, user_id):
@@ -49,12 +51,10 @@ def recognize_user(image):
49
 
50
  for user_id, embedding in user_embeddings.items():
51
  distance = cosine(new_embedding, embedding)
52
- print(f"Distance for {user_id}: {distance}") # Debug: Print distances
53
  if distance < min_distance:
54
  min_distance = distance
55
  recognized_user_id = user_id
56
-
57
- print(f"Min distance: {min_distance}") # Debug: Print minimum distance
58
  if min_distance > RECOGNITION_THRESHOLD:
59
  return "User not recognized."
60
  else:
@@ -62,11 +62,10 @@ def recognize_user(image):
62
  except Exception as e:
63
  return f"Error during recognition: {str(e)}"
64
 
65
-
66
  def main():
67
  with gr.Blocks() as demo:
68
  gr.Markdown("Facial Recognition System")
69
-
70
  with gr.Tab("Register"):
71
  with gr.Row():
72
  img_register = gr.Image()
@@ -85,4 +84,4 @@ def main():
85
  demo.launch(share=True)
86
 
87
  if __name__ == "__main__":
88
- main()
 
1
  import gradio as gr
2
  import torch
3
+ from torchvision import transforms
4
+ from PIL import Image
5
  import numpy as np
6
  from scipy.spatial.distance import cosine
 
 
 
7
 
8
+ # Constants
9
  RECOGNITION_THRESHOLD = 0.3
10
 
11
+ # Load the model
12
+ model_path = 'final_modelnew.pth'
13
+ model = torch.load(model_path, map_location=torch.device('cpu'))
14
+ model.eval() # Set the model to evaluation mode
15
 
16
  # Database to store embeddings and user IDs
17
  user_embeddings = {}
18
 
19
  # Preprocess the image
20
  def preprocess_image(image):
21
+ transform = transforms.Compose([
22
+ transforms.Resize((224, 224)),
23
+ transforms.ToTensor(),
24
+ ])
25
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
26
+ image = transform(image).unsqueeze(0)
27
+ return image
28
 
29
  # Generate embedding
30
  def generate_embedding(image):
31
  preprocessed_image = preprocess_image(image)
32
  with torch.no_grad(): # No need to track gradients
33
+ embedding = model(preprocessed_image)
34
+ return embedding.numpy()[0]
35
 
36
  # Register new user
37
  def register_user(image, user_id):
 
51
 
52
  for user_id, embedding in user_embeddings.items():
53
  distance = cosine(new_embedding, embedding)
 
54
  if distance < min_distance:
55
  min_distance = distance
56
  recognized_user_id = user_id
57
+
 
58
  if min_distance > RECOGNITION_THRESHOLD:
59
  return "User not recognized."
60
  else:
 
62
  except Exception as e:
63
  return f"Error during recognition: {str(e)}"
64
 
 
65
  def main():
66
  with gr.Blocks() as demo:
67
  gr.Markdown("Facial Recognition System")
68
+
69
  with gr.Tab("Register"):
70
  with gr.Row():
71
  img_register = gr.Image()
 
84
  demo.launch(share=True)
85
 
86
  if __name__ == "__main__":
87
+ main()