import cv2import tkinter as tk import os from PIL import Image, ImageTk import…
- User
import cv2import tkinter as tk import os from PIL import Image, ImageTk import face_recognition def detect_face(frame): # Загрузка каскадного классификатора для обнаружения лиц face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Преобразование кадра в оттенки серого gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Обнаружение лиц на кадре faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4, ) if len(faces) > 0: return True else: return False
Создание главного окна приложения
root = tk.Tk() def new_user(): # Создание нового окна new_user_window = tk.Toplevel(root) # Создание canvas (холста) для отображения видео с веб-камеры canvas = tk.Canvas(new_user_window, width=640, height=480) canvas.pack() # Создание формы регистрации login_label = tk.Label(new_user_window, text="Логин:") login_entry = tk.Entry(new_user_window) password_label = tk.Label(new_user_window, text="Пароль:") password_entry = tk.Entry(new_user_window, show='*') register_button = tk.Button(new_user_window, text="Зарегистрироваться", command=lambda: register(login_entry.get(), password_entry.get())) # Расположение элементов формы регистрации на окне login_label.pack() login_entry.pack() password_label.pack() password_entry.pack() register_button.pack()
def recognize_face(frame): # Load the dataset of registered users registered_users = os.listdir("dataset") # Convert the frame to RGB frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Detect faces in the frame face_locations = face_recognition.face_locations(frame_rgb) # Extract face embeddings from the detected faces face_encodings = face_recognition.face_encodings(frame_rgb, face_locations) for face_encoding in face_encodings: # Compare the face encoding with each registered user for user in registered_users: user_image = face_recognition.load_image_file(f"dataset/{user}/face.jpeg") user_encoding = face_recognition.face_encodings(user_image)[0] # Compare the face encoding with the registered user's face match = face_recognition.compare_faces([user_encoding], face_encoding) if match[0]: print("User already exists!") return True return False def register(login, password): # Check if the user already exists by recognizing the face ret, frame = video_capture.read() if not detect_face(frame): print("Face not detected!") return if recognize_face(frame): return else: print('User Createdf') os.mkdir(f"dataset/{login}") # Сохранение кадра с лицом пользователя cv2.imwrite(f"dataset/{login}/face.jpeg", frame) # Сохранение логина и пароля в файле with open(f"dataset/{login}/info.txt", "w") as file: file.write(f"Логин: {login}\n") file.write(f"Пароль: {password}")
Создание кнопок “Новый пользователь” и “Войти”new_user_button = tk.Button(root, text="Новый пользователь", command=new_user)
login_button = tk.Button(root, text="Войти")
Расположение кнопок на холстеnew_user_button.pack()
- Assistant