Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,85 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
| 5 |
import zipfile
|
| 6 |
-
import embeddings
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
| 15 |
def main():
|
| 16 |
-
st.title("
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|