Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,13 +5,21 @@ from scipy.spatial.distance import cosine
|
|
5 |
import cv2
|
6 |
import os
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Load the embedding model
|
11 |
embedding_model = tf.keras.models.load_model('embedding_model.h5')
|
12 |
|
13 |
# Database to store embeddings and user IDs
|
14 |
-
user_embeddings =
|
|
|
15 |
|
16 |
# Preprocess the image
|
17 |
def preprocess_image(image):
|
@@ -28,7 +36,8 @@ def generate_embedding(image):
|
|
28 |
def register_user(image, user_id):
|
29 |
try:
|
30 |
embedding = generate_embedding(image)
|
31 |
-
user_embeddings
|
|
|
32 |
return f"User {user_id} registered successfully."
|
33 |
except Exception as e:
|
34 |
return f"Error during registration: {str(e)}"
|
@@ -37,23 +46,29 @@ def register_user(image, user_id):
|
|
37 |
def recognize_user(image):
|
38 |
try:
|
39 |
new_embedding = generate_embedding(image)
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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 |
-
|
54 |
-
|
|
|
|
|
55 |
except Exception as e:
|
56 |
return f"Error during recognition: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
def main():
|
59 |
with gr.Blocks() as demo:
|
|
|
5 |
import cv2
|
6 |
import os
|
7 |
|
8 |
+
# K-mean Clustering
|
9 |
+
from sklearn.cluster import KMeans
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
|
12 |
+
# Threshold
|
13 |
+
RECOGNITION_THRESHOLD = 0.1 # Adjust as needed
|
14 |
+
n_clusters = 5 # You can adjust this based on your data
|
15 |
+
kmeans = KMeans(n_clusters=n_clusters)
|
16 |
|
17 |
# Load the embedding model
|
18 |
embedding_model = tf.keras.models.load_model('embedding_model.h5')
|
19 |
|
20 |
# Database to store embeddings and user IDs
|
21 |
+
user_embeddings = []
|
22 |
+
user_ids = []
|
23 |
|
24 |
# Preprocess the image
|
25 |
def preprocess_image(image):
|
|
|
36 |
def register_user(image, user_id):
|
37 |
try:
|
38 |
embedding = generate_embedding(image)
|
39 |
+
user_embeddings.append(embedding)
|
40 |
+
user_ids.append(user_id)
|
41 |
return f"User {user_id} registered successfully."
|
42 |
except Exception as e:
|
43 |
return f"Error during registration: {str(e)}"
|
|
|
46 |
def recognize_user(image):
|
47 |
try:
|
48 |
new_embedding = generate_embedding(image)
|
49 |
+
# Update the KMeans model
|
50 |
+
kmeans.fit(user_embeddings)
|
51 |
+
cluster_label = kmeans.predict([new_embedding])[0]
|
52 |
+
distances = kmeans.transform([new_embedding])[0]
|
53 |
+
min_distance = np.min(distances)
|
54 |
+
|
|
|
|
|
|
|
|
|
|
|
55 |
if min_distance > RECOGNITION_THRESHOLD:
|
56 |
return "User not recognized."
|
57 |
+
|
58 |
+
# Find the user ID(s) in the closest cluster
|
59 |
+
recognized_user_ids = [user_ids[i] for i, label in enumerate(kmeans.labels_) if label == cluster_label]
|
60 |
+
return f"Recognized User(s): {', '.join(recognized_user_ids)}"
|
61 |
except Exception as e:
|
62 |
return f"Error during recognition: {str(e)}"
|
63 |
+
|
64 |
+
def plot_clusters():
|
65 |
+
# Assuming embeddings are 2-dimensional
|
66 |
+
plt.figure(figsize=(8, 6))
|
67 |
+
plt.scatter(*zip(*user_embeddings), c=kmeans.labels_)
|
68 |
+
plt.title('User Embeddings Clustered by K-Means')
|
69 |
+
plt.xlabel('Embedding Dimension 1')
|
70 |
+
plt.ylabel('Embedding Dimension 2')
|
71 |
+
plt.show()
|
72 |
|
73 |
def main():
|
74 |
with gr.Blocks() as demo:
|