ImanAmran commited on
Commit
fd4b460
·
1 Parent(s): 2f38536

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Load the embedding model
9
+ embedding_model = tf.keras.models.load_model('embedding_model.h5')
10
+
11
+ # Database to store embeddings and user IDs
12
+ user_embeddings = {}
13
+
14
+ # Preprocess the image
15
+ def preprocess_image(image):
16
+ image = cv2.resize(image, (200, 200)) # Assuming your model expects 200x200 input
17
+ image = tf.keras.applications.resnet50.preprocess_input(image)
18
+ return np.expand_dims(image, axis=0)
19
+
20
+ # Generate embedding
21
+ def generate_embedding(image):
22
+ preprocessed_image = preprocess_image(image)
23
+ return embedding_model.predict(preprocessed_image)[0]
24
+
25
+ # Register new user
26
+ def register_user(image, user_id):
27
+ embedding = generate_embedding(image)
28
+ user_embeddings[user_id] = embedding
29
+ return f"User {user_id} registered successfully."
30
+
31
+ # Recognize user
32
+ def recognize_user(image):
33
+ new_embedding = generate_embedding(image)
34
+ min_distance = float('inf')
35
+ recognized_user_id = "Unknown"
36
+
37
+ for user_id, embedding in user_embeddings.items():
38
+ distance = cosine(new_embedding, embedding)
39
+ if distance < min_distance:
40
+ min_distance = distance
41
+ recognized_user_id = user_id
42
+
43
+ return f"Recognized User: {recognized_user_id}"
44
+
45
+ # Gradio interface for registering users
46
+ register_interface = gr.Interface(
47
+ fn=register_user,
48
+ inputs=[gr.inputs.Image(shape=(200, 200)), gr.inputs.Textbox(label="User ID")],
49
+ outputs="text",
50
+ live=True
51
+ )
52
+
53
+ # Gradio interface for recognizing users
54
+ recognize_interface = gr.Interface(
55
+ fn=recognize_user,
56
+ inputs=gr.inputs.Image(shape=(200, 200)),
57
+ outputs="text",
58
+ live=True
59
+ )
60
+
61
+ if __name__ == "__main__":
62
+ register_interface.launch(share=True)
63
+ recognize_interface.launch(share=True)