# thearn/webcam-pulse-detector architecture

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

- Interactive diagram: https://gitdiagram.com/thearn/webcam-pulse-detector
- Repository: https://github.com/thearn/webcam-pulse-detector

## Overview

##### Project type and purpose
- This is a real-time computer-vision and signal-processing Python application.
- It captures video frames from a webcam or IP camera, detects a face, extracts a forehead ROI, measures optical intensity over time, computes heart rate, and renders both the video overlay and data plots.

##### File structure
- Entry point: get_pulse.py (orchestrates the pipeline and user controls).
- lib/device.py: abstracts video source (local webcam or IP camera).
- lib/interface.py: handles UI windows, keyboard events, and on-frame overlays.
- lib/processors.py: contains image-processing (face detection, ROI extraction) and signal-processing (raw intensity → filtered signal → BPM estimation).
- haarcascade_frontalface_alt.xml: OpenCV face-detection model.
- tests/test_processors.py: unit tests for processing algorithms.
- requirements.txt: Python dependencies (e.g., opencv-python, numpy, scipy, matplotlib).

##### Key insights from README
- Real-time signal-processing pipeline illustrated in README.
- Dependencies: OpenCV for image capture and face detection; NumPy/SciPy for numerical processing; Matplotlib (optional) for data plotting.
- Single-face support; optional locking of ROI; real-time overlay and data display toggled via keyboard.

##### Main components and their interactions
a. Video Source Component (lib/device.py)
- Inputs: local webcam or IP camera stream.
- Dependency: OpenCV VideoCapture.
b. Face Detection & ROI Extraction (lib/processors.py)
- Uses Haarcascade XML via OpenCV.
- Outputs the forehead region subimage each frame.
c. Data Acquisition & Buffering (lib/processors.py)
- Computes mean green-channel intensity per frame.
- Buffers a time series of intensities.
d. Signal Processing & Heart-Rate Estimation (lib/processors.py)
- Applies filtering to isolate pulse waveform.
- Computes power spectral density and detects BPM peak.
- Computes phase for real-time pulsing overlay.
e. UI/Interface Component (lib/interface.py)
- Renders data plot window (raw intensity, filtered signal, PSD).
- Handles user keyboard input (S to lock ROI, D to toggle data plot).
f. Controller/Orchestration (get_pulse.py)
- Initializes components, enters main loop: grab frame → process → render → handle events.

##### Architectural patterns and design principles
- Pipeline architecture: clear linear stages (Capture → Process → Output).
- Separation of concerns: device abstraction, processing logic, and UI are decoupled into separate modules.
- Configurability: parameters (data_spike_limit, buffer size) defined in get_pulse.py.
- Testability: processing functions covered by unit tests in tests/test_processors.py.

##### Essential elements
- Clear, concise labels for all components.
- Optionally, annotate performance-critical paths (e.g., face detection and filtering loop).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Orchestration
    Controller["Controller<br/>(get_pulse.py)"]:::orchestration

    %% Core Modules
    Device["Video Source Component<br/>(Device abstraction)"]:::core
    Detector["Face Detection & ROI Extraction"]:::core
    Buffer["Data Acquisition & Buffering"]:::core
    Signal["Signal Processing & Heart-Rate Estimation"]:::core
    UI["UI/Interface Component"]:::ui

    %% External Libraries & Resources
    OpenCV["OpenCV"]:::external
    NumPySci["NumPy/SciPy"]:::external
    Matplotlib["Matplotlib"]:::external
    HaarXML["Haarcascade XML Model"]:::external
    Tests["Unit Tests<br/>(tests/test_processors.py)"]:::external
    Req["Requirements<br/>(requirements.txt)"]:::external

    %% Flow
    Controller -->|initializes| Device
    Controller -->|initializes| Detector
    Controller -->|initializes| Buffer
    Controller -->|initializes| Signal
    Controller -->|initializes| UI

    Device -->|frame| Detector
    Detector -->|ROI image| Buffer
    Buffer -->|intensity time series| Signal
    Signal -->|BPM & phase| UI
    UI -->|keyboard events| Controller

    %% External dependencies linkage
    Device --> OpenCV
    Detector --> OpenCV
    Signal --> NumPySci
    Signal --> Matplotlib
    Detector --> HaarXML

    %% Loop annotation
    Controller -.->|loop| Device

    %% Legend
    subgraph Legend
        L1["Green: Orchestration"]:::orchestration
        L2["Blue: Core Modules"]:::core
        L3["Orange: UI Component"]:::ui
        L4["Yellow: External & Resources"]:::external
        L5["Arrows labeled with dataflow and control"] 
    end

    %% Click Events
    click Controller "https://github.com/thearn/webcam-pulse-detector/blob/master/get_pulse.py"
    click Device "https://github.com/thearn/webcam-pulse-detector/blob/master/lib/device.py"
    click Detector "https://github.com/thearn/webcam-pulse-detector/blob/master/lib/processors.py"
    click Buffer "https://github.com/thearn/webcam-pulse-detector/blob/master/lib/processors.py"
    click Signal "https://github.com/thearn/webcam-pulse-detector/blob/master/lib/processors.py"
    click UI "https://github.com/thearn/webcam-pulse-detector/blob/master/lib/interface.py"
    click HaarXML "https://github.com/thearn/webcam-pulse-detector/blob/master/haarcascade_frontalface_alt.xml"
    click Tests "https://github.com/thearn/webcam-pulse-detector/blob/master/tests/test_processors.py"
    click Req "https://github.com/thearn/webcam-pulse-detector/blob/master/requirements.txt"

    %% Styles
    classDef orchestration fill:#b3e6b3,stroke:#339933,stroke-width:2px;
    classDef core fill:#cce0ff,stroke:#3366cc,stroke-width:2px;
    classDef ui fill:#ffe0b3,stroke:#ff8000,stroke-width:2px;
    classDef external fill:#ffffb3,stroke:#cccc00,stroke-width:2px;
    class Controller orchestration
    class Device,Detector,Buffer,Signal core
    class UI ui
    class OpenCV,NumPySci,Matplotlib,HaarXML,Tests,Req external
```
