Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -73,44 +73,60 @@ with app_tab:
|
|
73 |
picture = st.camera_input("Take a picture", disabled=not enable)
|
74 |
|
75 |
if picture is not None:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
else:
|
83 |
-
st.
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
# Detect faces
|
91 |
-
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
|
92 |
-
|
93 |
-
# Draw rectangles around faces
|
94 |
-
for (x, y, w, h) in faces:
|
95 |
-
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
96 |
-
|
97 |
-
return image,
|
98 |
-
|
99 |
-
if matched_score >= 0.6:
|
100 |
-
matched_name = os.path.basename(image_paths[match_idx]).split('.')[0]
|
101 |
-
|
102 |
-
st.success(f"✅ Welcome: {matched_name}")
|
103 |
-
|
104 |
-
# Send attendance via POST
|
105 |
-
url = "https://aiml2025.glitch.me/attend"
|
106 |
-
data = {'rollno': 15, 'Name': matched_name, 'Class': 7}
|
107 |
-
try:
|
108 |
-
response = requests.post(url, data=data)
|
109 |
-
if response.status_code == 200:
|
110 |
-
st.success("Attendance marked successfully.")
|
111 |
-
else:
|
112 |
-
st.warning("Failed to update attendance.")
|
113 |
-
except Exception as e:
|
114 |
-
st.error(f"Request failed: {e}")
|
115 |
-
else:
|
116 |
-
st.error("❌ Match not found. Try again.")
|
|
|
73 |
picture = st.camera_input("Take a picture", disabled=not enable)
|
74 |
|
75 |
if picture is not None:
|
76 |
+
with st.spinner("Analyzing face..."):
|
77 |
+
image_pil = Image.open(picture)
|
78 |
+
np_webcam = np.array(image_pil)
|
79 |
+
cv2_webcam = cv2.cvtColor(np_webcam, cv2.COLOR_RGB2BGR)
|
80 |
+
|
81 |
+
webcam_faces = app.get(cv2_webcam, max_num=1)
|
82 |
+
|
83 |
+
if not webcam_faces:
|
84 |
+
st.warning("No face detected in the captured image.")
|
85 |
+
else:
|
86 |
+
webcam_emb = torch.tensor(webcam_faces[0].embedding, dtype=torch.float32)
|
87 |
+
|
88 |
+
similarity_scores = []
|
89 |
+
for path in image_paths:
|
90 |
+
img = cv2.imread(path)
|
91 |
+
faces = app.get(img, max_num=1)
|
92 |
+
if not faces:
|
93 |
+
similarity_scores.append(torch.tensor(-1.0))
|
94 |
+
continue
|
95 |
+
|
96 |
+
face_emb = torch.tensor(faces[0].embedding, dtype=torch.float32)
|
97 |
+
score = F.cosine_similarity(face_emb, webcam_emb, dim=0)
|
98 |
+
similarity_scores.append(score)
|
99 |
+
|
100 |
+
similarity_scores = torch.stack(similarity_scores)
|
101 |
+
match_idx = torch.argmax(similarity_scores)
|
102 |
+
matched_score = similarity_scores[match_idx].item()
|
103 |
+
|
104 |
+
# Draw bounding box and name
|
105 |
+
(x1, y1, x2, y2) = map(int, webcam_faces[0].bbox)
|
106 |
+
cv2.rectangle(cv2_webcam, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
107 |
+
|
108 |
+
if matched_score >= 0.6:
|
109 |
+
matched_name = os.path.basename(image_paths[match_idx]).split('.')[0]
|
110 |
+
cv2.putText(cv2_webcam, matched_name, (x1, y1 - 10),
|
111 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
112 |
+
|
113 |
+
st.success(f"✅ Welcome: {matched_name}")
|
114 |
+
|
115 |
+
# Send attendance via POST
|
116 |
+
url = "https://aiml2025.glitch.me/attend"
|
117 |
+
data = {'rollno': 15, 'Name': matched_name, 'Class': 7}
|
118 |
+
try:
|
119 |
+
response = requests.post(url, data=data)
|
120 |
+
if response.status_code == 200:
|
121 |
+
st.success("Attendance marked successfully.")
|
122 |
+
else:
|
123 |
+
st.warning("Failed to update attendance.")
|
124 |
+
except Exception as e:
|
125 |
+
st.error(f"Request failed: {e}")
|
126 |
else:
|
127 |
+
st.error("❌ Match not found. Try again.")
|
128 |
+
|
129 |
+
# Convert back to RGB for displaying in Streamlit
|
130 |
+
final_img = cv2.cvtColor(cv2_webcam, cv2.COLOR_BGR2RGB)
|
131 |
+
st.image(final_img, caption="Detected Face")
|
132 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|