ImanAmran commited on
Commit
162217b
·
1 Parent(s): 1200449

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -71,19 +71,25 @@ def register_user(image, user_id):
71
  def recognize_user(image):
72
  try:
73
  new_embedding = generate_embedding(image)
74
- closest_user_id = None
75
- closest_distance = float('inf')
76
-
77
- for user_id, embedding in zip(user_ids, user_embeddings):
78
- distance = cosine(new_embedding, embedding)
79
- if distance < closest_distance:
80
- closest_distance = distance
81
- closest_user_id = user_id
82
-
83
- if closest_distance <= RECOGNITION_THRESHOLD:
84
- return f"Recognized User: {closest_user_id}"
85
- else:
 
 
86
  return "User not recognized."
 
 
 
 
87
  except Exception as e:
88
  return f"Error during recognition: {str(e)}"
89
 
 
71
  def recognize_user(image):
72
  try:
73
  new_embedding = generate_embedding(image)
74
+
75
+ if len(user_embeddings) < n_clusters:
76
+ # Handle the case where there are not enough users for K-means
77
+ # For example, you could use nearest neighbor search among existing embeddings
78
+ # Here, I'm just returning a message for simplicity
79
+ return "Not enough registered users for recognition."
80
+
81
+ # Update the KMeans model
82
+ kmeans.fit(user_embeddings)
83
+ cluster_label = kmeans.predict([new_embedding])[0]
84
+ distances = kmeans.transform([new_embedding])[0]
85
+ min_distance = np.min(distances)
86
+
87
+ if min_distance > RECOGNITION_THRESHOLD:
88
  return "User not recognized."
89
+
90
+ # Find the user ID(s) in the closest cluster
91
+ recognized_user_ids = [user_ids[i] for i, label in enumerate(kmeans.labels_) if label == cluster_label]
92
+ return f"Recognized User(s): {', '.join(recognized_user_ids)}"
93
  except Exception as e:
94
  return f"Error during recognition: {str(e)}"
95