Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
37 |
if not webcam_faces:
|
38 |
-
return
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
79 |
|
80 |
-
if
|
81 |
st.warning("No face detected in the captured image.")
|
82 |
else:
|
83 |
-
st.write("Similarity Scores:", prediction_scores)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
|
|
|
|
|
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.")
|