Python Project · Computer Vision

Roadmap: Build a Facial Recognition System in Python

A complete step‑by‑step guide from installing the libraries to real‑time webcam recognition. Learn face detection, encoding, and logging with OpenCV and face_recognition.

By Arj Updated July 2025 14 min read Intermediate Python
What you'll build
  • A real‑time facial recognition system using your webcam
  • Ability to register known faces and recognize them with names
  • Logging of unknown faces and attendance records
  • A solid project structure for future expansion

1Phase 1: Foundation

  • Learn Python basics (variables, loops, functions, file handling)
  • Learn about image processing libraries: OpenCV, face_recognition, dlib
  • Understand how face detection and recognition differ
  • Install required tools
Prerequisites

You should have Python 3.7+ installed and a basic understanding of Python syntax. If you are new to Python, check out our Python hub first.

2Phase 2: Face Detection & Recognition Basics

  • Load and encode images of known people
  • Detect and recognize faces from new images
  • Recognize faces in real‑time using webcam

3Phase 3: Organize Your System

  • Create folders for known faces
  • Add unknown recognition with logging
  • Handle multiple faces in a frame

4Phase 4: Expansion

  • Create a face registration system (GUI or CLI)
  • Store attendance logs in CSV
  • Add thresholding for better accuracy
  • Integrate with external hardware or services

5Step 0: Install the required libraries

terminal
pip install face_recognition opencv-python numpy
Pro tip
If you are on Windows, you may need to install dlib separately. Consider using a virtual environment to avoid conflicts.

6Step 1: Load and encode known faces

Create a folder called known/ with labeled images:

file structure
known/
├── Alice.jpg
├── Bob.jpg
└── Charlie.png

Python code:

python
import face_recognition
import os

known_faces = []
known_names = []

# Loop through images in the known directory
for filename in os.listdir('known'):
    if filename.endswith(('.jpg', '.jpeg', '.png')):
        path = os.path.join('known', filename)
        image = face_recognition.load_image_file(path)
        encodings = face_recognition.face_encodings(image)

        # Check if a face was found
        if encodings:
            known_faces.append(encodings[0])
            known_names.append(os.path.splitext(filename)[0])
What this does Loads each image from the known/ folder. Converts it into a 128‑dimension face encoding (a unique vector). Stores both the encoding and the person's name (based on the filename).

7Step 2: Detect and recognize faces in webcam feed

python
import cv2

video = cv2.VideoCapture(0)

while True:
    ret, frame = video.read()
    rgb = frame[:, :, ::-1]  # Convert BGR to RGB for face_recognition

    face_locations = face_recognition.face_locations(rgb)
    face_encodings = face_recognition.face_encodings(rgb, face_locations)

    for encoding, location in zip(face_encodings, face_locations):
        matches = face_recognition.compare_faces(known_faces, encoding, tolerance=0.6)
        name = "Unknown"

        face_distances = face_recognition.face_distance(known_faces, encoding)
        best_match_index = face_distances.argmin()

        if matches[best_match_index]:
            name = known_names[best_match_index]

        top, right, bottom, left = location
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)

    cv2.imshow("Face Recognition", frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video.release()
cv2.destroyAllWindows()
What this does Reads frames from your webcam. Detects all faces in the frame. Compares each face to the known face encodings. Draws a box and displays the name if matched.

8Step 3: Handle unknown faces (optional)

If you want to log unknown faces, add this inside the loop:

python
if name == "Unknown":
    timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    unknown_image = frame[top:bottom, left:right]
    cv2.imwrite(f"unknown/face_{timestamp}.jpg", unknown_image)

You could also maintain a log file:

python
with open("logs.csv", "a") as f:
    f.write(f"{timestamp},{name}\n")

9Project structure (recommended)

file structure
facial_recognition_project/
│
├── known/                  # Known faces
├── unknown/                # Captured unknown faces
├── logs.csv                # Attendance or detection logs
├── recognizer.py           # Main script
└── register_face.py        # Optional: add new faces

10Tips for accuracy and real‑world use

  • Lighting matters – Ensure good lighting for better accuracy.
  • Use front‑facing images for encoding.
  • Add multiple images per person (in subfolders) to improve recognition.
  • You can create an embedding dataset and use machine learning classifiers (SVM, KNN) on top of encodings for large‑scale systems.
Threshold tuning
The tolerance parameter in compare_faces controls sensitivity. Lower values (e.g., 0.4) are stricter; higher (e.g., 0.6) are more forgiving. Adjust based on your dataset.

11Optional: GUI with Tkinter or PyQt

To let users:

  • Register themselves with webcam
  • See live feed with face tags
  • Press buttons to mark attendance

You can build a small GUI that wraps around the recognizer.

12Final thoughts

Facial recognition systems in Python are not just academic – they are practical, powerful, and surprisingly achievable with open‑source tools. The face_recognition library provides a high‑level, easy‑to‑use interface, while OpenCV handles real‑time video and image processing.

StepGoal
Encode known facesCreate identity vectors
Read webcam frameReal‑time image feed
Detect & encode facesFind face positions and features
Match against knownCompare face encoding using distance
Display resultsDraw boxes and show name on screen

Want to go further?

  • Train a custom CNN using dlib or FaceNet
  • Use Raspberry Pi for IoT‑based face detection
  • Build a REST API to perform recognition on remote images
  • Integrate with a database for attendance/entry logs

Master Python with real projects

Get our Python Bundle: 5 real‑world projects, hands‑on exercises, and a step‑by‑step roadmap to go from beginner to job‑ready.

Grab the Python Bundle →

13Quiz: Test your understanding

Facial Recognition Quiz
Question 1 of 10
Keep building
Using the face_recognition Library
Dive deeper into the face_recognition library: detect faces, compare encodings, and build advanced recognition pipelines.
Read the next article

Stop wrestling with confusion.

Join thousands of students mastering Computer Science without the academic jargon.

From syntax to systems. We break down the hardest ideas in computer science so you can actually build things.

© 2026 Painless Programming. Built for students.
Scroll to Top