File size: 2,893 Bytes
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
import streamlit as st
from PIL import Image
import face_recognition
from utils.levels import complete_level, render_page, initialize_level
from utils.login import get_login
import os
import uuid
from utils.login import initialize_login

initialize_login()
initialize_level()

LEVEL = 2


def step2_page():
    st.header("Face Detection")
    st.write(
        "Now it's time to collect the pictures we need to create our known-faces data base for our face recognition model. "
        "But remember, we should always ask for permission before taking someone's picture. We can use a smartphone or a digital camera to capture pictures, and it's important to take pictures of different people. This will help our application to have a good known-faces database!"
    )

    img_dir = os.path.join(".sessions", get_login()["username"], "known_faces")
    os.makedirs(img_dir, exist_ok=True)

    picture = st.camera_input("Take a picture")
    if picture:
        image = face_recognition.load_image_file(picture)
        st.image(image)

        # Find all the faces in the image using the default HOG-based model.
        # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated.
        # See also: find_faces_in_picture_cnn.py
        face_locations = face_recognition.face_locations(image)
        st.write("Algorithm found {} face(s) in this photograph.".format(len(face_locations)))

        cols = st.columns(len(face_locations))
        for i in range(len(face_locations)):
            col = cols[i]
            face = face_locations[i]
            # display faces
            with col:
                st.header("Face {}".format(i))
                # Print the location of each face in this image
                top, right, bottom, left = face
                # You can access the actual face itself like this:
                face_image = image[top:bottom, left:right]
                pil_image = Image.fromarray(face_image)
                st.image(pil_image)
                face_name = st.text_input('Specify name', "This is a placeholder", key="text_"+str(i))
                if st.button("Save", key="button_"+str(i)):
                    img_name = str(uuid.uuid4()) + f"{face_name}_{i}" + ".jpg"
                    img_path = os.path.join(img_dir, img_name)
                    with open(img_path, "wb") as f:
                        f.write(face_image.getvalue())
                    st.success("Face added successfully!")

    images = os.listdir(img_dir)
    if st.button("Clear All"):
        for img in images:
            os.remove(os.path.join(img_dir, img))
        st.success("All images cleared!")
        st.experimental_rerun()

    st.info("If you are satisfied with your images, click on the button below to complete this level.")
    if st.button("Complete"):
        complete_level(LEVEL)


render_page(step2_page, LEVEL)