Spaces:
Runtime error
Runtime error
import streamlit as st | |
from utils.levels import complete_level, render_page, initialize_level | |
from utils.login import get_login, initialize_login | |
from utils.inference import recognize | |
import os | |
import time | |
import face_recognition | |
import cv2 | |
import numpy as np | |
from PIL import Image | |
initialize_login() | |
initialize_level() | |
LEVEL = 4 | |
def step4_page(): | |
st.header("Face Recognition: Trying It Out") | |
st.write( | |
""" | |
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 (our known-face database) | |
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 that we know how our face recognition application works, let's try it out!" | |
) | |
# Select input type | |
st.info("Select your input type to analyze!") | |
input_type = st.radio("Select the Input Type", ["Image upload", "Camera"]) | |
# Put slide to adjust tolerance | |
tolerance = 0.6 | |
# tolerance = st.slider("Tolerance", 0.0, 1.0, 0.15, 0.01) | |
# st.info( | |
# "Tolerance is the threshold for face recognition. The lower the tolerance, the more strict the face recognition. The higher the tolerance, the more loose the face recognition.") | |
if input_type == "Image upload": | |
st.title("Face Recognition App") | |
uploaded_images = st.file_uploader("Please upload image(s) to try it out!", type=['jpg', 'png', 'jpeg'], accept_multiple_files=True) | |
if len(uploaded_images) != 0: | |
# Read uploaded image with face_recognition | |
for image in uploaded_images: | |
image = face_recognition.load_image_file(image) | |
image, name, face_id = recognize(image, tolerance) | |
st.image(image) | |
else: | |
st.info("Please upload an image") | |
elif input_type == "Camera": | |
st.title("Face Recognition App") | |
uploaded_image = st.camera_input("Take a picture") | |
if uploaded_image: | |
# Read uploaded image with face_recognition | |
image = face_recognition.load_image_file(uploaded_image) | |
image, name, face_id = recognize(image, tolerance) | |
st.image(image) | |
else: | |
st.info("Please take an image") | |
else: | |
st.title("Face Recognition App") | |
# Camera Settings | |
cam = cv2.VideoCapture(0) | |
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 640) | |
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) | |
FRAME_WINDOW = st.image([]) | |
while True: | |
ret, frame = cam.read() | |
if not ret: | |
st.error("Failed to capture frame from camera") | |
st.info("Please turn off the other app that is using the camera and restart app") | |
st.stop() | |
image, name, face_id = recognize(frame, tolerance) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
# Display name and ID of the person | |
FRAME_WINDOW.image(image) | |
st.info("Click on the button below to complete this level!") | |
if st.button("Complete Level"): | |
complete_level(LEVEL) | |
render_page(step4_page, LEVEL) | |