# niloy-biswas/face-recognition architecture

> Architecture diagram of the GitHub repository niloy-biswas/face-recognition, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/niloy-biswas/face-recognition
- Repository: https://github.com/niloy-biswas/face-recognition

## Overview

##### Project Type & Purpose
- Type: A standalone Python computer-vision application (not a web or distributed system).
- Purpose: Capture webcam frames, detect faces via OpenCV Haar cascades, train/recognize faces, and demonstrate auxiliary lessons (filters, watermark, timelapse).

##### Top-Level Components
A. External Dependencies
– Python runtime
– OpenCV library
B. Hardware Interface
– Webcam (video capture device)
C. Application Modules (all under src/)
1. Module
– capture-image.py: initializes cv2.VideoCapture, grabs frames
2. Face Detection Module
– cascades/data/*.xml: Haar cascade definitions
3. Training & Recognition Module
– faces-train.py & trainner.yml: build/update recognizer model
– faces.py & recognizers/face-trainner.yml: load model, predict labels
– src/labels.pickle & src/pickles/face-labels.pickle: label‐to‐ID mapping
4. Demo / Lesson Modules (optional features)
– filter_lesson.py, watermark_lesson.py, record-video.py, timelapse-how-to.py
5. Data Storage
– src/images/*: raw face images for training
– src/recognizers/*.yml: serialized trained models
– pickles/: persisted label dictionaries

##### Relationships & Data Flow
- Webcam → (Capture Module) → Frame buffer
- Frame buffer → (Face Detection Module) uses Haar cascades → Detected face ROIs
- ROIs →
– (Training Path) → faces-train.py reads src/images/ → updates recognizer → writes .yml & .pickle
– (Recognition Path) → faces.py loads recognizer & pickles → predicts face IDs → overlays labels on frame
- Optional Lesson Modules subscribe to the frame stream to apply filters, watermark, timelapse logic, or save video

##### Architectural Patterns & Principles
– Modular separation: capture, detection, recognition, and demos are self-contained scripts
– Data-driven: cascades and images on disk; models and labels persisted via pickle/YAML
– Single process, synchronous flow (no microservices or external APIs)

##### Final Checklist
- Clearly labeled components (e.g., “Face Detection Module – uses Haar cascades”)
- A note on the side listing key technologies: Python 3.x, OpenCV, Haar Cascades, pickle/YAML

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External Dependencies"
        Libs["Python & OpenCV"]:::external
    end

    Webcam["Webcam"]:::hardware

    Libs --> Webcam
    Webcam --> Capture

    Capture["Capture Module"]:::modules
    click Capture "https://github.com/niloy-biswas/face-recognition/blob/master/src/capture-image.py"

    Capture --> Detection

    Detection["Face Detection Module"]:::modules
    click Detection "https://github.com/niloy-biswas/face-recognition/blob/master/src/cascades/data/*.xml"
    click Detection "https://github.com/niloy-biswas/face-recognition/tree/master/src/cascades/third-party/*"

    Detection --> Train
    Detection --> Recognize

    Train["Training Module"]:::modules
    click Train "https://github.com/niloy-biswas/face-recognition/blob/master/src/faces-train.py"

    Recognize["Recognition Module"]:::modules
    click Recognize "https://github.com/niloy-biswas/face-recognition/blob/master/src/faces.py"

    subgraph "Demo / Lessons"
        Filter["Filter Lesson"]:::modules
        click Filter "https://github.com/niloy-biswas/face-recognition/blob/master/src/lessons/filter_lesson.py"
        Watermark["Watermark Lesson"]:::modules
        click Watermark "https://github.com/niloy-biswas/face-recognition/blob/master/src/lessons/watermark_lesson.py"
        Record["Video Recording Lesson"]:::modules
        click Record "https://github.com/niloy-biswas/face-recognition/blob/master/src/lessons/record-video.py"
        Timelapse["Timelapse Lesson"]:::modules
        click Timelapse "https://github.com/niloy-biswas/face-recognition/blob/master/src/lessons/timelapse-how-to.py"
    end

    Detection -->|frames| Filter
    Detection -->|frames| Watermark
    Detection -->|frames| Record
    Detection -->|frames| Timelapse

    subgraph "Data Storage"
        Images["Image Data Store (src/images/)"]:::datastores
        click Images "https://github.com/niloy-biswas/face-recognition/tree/master/src/images/"
        CascadesDS["Cascades Store"]:::datastores
        click CascadesDS "https://github.com/niloy-biswas/face-recognition/blob/master/src/cascades/data/*.xml"
        Pickles["Label Persistence (src/labels.pickle)"]:::datastores
        click Pickles "https://github.com/niloy-biswas/face-recognition/blob/master/src/labels.pickle"
        PicklesDir["Pickles Dir"]:::datastores
        click PicklesDir "https://github.com/niloy-biswas/face-recognition/blob/master/src/pickles/face-labels.pickle"
        Models["Model Data Store"]:::datastores
        click Models "https://github.com/niloy-biswas/face-recognition/blob/master/src/recognizers/*.yml"
    end

    Train -->|writes .yml| Models
    Train -->|writes pickle| Pickles
    Recognize -->|loads .yml| Models
    Recognize -->|loads pickle| Pickles

    subgraph "Dependency Definitions"
        Req["requirements.txt"]:::datastores
        click Req "https://github.com/niloy-biswas/face-recognition/blob/master/requirements.txt"
        Runtime["runtime.txt"]:::datastores
        click Runtime "https://github.com/niloy-biswas/face-recognition/blob/master/runtime.txt"
    end

    classDef modules fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef datastores fill:#F5F5DC,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef hardware fill:#E6E6FA,stroke:#333,stroke-width:1px
```
