Spaces:
Runtime error
Runtime error
File size: 4,545 Bytes
0cba43f 45067dd 0cba43f 45067dd 0cba43f 2736c78 a0b7ae5 7638389 0cba43f a0b7ae5 0cba43f 0651d29 0cba43f 45067dd 0cba43f 45067dd 0cba43f 45067dd 0cba43f eceda24 0cba43f 45067dd 0cba43f eb30d87 0cba43f e87299f 0cba43f a0b7ae5 0651d29 0cba43f 0651d29 e87299f a0b7ae5 0651d29 a0b7ae5 0cba43f a0b7ae5 e87299f 0cba43f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
import streamlit as st
from PIL import Image
from utils.levels import complete_level, render_page, initialize_level
from utils.login import get_login, initialize_login
from utils.inference import query
import os
import time
import face_recognition
import json
import numpy as np
from collections import defaultdict
import pickle as pkl
initialize_login()
initialize_level()
LEVEL = 3
PKL_PATH = 'dataset/database.pkl'
information = defaultdict(dict)
def get_database():
with open(PKL_PATH,'rb') as f:
database = pkl.load(f)
return database
def step3_page():
st.header("Feature encoding")
st.markdown(
"""
### What is Face Encoding?
In face recognition, face encodings are numerical representations of facial features that are used to uniquely identify individuals.
These encodings are obtained by extracting relevant facial information from an input image or video frame.
Face encodings are typically computed using deep learning models, such as Convolutional Neural Networks (CNNs),
that are trained on large datasets of labeled faces. During the training process, these models learn to recognize patterns and extract discriminative features from facial images.
"""
)
st.image(
"https://miro.medium.com/v2/resize:fit:720/format:webp/1*V_wNVR0QvLQ7JZyUwMTv8w.jpeg",
use_column_width=True,
)
st.markdown(
"""
Once the face encodings are obtained, they can be stored in a database or used for face recognition tasks.
During face recognition, the encodings of input faces are compared to the stored encodings to determine if a match exists.
Various similarity metrics, such as Euclidean distance or cosine similarity, can be utilized to measure the similarity between
face encodings and determine potential matches.
"""
)
st.info(
"Now it's your turn to create face encodings to each of the faces in the known-face database that you have created in the previous step!"
)
img_dir = os.path.join(".sessions", get_login()["username"], "known_faces")
face_encodings_dir = os.path.join(".sessions", get_login()["username"], "face_encodings")
os.makedirs(face_encodings_dir, exist_ok=True)
images = os.listdir(img_dir)
if len(images) > 0:
st.subheader("Let's see your saved faces in your known-face database.")
cols = st.columns(len(images))
for i, img in enumerate(images):
face_name = img.split("_")[0]
cols[i].image(os.path.join(img_dir, img), use_column_width=True)
cols[i].write(face_name)
st.subheader("Lets create face encodings for the known-faces.")
# face_encodings_dict = {}
if st.button("Create Face Encodings"):
database = get_database()
my_bar = st.progress(0, text="Generating face encodings...")
if len(images) > 0:
for i, img in enumerate(images):
face_image = face_recognition.load_image_file(os.path.join(img_dir, img))
my_face_encoding = face_recognition.face_encodings(face_image)
face_name = img.split("_")[0]
face_id = img.split(".")[0]
# check if id already exists
existing_id = [i for i in database.keys()]
if face_id in existing_id:
st.error(f"Encoding already created for : {face_id}")
else:
database[face_id] = {'name': face_name,
'encoding': my_face_encoding}
with open(PKL_PATH, 'wb') as f:
pkl.dump(database, f)
my_bar.progress(int((i + 1) / len(images) * 100), text="Generating face encodings...")
# np.save(os.path.join(face_encodings_dir, img.split("_")[0]+".npy"), my_face_encoding)
# # face_encodings_dict[img.split("_")[1]] = my_face_encoding.tolist()
my_bar.progress(100, text="Successfully encoded all the known faces!")
st.success("Face encoding completed successfully!")
st.write(database)
# with open(os.path.join(".sessions", get_login()["username"], "face_encodings.json"), "w") as write_file:
# json.dump(face_encodings_dict, write_file, indent=4)
complete_level(LEVEL)
else:
my_bar.empty()
st.error("You have not taken any images yet! Do the previous steps first!")
render_page(step3_page, LEVEL)
|