File size: 3,391 Bytes
0cba43f
 
 
16eecd4
0cba43f
 
2736c78
 
 
caa436d
0cba43f
 
 
 
 
 
 
 
19127c5
 
 
 
 
 
 
 
 
2736c78
 
 
16eecd4
 
0cb5452
16eecd4
3157ae0
 
68c2ecc
 
caa436d
c1cf12f
16eecd4
c1cf12f
16eecd4
 
 
 
 
 
 
 
c1cf12f
 
 
 
 
 
 
 
 
 
caa436d
16eecd4
 
 
 
 
 
caa436d
16eecd4
 
 
 
 
 
 
 
 
 
caa436d
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
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)