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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -40
app.py CHANGED
@@ -33,26 +33,31 @@ def prod_function(app, prod_path, webcam_img_pil):
33
  np_webcam = np.array(webcam_img_pil)
34
  cv2_webcam = cv2.cvtColor(np_webcam, cv2.COLOR_RGB2BGR)
35
 
36
- webcam_faces = app.get(cv2_webcam, max_num=1)
37
  if not webcam_faces:
38
- return None, None
39
 
40
- webcam_emb = torch.tensor(webcam_faces[0].embedding, dtype=torch.float32)
41
-
42
- similarity_scores = []
43
- for path in prod_path:
44
- img = cv2.imread(path)
45
- faces = app.get(img, max_num=1)
46
- if not faces:
47
- similarity_scores.append(torch.tensor(-1.0))
48
- continue
49
-
50
- face_emb = torch.tensor(faces[0].embedding, dtype=torch.float32)
51
- score = F.cosine_similarity(face_emb, webcam_emb, dim=0)
52
- similarity_scores.append(score)
53
-
54
- similarity_scores = torch.stack(similarity_scores)
55
- return similarity_scores, torch.argmax(similarity_scores)
 
 
 
 
 
56
 
57
  # Streamlit tabs
58
  about_tab, app_tab = st.tabs(["About the app", "Face Recognition"])
@@ -75,28 +80,32 @@ with app_tab:
75
  if picture is not None:
76
  with st.spinner("Analyzing face..."):
77
  image_pil = Image.open(picture)
78
- prediction_scores, match_idx = prod_function(app, image_paths, image_pil)
79
 
80
- if prediction_scores is None:
81
  st.warning("No face detected in the captured image.")
82
  else:
83
- st.write("Similarity Scores:", prediction_scores)
84
- matched_score = prediction_scores[match_idx].item()
85
-
86
- if matched_score >= 0.6:
87
- matched_name = os.path.basename(image_paths[match_idx]).split('.')[0]
88
- st.success(f"βœ… Welcome: {matched_name}")
89
-
90
- # Send attendance via POST
91
- url = "https://nielit-attendance.glitch.me/adds"
92
- data = {'rno': 15, 'sname': matched_name, 'sclass': 7}
93
- try:
94
- response = requests.post(url, data=data)
95
- if response.status_code == 200:
96
- st.success("Attendance marked successfully.")
97
- else:
98
- st.warning("Failed to update attendance.")
99
- except Exception as e:
100
- st.error(f"Request failed: {e}")
101
- else:
102
- st.error("❌ Match not found. Try again.")
 
 
 
 
 
33
  np_webcam = np.array(webcam_img_pil)
34
  cv2_webcam = cv2.cvtColor(np_webcam, cv2.COLOR_RGB2BGR)
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:
42
+ webcam_emb = torch.tensor(webcam_face.embedding, dtype=torch.float32)
43
+ similarity_scores = []
44
+ for path in prod_path:
45
+ img = cv2.imread(path)
46
+ faces = app.get(img, max_num=1)
47
+ if not faces:
48
+ similarity_scores.append(torch.tensor(-1.0))
49
+ continue
50
+
51
+ face_emb = torch.tensor(faces[0].embedding, dtype=torch.float32)
52
+ score = F.cosine_similarity(face_emb, webcam_emb, dim=0)
53
+ similarity_scores.append(score)
54
+
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"])
 
80
  if picture is not None:
81
  with st.spinner("Analyzing face..."):
82
  image_pil = Image.open(picture)
83
+ matches = prod_function(app, image_paths, image_pil)
84
 
85
+ if not matches:
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"
98
+ data = {'rno': 15, 'sname': matched_name, 'sclass': 7}
99
+ try:
100
+ response = requests.post(url, data=data)
101
+ if response.status_code == 200:
102
+ st.success(f"Attendance marked successfully for {matched_name}.")
103
+ else:
104
+ st.warning(f"Failed to update attendance for {matched_name}.")
105
+ except Exception as e:
106
+ st.error(f"Request failed: {e}")
107
+ else:
108
+ st.warning("❌ Face match not found or too low confidence.")
109
+
110
+ if not recognized:
111
+ st.error("❌ No matches above threshold. Try again.")