face_recognition_tutorial / pages /3_Face Encodings.py
Shafeek Saleem
added face encodings pagejj
eceda24
raw
history blame
3.68 kB
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
initialize_login()
initialize_level()
LEVEL = 3
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("_")[1]
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"):
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)
my_bar.progress(int((i + 1) / len(images) * 100), text="Generating face encodings...")
np.save(os.path.join(face_encodings_dir, img.split("_")[1]+".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!")
# 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)