New Text Document
New Text Document
import face_recognition
import pyttsx3
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import threading
# Add known faces (you can replace with actual file paths)
add_face("path_to_person1_image.jpg", "Person 1")
add_face("path_to_person2_image.jpg", "Person 2")
if not ret:
print("Failed to grab frame.")
break
# Convert the frame to RGB (face_recognition works with RGB, not BGR)
rgb_frame = cv2.cvtColor(video_data, cv2.COLOR_BGR2RGB)
# Find all face locations and face encodings in the current frame
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
name = "Unknown"
for (top, right, bottom, left), face_encoding in zip(face_locations,
face_encodings):
# Check if the face matches any known face
matches = face_recognition.compare_faces(known_faces, face_encoding)
if True in matches:
first_match_index = matches.index(True)
name = known_names[first_match_index]
try:
add_face(image_path, name)
messagebox.showinfo("Success", f"Added {name} to the system!")
except Exception as e:
messagebox.showerror("Error", f"Error adding face: {str(e)}")
add_face_window = tk.Toplevel(root)
add_face_window.title("Add Known Face")
tk.Label(add_face_window, text="Name:").pack(pady=5)
entry_name = tk.Entry(add_face_window, width=40)
entry_name.pack(pady=5)
# Release the video capture object after the GUI window is closed
video_cap.release()
cv2.destroyAllWindows()