import cv2 import tkinter as tk from tkinter import messagebox from datetime import…
- User
import cv2 import tkinter as tk from tkinter import messagebox from datetime import datetime from PIL import Image, ImageTk
import os import pickle import face_recognition
def add_new_user(known_faces, known_names): video = cv2.VideoCapture(0) # Веб-камера
while True: ret, frame = video.read()
Определение лиц на кадре
face_locations = face_recognition.face_locations(frame) face_encodings = face_recognition.face_encodings(frame, face_locations)
for face_encoding, face_location in zip(face_encodings, face_locations): matches = face_recognition.compare_faces(known_faces, face_encoding) name = “Unknown”
Проверка, что лицо на видео есть в базе данных
if True in matches: continue
name = tk.simpledialog.askstring(“Новый пользователь”, “Введите имя пользователя:”) if name is None: return
Создание директории для нового пользователя
person_folder = os.path.join(“database”, name) os.makedirs(person_folder, exist_ok=True)
Сохранение скриншота с лицом
screenshot_name = datetime.now().strftime(“%Y%m%d%H%M%S”) + “.png” screenshot_path = os.path.join(person_folder, screenshot_name) cv2.imwrite(screenshot_path, frame)
Обновление базы данных
known_faces.append(face_encoding) known_names.append(name)
messagebox.showinfo(“Новый пользователь”, “Пользователь успешно добавлен в базу данных.”) return
cv2.imshow(““Face Recognition””, frame)
if cv2.waitKey(1) & 0xFF == ord(‘q’): break
video.release() cv2.destroyAllWindows()
def load_known_faces(folder_path): known_faces = [] known_names = []
for name in os.listdir(folder_path): person_folder = os.path.join(folder_path, name) if not os.path.isdir(person_folder): continue
for image_name in os.listdir(person_folder): image_path = os.path.join(person_folder, image_name) image = face_recognition.load_image_file(image_path) image_encoding = face_recognition.face_encodings(image) if len(image_encoding) > 0: known_faces.append(image_encoding[0]) known_names.append(name)
return known_faces, known_names
def recognize_faces(camera_id, known_faces, known_names): video = cv2.VideoCapture(camera_id)
while True: ret, frame = video.read()
Определение лиц на кадре
face_locations = face_recognition.face_locations(frame) face_encodings = face_recognition.face_encodings(frame, face_locations)
for face_encoding, face_location in zip(face_encodings, face_locations): matches = face_recognition.compare_faces(known_faces, face_encoding) name = “Unknown”
Поиск соответствия лица с известными лицами
if True in matches: first_match_index = matches.index(True) name = known_names[first_match_index]
- Assistant
You can use the
cv2.resize()function to resize the video frame before displaying it in the tkinter window. Here's an example of how you can modify therecognize_faces()function:import numpy as np def recognize_faces(camera_id, known_faces, known_names): video = cv2.VideoCapture(camera_id) canvas = tk.Canvas(window, width=800, height=600) canvas.pack() while True: ret, frame = video.read() # Resize the video frame resized_frame = cv2.resize(frame, (800, 450)) # ... Face recognition code ... # Display the resized frame in the tkinter window img = ImageTk.PhotoImage(Image.fromarray(cv2.cvtColor(resized_frame, cv2.COLOR_BGR2RGB))) canvas.create_image(0, 0, image=img, anchor=tk.NW) # ... Rest of the code ...In this example, the
resized_frameis created by resizing the originalframeusingcv2.resize()function. Theresized_frameis then converted to a PIL image (Image.fromarray()) and then to a Tkinter-compatible image (). Finally, the resized frame is displayed in the tkinter window using . Adjust the size parameters in to fit your specific needs.