File size: 2,341 Bytes
37ca0d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from deepface import DeepFace
import cv2
import time
import streamlit as st
from PIL import Image

windowsHolder = st.empty()
value = st.empty()

def main():
    cap = cv2.VideoCapture(0)
    while True:
        time.sleep(0.5)
        _, img = cap.read()

        if img is None:
            break

        #cv2.imshow("Window", img)
        windowsHolder.image(img, channels="BGR")
        extract_faces(img)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    cap.release()
    cv2.destroyAllWindows()

    return


def extract_faces(raw_img):
    test = DeepFace.extract_faces(raw_img, detector_backend="mtcnn", align=True)

    if test:
        faces = []

        for face_obj in test:
            facial_area = face_obj["facial_area"]
            faces.append(
                (
                    facial_area["x"],
                    facial_area["y"],
                    facial_area["w"],
                    facial_area["h"],
                )
            )

        detected_faces = []
        for x, y, w, h in faces:
            detected_face = raw_img[int(y) : int(y + h), int(x) : int(x + w)]
            detected_faces.append(detected_face)
            recognition(detected_face)
            print("Face detected")
            time.sleep(0.6)
            return


def recognition(img):
    print("Starting recognition")
    dfs = DeepFace.find(
        img_path=img,
        db_path="/Users/futuregadgetlab/Desktop/DB",
        detector_backend="mtcnn",
        model_name="VGG-Face",
        distance_metric="euclidean_l2",
        enforce_detection=False,
    )

    found_non_empty_df = False

    for df in dfs:
        if len(df) != 0:
            for _, row in df.iterrows():
                if row["VGG-Face_euclidean_l2"] < 0.6:
                    print("Matched!")
                    with value.container():
                        st.write("Matched! Welcome to expo 2023")
                    st.balloons()
                    found_non_empty_df = True
                    break

            if found_non_empty_df:
                break  # Exit the outer loop if a match is found in any DataFrame

    if not found_non_empty_df:
        print("SIke")
        with value.container():
            st.warning("You don't exist, my friend!")
    time.sleep(1)


if __name__ == "__main__":
    main()