N3tron commited on
Commit
d647994
·
verified ·
1 Parent(s): 4268477

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -83
app.py CHANGED
@@ -10,6 +10,11 @@ from tqdm import tqdm
10
  from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
11
  import shutil
12
  import zipfile
 
 
 
 
 
13
 
14
  # Function to extract zip file
15
  def extract_zip(zip_file_path, extract_dir):
@@ -17,88 +22,6 @@ def extract_zip(zip_file_path, extract_dir):
17
  zip_ref.extractall(extract_dir)
18
 
19
 
20
- class FaceRecognitionTransformer(VideoTransformerBase):
21
- def __init__(self):
22
- self.app = FaceAnalysis(name='buffalo_l')
23
- self.app.prepare(ctx_id=0, det_size=(640, 640))
24
- self.names = None
25
- self.embeddings = None
26
-
27
- def _recognize_faces(self, frame):
28
- if self.names is None or self.embeddings is None:
29
- return frame
30
-
31
- # Perform face analysis on the frame
32
- faces = self.app.get(frame)
33
-
34
- # Process each detected face separately
35
- for face in faces:
36
- # Retrieve the embedding for the detected face
37
- detected_embedding = face.normed_embedding
38
-
39
- # Calculate similarity scores with known embeddings
40
- scores = np.dot(detected_embedding, np.array(self.embeddings).T)
41
- scores = np.clip(scores, 0., 1.)
42
-
43
- # Find the index with the highest score
44
- idx = np.argmax(scores)
45
- max_score = scores[idx]
46
-
47
- # Check if the maximum score is above a certain threshold (adjust as needed)
48
- threshold = 0.7
49
- if max_score >= threshold:
50
- recognized_name = self.names[idx]
51
- else:
52
- recognized_name = "Unknown"
53
-
54
- # Draw bounding box around the detected face
55
- bbox = face.bbox.astype(int)
56
- cv2.rectangle(frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 255, 0), 2)
57
- # Write recognized name within the bounding box
58
- cv2.putText(frame, recognized_name, (bbox[0], bbox[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
59
-
60
- # Debug print
61
- print("Detected face:", recognized_name, "with confidence:", max_score)
62
-
63
- return frame
64
-
65
- def transform(self, frame):
66
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
67
- frame = self._recognize_faces(frame)
68
- return frame
69
-
70
- # Function to get embeddings
71
- def get_embeddings(db_dir):
72
- app = FaceAnalysis(name='buffalo_l')
73
- app.prepare(ctx_id=0, det_size=(640, 640),)
74
- names = []
75
- embeddings = []
76
-
77
- # Traverse through each subfolder
78
- for root, dirs, files in os.walk(db_dir):
79
- for folder in dirs:
80
- if folder == ".ipynb_checkpoints":
81
- continue
82
- img_paths = glob(os.path.join(root, folder, '*'))
83
- for img_path in img_paths:
84
- img = cv2.imread(img_path)
85
- if img is None:
86
- continue
87
- faces = app.get(img)
88
- if len(faces) != 1:
89
- continue
90
- face = faces[0]
91
- names.append(folder)
92
- embeddings.append(face.normed_embedding)
93
-
94
- if embeddings:
95
- embeddings = np.stack(embeddings, axis=0)
96
- np.save(os.path.join(db_dir, "embeddings.npy"), embeddings)
97
- np.save(os.path.join(db_dir, "names.npy"), names)
98
- else:
99
- st.warning("No embeddings generated. Please ensure that there are valid images with detected faces.")
100
-
101
-
102
  # Function to delete files and directory
103
  def delete_files(db_dir):
104
  shutil.rmtree(db_dir)
@@ -140,7 +63,7 @@ def main():
140
  # Other tabs can be added similarly
141
  if choice == "Webcam":
142
  st.header("WEBCAM")
143
- st.subheader("upload names and embeddings file")
144
  uploaded_names = st.file_uploader("Upload names.npy", type="npy")
145
  uploaded_embeddings = st.file_uploader("Upload embeddings.npy", type="npy")
146
 
@@ -160,7 +83,21 @@ def main():
160
  async_processing=True,
161
  )
162
 
 
 
 
 
 
 
 
163
 
 
 
 
 
 
 
 
164
 
165
 
166
  if __name__ == "__main__":
 
10
  from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
11
  import shutil
12
  import zipfile
13
+ import image_app
14
+ from utils import app
15
+ from embeddings_app import get_embeddings
16
+ import webcam_app
17
+ from webcam_app import FaceRecognitionTransformer
18
 
19
  # Function to extract zip file
20
  def extract_zip(zip_file_path, extract_dir):
 
22
  zip_ref.extractall(extract_dir)
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # Function to delete files and directory
26
  def delete_files(db_dir):
27
  shutil.rmtree(db_dir)
 
63
  # Other tabs can be added similarly
64
  if choice == "Webcam":
65
  st.header("WEBCAM")
66
+ st.subheader("Upload names and embeddings file")
67
  uploaded_names = st.file_uploader("Upload names.npy", type="npy")
68
  uploaded_embeddings = st.file_uploader("Upload embeddings.npy", type="npy")
69
 
 
83
  async_processing=True,
84
  )
85
 
86
+ if choice == "Face Recognition in Image":
87
+ st.header("Image Recognition")
88
+ st.subheader("Upload names and embeddings file")
89
+ upload_names = st.file_uploader("Upload names.npy", type="npy")
90
+ upload_embeddings = st.file_uploader("Upload embeddings.npy", type="npy")
91
+ st.subheader("Upload Image")
92
+ upload_img = st.file_uploader("Upload Image",type=["png","jpg"])
93
 
94
+ if upload_img and upload_names and upload_embeddings:
95
+ names = np.load(uploaded_names)
96
+ embeddings = np.load(uploaded_embeddings)
97
+ im_array = np.frombuffer(upload_img.read(),np.uint8)
98
+ img = cv2.imdecode(im_array,cv2.IMREAD_COLOR)
99
+ if st.button("Verify Faces"):
100
+ image_app.recognize_and_display(img,embeddings,names,app)
101
 
102
 
103
  if __name__ == "__main__":