N3tron commited on
Commit
cb3b592
·
verified ·
1 Parent(s): 8325815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -1
app.py CHANGED
@@ -13,6 +13,39 @@ def extract_zip(zip_file_path, extract_dir):
13
  with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
14
  zip_ref.extractall(extract_dir)
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Function to get embeddings
17
  def get_embeddings(db_dir):
18
  app = FaceAnalysis(name='buffalo_l')
@@ -53,7 +86,7 @@ def delete_files(db_dir):
53
  def main():
54
  st.title("Face Recognition App")
55
  # Tabs
56
- tabs = ["Embeddings", "Face Recognition in Image", "Face Recognition in Video", "Face Recognition through Webcam"]
57
  choice = st.sidebar.selectbox("Select Option", tabs)
58
 
59
  # Embeddings tab
@@ -84,6 +117,46 @@ def main():
84
  st.success("Files deleted successfully!")
85
 
86
  # Other tabs can be added similarly
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  if __name__ == "__main__":
89
  main()
 
13
  with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
14
  zip_ref.extractall(extract_dir)
15
 
16
+ # Function to recognize faces
17
+ def recognize_faces(frame, names, embeddings, app):
18
+ # Perform face analysis on the frame
19
+ faces = app.get(frame)
20
+
21
+ # Process each detected face separately
22
+ for face in faces:
23
+ # Retrieve the embedding for the detected face
24
+ detected_embedding = face.normed_embedding
25
+
26
+ # Calculate similarity scores with known embeddings
27
+ scores = np.dot(detected_embedding, np.array(embeddings).T)
28
+ scores = np.clip(scores, 0., 1.)
29
+
30
+ # Find the index with the highest score
31
+ idx = np.argmax(scores)
32
+ max_score = scores[idx]
33
+
34
+ # Check if the maximum score is above a certain threshold (adjust as needed)
35
+ threshold = 0.7
36
+ if max_score >= threshold:
37
+ recognized_name = names[idx]
38
+ else:
39
+ recognized_name = "Unknown"
40
+
41
+ # Draw bounding box around the detected face
42
+ bbox = face.bbox.astype(int)
43
+ cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
44
+ # Write recognized name within the bounding box
45
+ cv2.putText(frame, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
46
+
47
+ return frame
48
+
49
  # Function to get embeddings
50
  def get_embeddings(db_dir):
51
  app = FaceAnalysis(name='buffalo_l')
 
86
  def main():
87
  st.title("Face Recognition App")
88
  # Tabs
89
+ tabs = ["Embeddings", "Face Recognition in Image", "Face Recognition in Video", "Webcam"]
90
  choice = st.sidebar.selectbox("Select Option", tabs)
91
 
92
  # Embeddings tab
 
117
  st.success("Files deleted successfully!")
118
 
119
  # Other tabs can be added similarly
120
+ if choice == "Webcam":
121
+ st.header("WEBCAM")
122
+ st.subheader("upload names and embeddings file")
123
+ uploaded_names = st.file_uploader("Upload names.npy", type="npy")
124
+ uploaded_embeddings = st.file_uploader("Upload embeddings.npy", type="npy")
125
+
126
+ if uploaded_names and uploaded_embeddings:
127
+ # Load names and embeddings
128
+ names = np.load(uploaded_names)
129
+ embeddings = np.load(uploaded_embeddings)
130
+
131
+ # Initialize FaceAnalysis app
132
+ app = FaceAnalysis(name='buffalo_l')
133
+ app.prepare(ctx_id=0, det_size=(640, 640))
134
+
135
+ # Start capturing video from webcam
136
+ cap = cv2.VideoCapture(0)
137
+
138
+ # Process each frame in real-time
139
+ while True:
140
+ # Capture frame-by-frame
141
+ ret, frame = cap.read()
142
+ if not ret:
143
+ break
144
+
145
+ # Perform face recognition
146
+ frame = recognize_faces(frame, names, embeddings, app)
147
+
148
+ # Display the resulting frame
149
+ st.image(frame, channels="BGR", use_column_width=True)
150
+
151
+ # Break the loop if 'q' is pressed
152
+ if cv2.waitKey(1) & 0xFF == ord('q'):
153
+ break
154
+
155
+ # Release the capture
156
+ cap.release()
157
+ cv2.destroyAllWindows()
158
+
159
+
160
 
161
  if __name__ == "__main__":
162
  main()