Ismail Ashraq commited on
Commit
0f70127
1 Parent(s): b2be13f

add no face detection message

Browse files
Files changed (2) hide show
  1. app.py +7 -5
  2. facenet.py +7 -5
app.py CHANGED
@@ -40,12 +40,14 @@ facenet = init_facenet()
40
 
41
  col1, col2, col3 = st.columns([0.2, 2, 0.2])
42
 
43
- with col2:
44
- img_file_buffer = st.camera_input("")
45
 
46
  if img_file_buffer is not None:
47
  img = Image.open(img_file_buffer)
48
  emb = facenet.encode([img])
49
- result = index.query(emb[0], top_k=3, include_metadata=True)
50
- cards = template.render(results=result["matches"])
51
- st.markdown(f"<div class='container-md' align='center'><div class='row'>{cards}</div></div>", unsafe_allow_html=True)
 
 
 
 
40
 
41
  col1, col2, col3 = st.columns([0.2, 2, 0.2])
42
 
43
+ with col2: img_file_buffer = st.camera_input("")
 
44
 
45
  if img_file_buffer is not None:
46
  img = Image.open(img_file_buffer)
47
  emb = facenet.encode([img])
48
+ if emb:
49
+ result = index.query(emb[0], top_k=3, include_metadata=True)
50
+ cards = template.render(results=result["matches"])
51
+ st.markdown(f"<div class='container-md' align='center'><div class='row'>{cards}</div></div>", unsafe_allow_html=True)
52
+ else:
53
+ with col2: st.error('Oops! No face was detected, try again.', icon="🚨")
facenet.py CHANGED
@@ -15,9 +15,11 @@ class FacenetEmbedder:
15
  def encode(self, batch):
16
  face_batch = self.mtcnn(batch)
17
  face_batch = [i for i in face_batch if i is not None]
18
- aligned = torch.stack(face_batch)
19
- if self.device.type == "cuda":
20
- aligned = aligned.to(self.device)
 
21
 
22
- embeddings = self.resnet(aligned).detach().cpu()
23
- return embeddings.tolist()
 
 
15
  def encode(self, batch):
16
  face_batch = self.mtcnn(batch)
17
  face_batch = [i for i in face_batch if i is not None]
18
+ if face_batch:
19
+ aligned = torch.stack(face_batch)
20
+ if self.device.type == "cuda":
21
+ aligned = aligned.to(self.device)
22
 
23
+ embeddings = self.resnet(aligned).detach().cpu()
24
+ return embeddings.tolist()
25
+ else: return None