diff --git a/__pycache__/util.cpython-39.pyc b/__pycache__/util.cpython-39.pyc new file mode 100644 index 0000000..e9d9f4e Binary files /dev/null and b/__pycache__/util.cpython-39.pyc differ diff --git a/main.py b/main.py index 8ddfaa0..511bdb1 100755 --- a/main.py +++ b/main.py @@ -8,7 +8,7 @@ import face_recognition import util -from test import test + class App: @@ -19,8 +19,7 @@ def __init__(self): self.login_button_main_window = util.get_button(self.main_window, 'login', 'green', self.login) self.login_button_main_window.place(x=750, y=200) - self.logout_button_main_window = util.get_button(self.main_window, 'logout', 'red', self.logout) - self.logout_button_main_window.place(x=750, y=300) + self.register_new_user_button_main_window = util.get_button(self.main_window, 'register new user', 'gray', self.register_new_user, fg='black') @@ -39,7 +38,7 @@ def __init__(self): def add_webcam(self, label): if 'cap' not in self.__dict__: - self.cap = cv2.VideoCapture(2) + self.cap = cv2.VideoCapture(0) self._label = label self.process_webcam() @@ -58,13 +57,7 @@ def process_webcam(self): def login(self): - label = test( - image=self.most_recent_capture_arr, - model_dir='/home/phillip/Desktop/todays_tutorial/27_face_recognition_spoofing/code/face-attendance-system/Silent-Face-Anti-Spoofing/resources/anti_spoof_models', - device_id=0 - ) - - if label == 1: + name = util.recognize(self.most_recent_capture_arr, self.db_dir) @@ -76,31 +69,9 @@ def login(self): f.write('{},{},in\n'.format(name, datetime.datetime.now())) f.close() - else: - util.msg_box('Hey, you are a spoofer!', 'You are fake !') - - def logout(self): + - label = test( - image=self.most_recent_capture_arr, - model_dir='/home/phillip/Desktop/todays_tutorial/27_face_recognition_spoofing/code/face-attendance-system/Silent-Face-Anti-Spoofing/resources/anti_spoof_models', - device_id=0 - ) - - if label == 1: - - name = util.recognize(self.most_recent_capture_arr, self.db_dir) - - if name in ['unknown_person', 'no_persons_found']: - util.msg_box('Ups...', 'Unknown user. Please register new user or try again.') - else: - util.msg_box('Hasta la vista !', 'Goodbye, {}.'.format(name)) - with open(self.log_path, 'a') as f: - f.write('{},{},out\n'.format(name, datetime.datetime.now())) - f.close() - - else: - util.msg_box('Hey, you are a spoofer!', 'You are fake !') + def register_new_user(self): @@ -138,18 +109,39 @@ def start(self): self.main_window.mainloop() def accept_register_new_user(self): - name = self.entry_text_register_new_user.get(1.0, "end-1c") + # Captura a codificação facial + new_user_encodings = face_recognition.face_encodings(self.register_new_user_capture) - embeddings = face_recognition.face_encodings(self.register_new_user_capture)[0] + if not new_user_encodings: + util.msg_box('Erro!', 'Não foi possível detectar um rosto. Tente novamente.') + return - file = open(os.path.join(self.db_dir, '{}.pickle'.format(name)), 'wb') - pickle.dump(embeddings, file) + new_user_encoding = new_user_encodings[0] - util.msg_box('Success!', 'User was registered successfully !') + # Carregar todas as codificações existentes + existing_encodings = [] + for filename in os.listdir(self.db_dir): + if filename.endswith('.pickle'): + with open(os.path.join(self.db_dir, filename), 'rb') as file: + existing_encodings.append(pickle.load(file)) + # Verificar se a nova codificação já existe + matches = face_recognition.compare_faces(existing_encodings, new_user_encoding) + + if any(matches): + util.msg_box('Erro!', 'Usuário já cadastrado. Tente registrar um rosto diferente.') + return + + # Salva a nova codificação facial + name = self.entry_text_register_new_user.get(1.0, "end-1c").strip() + with open(os.path.join(self.db_dir, f'{name}.pickle'), 'wb') as file: + pickle.dump(new_user_encoding, file) + + util.msg_box('Sucesso!', 'Usuário registrado com sucesso!') self.register_new_user_window.destroy() + if __name__ == "__main__": app = App() app.start() diff --git a/requirements.txt b/requirements.txt index 637245a..ada0c20 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -opencv-python==4.6.0.66 -Pillow==9.2.0 -face_recognition==1.3.0 +opencv-python +Pillow +face_recognition diff --git a/requirements_windows.txt b/requirements_windows.txt index 821b1b3..721d356 100644 --- a/requirements_windows.txt +++ b/requirements_windows.txt @@ -1,6 +1,6 @@ -cmake==3.17.2 -dlib==19.18.0 -opencv-python==4.6.0.66 -Pillow==9.2.0 +cmake +dlib +opencv-python +Pillow face_recognition==1.3.0 - +#dificil funcionar