N3tron commited on
Commit
c1a46aa
·
verified ·
1 Parent(s): 40c4453

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -27
app.py CHANGED
@@ -1,36 +1,85 @@
1
  import os
2
- import shutil
 
3
  import streamlit as st
4
- import tempfile
 
 
 
5
  import zipfile
6
- import embeddings
7
 
8
- def extract_database(zip_file):
9
- # Create a temporary directory to extract the contents of the zip file
10
- temp_dir = tempfile.mkdtemp()
11
- with zipfile.ZipFile(zip_file, 'r') as zip_ref:
12
- zip_ref.extractall(temp_dir)
13
- return temp_dir
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  def main():
16
- st.title("FACE ANALYZER")
17
- tab_selection = st.sidebar.selectbox("Select Functionality", ["Embeddings", "Image Recognition", "Video Recognition", "Webcam Recognition"])
18
- if tab_selection == "Embeddings":
19
- st.header("Embeddings")
20
- zip_file = st.file_uploader("Upload DataBase(ZIP)", type="zip")
21
-
22
- if zip_file:
23
- db_dir = extract_database(zip_file)
24
- if st.button("Get Embeddings"):
25
- db_dir = embeddings.get_embeddings(db_dir)
26
- embeddings_path = os.path.join(db_dir, "embeddings.npy")
27
- names_path = os.path.join(db_dir, "names.npy")
28
-
29
- st.write("Download numpy files:")
30
- st.download_button(label="Download embeddings.npy", data=open(embeddings_path, "rb"), file_name="embeddings.npy")
31
- st.download_button(label="Download names.npy", data=open(names_path, "rb"), file_name="names.npy")
32
-
33
- shutil.rmtree(db_dir, ignore_errors=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  if __name__ == "__main__":
36
  main()
 
1
  import os
2
+ import cv2
3
+ import numpy as np
4
  import streamlit as st
5
+ from insightface.app import FaceAnalysis
6
+ from glob import glob
7
+ from tqdm import tqdm
8
+ import shutil
9
  import zipfile
 
10
 
11
+ # Function to extract zip file
12
+ 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')
19
+ app.prepare(ctx_id=0, det_size=(640, 640),)
20
+ names = []
21
+ embeddings = []
22
+
23
+ folders = os.listdir(db_dir)
24
+ for folder in tqdm(folders):
25
+ if ".ipynb_checkpoints" in folder:
26
+ continue
27
+ img_paths = glob(os.path.join(db_dir, folder, '*'))
28
+ for img_path in img_paths:
29
+ img = cv2.imread(img_path)
30
+ if img is None:
31
+ continue
32
+ faces = app.get(img)
33
+ if len(faces) != 1:
34
+ continue
35
+ face = faces[0]
36
+ names.append(folder)
37
+ embeddings.append(face.normed_embedding)
38
+
39
+ embeddings = np.stack(embeddings, axis=0)
40
+ np.save(os.path.join(db_dir, "embeddings.npy"), embeddings)
41
+ np.save(os.path.join(db_dir, "names.npy"), names)
42
+
43
+ # Function to delete files and directory
44
+ def delete_files(db_dir):
45
+ shutil.rmtree(db_dir)
46
 
47
+ # Main function
48
  def main():
49
+ st.title("Face Recognition App")
50
+ # Tabs
51
+ tabs = ["Embeddings", "Face Recognition in Image", "Face Recognition in Video", "Face Recognition through Webcam"]
52
+ choice = st.sidebar.selectbox("Select Option", tabs)
53
+
54
+ # Embeddings tab
55
+ if choice == "Embeddings":
56
+ st.subheader("Upload a Zip File")
57
+ uploaded_file = st.file_uploader("Choose a zip file", type="zip")
58
+
59
+ if uploaded_file is not None:
60
+ with open("temp.zip", "wb") as f:
61
+ f.write(uploaded_file.getbuffer())
62
+ st.success("File uploaded successfully!")
63
+ extract_zip("temp.zip", "temp") # Extract the uploaded zip file to the temp directory
64
+
65
+ if st.button("Get Embeddings"):
66
+ get_embeddings("temp")
67
+ st.success("Embeddings generated successfully!")
68
+
69
+ if st.button("Download Files"):
70
+ st.markdown("### Download names.npy:")
71
+ with open("temp/names.npy", "rb") as file:
72
+ st.download_button("Download names.npy", file.getvalue(), file_name="names.npy", mime="application/octet-stream")
73
+
74
+ st.markdown("### Download embeddings.npy:")
75
+ with open("temp/embeddings.npy", "rb") as file:
76
+ st.download_button("Download embeddings.npy", file.getvalue(), file_name="embeddings.npy", mime="application/octet-stream")
77
+
78
+ if st.button("Delete Files"):
79
+ delete_files("temp")
80
+ st.success("Files deleted successfully!")
81
+
82
+ # Other tabs can be added similarly
83
 
84
  if __name__ == "__main__":
85
  main()