Upload 3 files
Browse files- embeddings.py +36 -0
- requirments.txt +8 -0
- streamlit_app.py +38 -0
embeddings.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from glob import glob
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2 as cv2
|
| 6 |
+
import tempfile
|
| 7 |
+
import insightface
|
| 8 |
+
from insightface.app import FaceAnalysis
|
| 9 |
+
from insightface.data import get_image as ins_get_image
|
| 10 |
+
|
| 11 |
+
def get_embeddings(db_dir):
|
| 12 |
+
app = FaceAnalysis(name='buffalo_l')
|
| 13 |
+
app.prepare(ctx_id=0, det_size=(640, 640),)
|
| 14 |
+
names = []
|
| 15 |
+
embeddings = []
|
| 16 |
+
|
| 17 |
+
folders = os.listdir(f'db_dir/data/raw')
|
| 18 |
+
for folder in tqdm(folders):
|
| 19 |
+
if ".ipynb_checkpoints" in folder: continue
|
| 20 |
+
print(folder)
|
| 21 |
+
img_paths = glob(f'db_dir/data/raw/{folder}/*')
|
| 22 |
+
for img_path in img_paths:
|
| 23 |
+
img = cv2.imread(img_path)
|
| 24 |
+
if img is None: continue
|
| 25 |
+
faces = app.get(img)
|
| 26 |
+
if len(faces) != 1: continue
|
| 27 |
+
face = faces[0]
|
| 28 |
+
names.append(folder)
|
| 29 |
+
embeddings.append(face.normed_embedding)
|
| 30 |
+
|
| 31 |
+
embeddings = np.stack(embeddings, axis=0)
|
| 32 |
+
temp_dir = tempfile.mkdtemp()
|
| 33 |
+
np.save(os.path.join(temp_dir, "embeddings.npy"), embeddings)
|
| 34 |
+
np.save(os.path.join(temp_dir, "names.npy"), names)
|
| 35 |
+
return temp_dir
|
| 36 |
+
|
requirments.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python
|
| 2 |
+
insightface
|
| 3 |
+
numpy
|
| 4 |
+
tdqm
|
| 5 |
+
matplotlib
|
| 6 |
+
pandas
|
| 7 |
+
onnx==1.16.0
|
| 8 |
+
onnxruntime==1.17.1
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
import zipfile
|
| 7 |
+
import shutil
|
| 8 |
+
import embeddings
|
| 9 |
+
|
| 10 |
+
def extract_database(zip_file):
|
| 11 |
+
# Create a temporary directory to extract the contents of the zip file
|
| 12 |
+
temp_dir = tempfile.mkdtemp()
|
| 13 |
+
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
| 14 |
+
zip_ref.extractall(temp_dir)
|
| 15 |
+
return temp_dir
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
#main function
|
| 19 |
+
def main():
|
| 20 |
+
st.title("FACE FUSE")
|
| 21 |
+
tab_selection = st.sidebar.selectbox("Select Funtionality",["Embeddings","Immage Recognition","Video Recognition","Webcam Recognition"])
|
| 22 |
+
if tab_selection == "Embeddings":
|
| 23 |
+
st.header("Embeddings")
|
| 24 |
+
zip_file = st.file_uploader("Upload DataBase(ZIP)",type="zip")
|
| 25 |
+
|
| 26 |
+
if zip_file:
|
| 27 |
+
db_dir = extract_database(zip_file)
|
| 28 |
+
if st.button("Get Embeddings"):
|
| 29 |
+
temp_dir=embeddings.get_embeddings(db_dir)
|
| 30 |
+
embeddings_path = os.path.join(temp_dir, "embeddings.npy")
|
| 31 |
+
names_path = os.path.join(temp_dir, "names.npy")
|
| 32 |
+
st.write("Download numpy files:")
|
| 33 |
+
st.download_button(label="Download embeddings.npy", data=open(embeddings_path, "rb"), file_name="embeddings.npy")
|
| 34 |
+
st.download_button(label="Download names.npy", data=open(names_path, "rb"), file_name="names.npy")
|
| 35 |
+
shutil.rmtree(db_dir, ignore_errors=True)
|
| 36 |
+
|
| 37 |
+
if __name__=="__main__":
|
| 38 |
+
main()
|