pushpinder06 commited on
Commit
e1ac669
·
verified ·
1 Parent(s): 4a7d450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -47
app.py CHANGED
@@ -73,56 +73,56 @@ with app_tab:
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
 
 
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