Rasleen commited on
Commit
9178928
Β·
verified Β·
1 Parent(s): c5f95fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -35,7 +35,7 @@ def prod_function(app, prod_path, webcam_img_pil):
35
 
36
  webcam_faces = app.get(cv2_webcam)
37
  if not webcam_faces:
38
- return []
39
 
40
  results = []
41
  for webcam_face in webcam_faces:
@@ -55,9 +55,17 @@ def prod_function(app, prod_path, webcam_img_pil):
55
  similarity_scores = torch.stack(similarity_scores)
56
  best_match_idx = torch.argmax(similarity_scores)
57
  best_score = similarity_scores[best_match_idx].item()
58
- results.append((best_score, best_match_idx))
 
 
 
 
 
 
 
 
59
 
60
- return results
61
 
62
  # Streamlit tabs
63
  about_tab, app_tab = st.tabs(["About the app", "Face Recognition"])
@@ -86,12 +94,20 @@ with app_tab:
86
  st.warning("No face detected in the captured image.")
87
  else:
88
  # st.write("Similarity Scores:", prediction_scores)
89
- recognized = False
90
- for score, idx in matches:
91
- if matched_score >= 0.6:
92
- matched_name = os.path.basename(image_paths[match_idx]).split('.')[0]
93
- st.success(f"βœ… Welcome: {matched_name}")
94
- recognizes=True
 
 
 
 
 
 
 
 
95
 
96
  # Send attendance via POST
97
  url = "https://nielit-attendance.glitch.me/adds"
 
35
 
36
  webcam_faces = app.get(cv2_webcam)
37
  if not webcam_faces:
38
+ return [], cv2_webcam
39
 
40
  results = []
41
  for webcam_face in webcam_faces:
 
55
  similarity_scores = torch.stack(similarity_scores)
56
  best_match_idx = torch.argmax(similarity_scores)
57
  best_score = similarity_scores[best_match_idx].item()
58
+
59
+ # Get coordinates
60
+ x1, y1, x2, y2 = [int(i) for i in webcam_face.bbox]
61
+ if best_score >= 0.6:
62
+ name = os.path.basename(prod_path[best_match_idx]).split('.')[0]
63
+ else:
64
+ name = "Unknown"
65
+ results.append({'bbox': (x1, y1, x2, y2), 'name': name})
66
+
67
 
68
+ return results, cv2_webcam
69
 
70
  # Streamlit tabs
71
  about_tab, app_tab = st.tabs(["About the app", "Face Recognition"])
 
94
  st.warning("No face detected in the captured image.")
95
  else:
96
  # st.write("Similarity Scores:", prediction_scores)
97
+ for match in matches:
98
+ x1, y1, x2, y2 = match['bbox']
99
+ name = match['name']
100
+ color = (0, 255, 0) if name != "Unknown" else (0, 0, 255)
101
+ cv2.rectangle(image_bgr, (x1, y1), (x2, y2), color, 2)
102
+ cv2.putText(image_bgr, name, (x1, y2 + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
103
+
104
+ if name != "Unknown":
105
+ # recognized = False
106
+ # for score, idx in matches:
107
+ # if matched_score >= 0.6:
108
+ # matched_name = os.path.basename(image_paths[match_idx]).split('.')[0]
109
+ # st.success(f"βœ… Welcome: {matched_name}")
110
+ # recognizes=True
111
 
112
  # Send attendance via POST
113
  url = "https://nielit-attendance.glitch.me/adds"