OVERVIEW :
 

● This project is a real-time face recognition system implemented in Python using the face_recognition, OpenCV, and dlib libraries.
● It loads known faces from a dataset and identifies them via webcam input. Ideal for security systems, attendance systems, or personal use.

How the System Works:
Dataset Preparation:

Add clear images of each person into known_faces/<name>/.

Encoding Faces:

During runtime, the script loads all images and encodes facial features.

Real-time Recognition:

Captures frames from webcam → Detects faces → Compares with known encodings → Displays results.

Step by step instructions :

● Create an new repository and redirect to that repository
● Install the following libraries and packages

pip install face_recognition opencv-python numpy pillow
sudo apt-get install cmake
pip install dlib==19.22.99

● Connect an USB cam or CSI camera in the jetson nano and run the following program

import cv2 import os import tkinter as tk from tkinter import ttk from tkinter import messagebox from PIL import Image, ImageTk class FaceCaptureApp: def __init__(self, root): self.root = root self.root.title("Face Capture GUI") self.root.geometry("700x300") # Smaller window # Variables self.person_name = tk.StringVar() self.mode = tk.StringVar(value="auto") self.save_path = "" self.count = 0 self.frame_count = 0 self.capture_running = False self.frame = None # GUI Layout self.setup_gui() # OpenCV self.cap = None def setup_gui(self): # Input for name tk.Label(self.root, text="Person Name:").grid(row=0, column=0, padx=5, pady=5, sticky="e") tk.Entry(self.root, textvariable=self.person_name).grid(row=0, column=1, padx=5, pady=5) # Mode dropdown tk.Label(self.root, text="Mode:").grid(row=1, column=0, padx=5, pady=5, sticky="e") ttk.Combobox(self.root, textvariable=self.mode, values=["auto", "manual"], state="readonly").grid(row=1, column=1, padx=5, pady=5) # Buttons tk.Button(self.root, text="Start Capture", command=self.start_capture).grid(row=2, column=0, padx=5, pady=5) tk.Button(self.root, text="Stop", command=self.stop_capture).grid(row=2, column=1, padx=5, pady=5) # Capture Button (manual mode only) self.capture_btn = tk.Button(self.root, text="Capture Image", command=self.capture_image) self.capture_btn.grid(row=3, column=0, columnspan=2, padx=5, pady=5) self.capture_btn.config(state="disabled") # Status label self.status_label = tk.Label(self.root, text="Status: Idle", fg="blue") self.status_label.grid(row=4, column=0, columnspan=2) # Image count self.count_label = tk.Label(self.root, text="Images Captured: 0") self.count_label.grid(row=5, column=0, columnspan=2) # Video panel self.video_panel = tk.Label(self.root) self.video_panel.grid(row=0, column=2, rowspan=6, padx=10) def start_capture(self): name = self.person_name.get().strip() if not name: messagebox.showwarning("Input Error", "Please enter a name.") return self.save_path = os.path.join("face_dataset", name) os.makedirs(self.save_path, exist_ok=True) self.cap = cv2.VideoCapture(0) if not self.cap.isOpened(): messagebox.showerror("Error", "Could not open webcam.") return # Smaller resolution self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320) self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) self.capture_running = True self.count = 0 self.frame_count = 0 self.status_label.config(text="Status: Capturing...", fg="green") if self.mode.get() == "manual": self.capture_btn.config(state="normal") self.root.after(10, self.video_loop) def stop_capture(self): self.capture_running = False if self.cap: self.cap.release() self.status_label.config(text="Status: Stopped", fg="red") self.video_panel.config(image='') # Clear video feed self.capture_btn.config(state="disabled") def capture_image(self): if self.frame is not None: face_img = cv2.resize(self.frame, (224, 224)) img_name = os.path.join(self.save_path, f"{self.count}.jpg") cv2.imwrite(img_name, face_img) self.count += 1 self.count_label.config(text=f"Images Captured: {self.count}") self.status_label.config(text=f"Saved: {img_name}", fg="blue") def video_loop(self): if not self.capture_running: return ret, frame = self.cap.read() if not ret: return self.frame = frame.copy() # Auto capture if self.mode.get() == "auto" and self.frame_count % 5 == 0: self.capture_image() self.frame_count += 1 # Convert image to RGB and resize to match preview size rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) img = Image.fromarray(rgb_frame).resize((320, 240)) imgtk = ImageTk.PhotoImage(image=img) self.video_panel.imgtk = imgtk self.video_panel.configure(image=imgtk) self.root.after(10, self.video_loop) # Run the GUI if __name__ == "__main__": root = tk.Tk() app = FaceCaptureApp(root) root.mainloop()
 

● While running the program please ensure that you’re inside the desired project repo then on successful running of the program the GUI will open



● Enter the person’s name and select the model then start capturing the image the collected images will be stored under separate folders





● Then we can start training the model with the collected data for training the model use this code

import os
import cv2
import face_recognition
import pickle

DATASET_PATH = "face_dataset"
ENCODINGS_FILE = "face_encodings.pkl"

known_encodings = []
known_names = []

for person_name in os.listdir(DATASET_PATH):
    person_path = os.path.join(DATASET_PATH, person_name)
    
    if not os.path.isdir(person_path):
        continue

    for image_name in os.listdir(person_path):
        image_path = os.path.join(person_path, image_name)
        image = cv2.imread(image_path)
        rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        encodings = face_recognition.face_encodings(rgb_image)
        if len(encodings) > 0:
            known_encodings.append(encodings[0])
            known_names.append(person_name)

data = {"encodings": known_encodings, "names": known_names}

with open(ENCODINGS_FILE, "wb") as f:
    pickle.dump(data, f)

print(f"✅ Training complete. {len(known_names)} faces encoded.")



● When the training is completed a new file with .pkl extension will be created in your repository that will contains the encodings of the training data


import cv2
import face_recognition
import pickle
import threading

# Load trained face encodings
with open("face_encodings.pkl", "rb") as f:
    data = pickle.load(f)

# Extract the encodings and names
known_faces = data["encodings"]
known_names = data["names"]

# Start video capture from the webcam
video_capture = cv2.VideoCapture(0)

# Set the resolution to a lower value for faster processing (e.g., 640x480)
video_capture.set(3, 640)  # Width
video_capture.set(4, 480)  # Height

print("📷 Starting webcam... Press 'q' to quit.")

frame_skip_rate = 2  # Number of frames to skip (e.g., 2 means process every 2nd frame)
frame_count = 0

# Function to process each frame in a separate thread
def process_frame(frame):
    global frame_count

    # Resize the frame to speed up face recognition
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    # Detect faces in the frame
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

    # Loop through each face found in the frame
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        # Compare the detected face with known faces
        matches = face_recognition.compare_faces(known_faces, face_encoding)
        name = "Unknown"

        # If a match is found, use the name of the matched person
        if True in matches:
            index = matches.index(True)
            name = known_names[index]

        # Draw a rectangle around the face and display the name
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)

    # Display the resulting frame
    cv2.imshow("Face Recognition", frame)

    frame_count += 1

    if cv2.waitKey(1) & 0xFF == ord("q"):
        return False  # Exit loop

    return True

while True:
    ret, frame = video_capture.read()
    if not ret:
        break

    # Skip processing frames based on frame_skip_rate
    if frame_count % frame_skip_rate == 0:
        # Process the current frame in a separate thread
        if not process_frame(frame):
            break

    # Continue to the next frame
    frame_count += 1

# Release the video capture and close the window
video_capture.release()
cv2.destroyAllWindows()

     
 

● It will start the detection





« Home

×