File size: 6,278 Bytes
29572ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c575759
29572ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b20b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29572ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c271282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b20b3a
af55fe0
12ba455
c271282
 
 
 
 
29572ad
c271282
 
29572ad
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import warnings
warnings.filterwarnings("ignore")
import av, os, sys, cv2
import streamlit as st
from pydub.playback import play
import time, string, random, shutil
from PIL import Image
import streamlit as st
from pydub import AudioSegment
from streamlit_webrtc import VideoProcessorBase, webrtc_streamer, WebRtcMode, RTCConfiguration

from src.faceRecognize.face import Faces
from src.faceRecognize.facerecognition import *

sys.path.append(os.path.abspath('src/faceRecognize'))

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'

def generate_unique_filename():
    chars = string.ascii_letters + string.digits
    random_string = ''.join(random.choices(chars, k=8))
    return random_string + '.jpg'

def load_alert_sound():
    song = AudioSegment.from_file('src/faceRecognize/audio/alert.wav', format="wav")
    return song

def create_new_folder():
    initial_folder_path = "src/faceRecognize/facerec/data"
    new_folder_name = st.text_input("Enter New User:")
    if new_folder_name and st.button("Create User"):
        new_folder_path = os.path.join(initial_folder_path, new_folder_name)
        os.makedirs(new_folder_path, exist_ok=True)
        st.success(f"User '{new_folder_name}' created successfully.")
        return new_folder_path


def delete_folder():
    base_dir = os.path.join(os.getcwd(),"src/faceRecognize/facerec/data")
    folder_options = [f.path for f in os.scandir(base_dir) if f.is_dir()]
    folder_dict = {}
    for folder in folder_options:
        key = folder.split("/")[-1]
        folder_dict[key] = folder
    selected_folder = st.selectbox("Select User to Delete", list(folder_dict.keys()))
    if selected_folder and st.button("Delete User"):
        try:
            shutil.rmtree(folder_dict[selected_folder])
            st.success("User deleted successfully.")
        except Exception as e:
            st.error(f"Failed to delete user: {str(e)}")

def start_training():
    st.write("Training in progress...")
    time.sleep(5)
    import src.faceRecognize.facerec.train_v2
    st.success("Training completed successfully.")

def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
    image = frame.to_ndarray(format="bgr24")
    face_encoder = model_selector("Facenet")
    encodings_path = './src/faceRecognize/facerec/encodings/encodings.pkl'
    encoding_dict = load_pickle(self.encodings_path)
    # Run inference
    blob = cv2.dnn.blobFromImage(
        cv2.resize(image, (300, 300)), 0.007843, (300, 300), 127.5
    )
    net.setInput(blob)
    output = net.forward()

    h, w = image.shape[:2]

    # Convert the output array into a structured form.
    output = output.squeeze()  # (1, 1, N, 7) -> (N, 7)
    output = output[output[:, 2] >= score_threshold]
    
    detections = [
       detect(output, face_detector, face_encoder, encoding_dict)
        for detection in output
    ]

    # Render bounding boxes and captions
    for detection in detections:
        # image, pred = detect(frame_resized, face_detector, self.face_encoder, self.encoding_dict)
        caption = f"{detection.label}: {round(detection.score * 100, 2)}%"
        color = COLORS[detection.class_id]
        xmin, ymin, xmax, ymax = detection.box.astype("int")

        cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color, 2)
        cv2.putText(
            image,
            caption,
            (xmin, ymin - 15 if ymin - 15 > 15 else ymin + 15),
            cv2.FONT_HERSHEY_SIMPLEX,
            0.5,
            color,
            2,
        )

    result_queue.put(detections)

    return av.VideoFrame.from_ndarray(image, format="bgr24")

class FaceRecognitionProcessor(VideoProcessorBase):
    def __init__(self):
        self.alert_sound = load_alert_sound()
        self.face_encoder = model_selector("Facenet")
        self.encodings_path = './src/faceRecognize/facerec/encodings/encodings.pkl'
        self.encoding_dict = load_pickle(self.encodings_path)
        self.COUNT = 0

    def recv(self, frame):
        img = frame.to_ndarray(format="bgr24")
        frame_resized = cv2.resize(img, (640, 300))
        frame4 = Faces(frame_resized)
        try:
            frame2, pred = detect(frame_resized, face_detector, self.face_encoder, self.encoding_dict)
            if pred == 'unknown':
                if self.COUNT < 10:
                    self.COUNT += 1
                else:
                    play(self.alert_sound)
            else:
                self.COUNT = 0
            
            top_row = cv2.hconcat([frame2, frame2])
            bottom_row = cv2.hconcat([frame4, frame4])
            grid = cv2.vconcat([top_row, bottom_row])
        except Exception as e:
            top_row = cv2.hconcat([img, img])
            bottom_row = cv2.hconcat([frame4, frame4])
            grid = cv2.vconcat([top_row, bottom_row])
        
        return av.VideoFrame.from_ndarray(grid, format="bgr24")

def main():
    st.title("Face Recognition App")
    option = st.sidebar.selectbox("Choose an option", 
                        ("Add User", "Delete User", "Start Training","Run Code"))

    if option == "Add User":
        create_new_folder()

    elif option == "Delete User":
        delete_folder()

    elif option == "Start Training":
        start_training()

    elif option == "Run Code":
        RTC_CONFIGURATION = RTCConfiguration({
        "iceServers": [
            {"urls": ["stun:stun.l.google.com:19302"]},
            {"urls": ["stun:stun1.l.google.com:19302"]},
            {"urls": ["stun:stun2.l.google.com:19302"]},
            {"urls": ["stun:stun3.l.google.com:19302"]},
            {"urls": ["stun:stun4.l.google.com:19302"]}
            ]})

        webrtc_ctx = webrtc_streamer(
            key="face-detection",
            mode=WebRtcMode.SENDRECV,
            rtc_configuration=RTC_CONFIGURATION,
            #video_frame_callback=FaceRecognitionProcessor().recv,
            video_frame_callback = video_frame_callback,
            media_stream_constraints={"video": True, "audio": False},async_processing=True
        )
        if webrtc_ctx.state.playing:
            st.write("WebRTC is playing.")
        else:
            st.write("WebRTC is not playing.")
        
if __name__ == '__main__':
    main()
# streamlit run app.py