# ak2042/yolo_face_detection architecture

> Architecture diagram of the GitHub repository ak2042/yolo_face_detection, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ak2042/yolo_face_detection
- Repository: https://github.com/ak2042/yolo_face_detection

## Overview

#### Purpose and Entry Points
This is a terminal-driven YOLOv8 face-detection pipeline built around the WIDER FACE dataset. Its three operational entry points are dataset preparation, model training, and webcam inference:
- `scripts/DownloadAndFormatDataset.py`
- `scripts/train.sh`
- `scripts/infer.sh`

#### Dataset Acquisition and Normalization
`scripts/DownloadAndFormatDataset.py` is the data-ingestion boundary. It downloads WIDER FACE training/validation imagery and annotations, then converts source annotations into YOLO detection labels. It creates the runtime dataset tree under `datasets/widerface/` (generated, not versioned in this tree), including train/validation images, labels, and the YOLO dataset YAML.

#### YOLO Dataset Contract
The generated `datasets/widerface/widerface.yaml` is the contract between preparation and training. It establishes:
- dataset root: `datasets/widerface`
- splits: `images/train` and `images/val`
- single detection class: `face` at class ID `0`

Annotation conversion must produce normalized YOLO bounding-box labels aligned with each image, since Ultralytics consumes this layout directly.

#### Training Stage
`scripts/train.sh` invokes the Ultralytics YOLOv8 CLI to fine-tune a pretrained YOLOv8 nano checkpoint (`yolov8n.pt` per the README) against the generated dataset. The documented default training profile is 50 epochs at 640×640 resolution. Training outputs, including the selected checkpoint, are expected under Ultralytics’ `runs/detect/train/weights/`, notably `best.pt`.

#### Inference Stage
`scripts/infer.sh` is the live deployment entry point. It loads the trained detector and passes the default local camera as the inference source, relying on Ultralytics for frame capture, prediction, rendering, and on-screen bounding-box display. The trained-model boundary is the generated checkpoint, typically `runs/detect/train/weights/best.pt`.

#### Runtime and External Dependencies
Python 3.8+ and packages declared in `requirements.txt` provide the execution environment. Core ML runtime behavior is delegated to Ultralytics YOLOv8 and PyTorch; GPU-backed PyTorch is recommended but not required by the repository interface. External data/services are the WIDER FACE dataset and its referenced CUHK-CSE Hugging Face mirror. Webcam access is required only for the live inference path.

#### End-to-End Control Flow
1. Install runtime dependencies from `requirements.txt`.
2. Run `scripts/DownloadAndFormatDataset.py` to materialize YOLO-compatible WIDER FACE data.
3. Run `scripts/train.sh`; it reads the generated dataset contract and writes experiment artifacts/checkpoints.
4. Run `scripts/infer.sh` or an equivalent `yolo detect predict` command using the trained checkpoint and a camera or image source.

The repository itself is orchestration-centric: data conversion and shell entry points define local workflow, while model training and prediction semantics remain owned by the external Ultralytics runtime.

Main entry points: [`scripts/DownloadAndFormatDataset.py`](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/DownloadAndFormatDataset.py) (Download and format), [`scripts/infer.sh`](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/infer.sh) (Run webcam inference), [`requirements.txt`](https://github.com/ak2042/yolo_face_detection/blob/main/requirements.txt) (Python dependencies), [`scripts/train.sh`](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/train.sh) (Train model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_setup["Environment"]
  node_requirements["Python dependencies<br/>requirements<br/>[requirements.txt]"]
  node_ultralytics{{"Ultralytics YOLOv8<br/>external ML runtime"}}
end

subgraph group_data["Dataset pipeline"]
  node_widerface[("WIDER FACE source<br/>external dataset")]
  node_prepare["Download and format<br/>Python entry point"]
  node_image_splits["Train and val images<br/>generated image splits"]
  node_yolo_labels["Normalized YOLO labels<br/>generated labels"]
  node_dataset_yaml["Dataset contract<br/>YOLO dataset YAML"]
end

subgraph group_training["Training"]
  node_pretrained_model["YOLOv8 nano base<br/>pretrained checkpoint"]
  node_train_script["Train model<br/>shell entry point<br/>[train.sh]"]
  node_training_run["Detection training run<br/>Ultralytics experiment"]
  node_best_checkpoint["Selected detector<br/>trained checkpoint"]
end

subgraph group_inference["Live inference"]
  node_infer_script["Run webcam inference<br/>shell entry point<br/>[infer.sh]"]
  node_webcam(("Local camera<br/>video source"))
  node_live_display["Rendered detections<br/>live output"]
end

node_requirements -->|"installs"| node_ultralytics
node_widerface -->|"downloads"| node_prepare
node_prepare -->|"materializes"| node_image_splits
node_prepare -->|"converts annotations to"| node_yolo_labels
node_prepare -->|"writes"| node_dataset_yaml
node_image_splits -->|"referenced as splits"| node_dataset_yaml
node_yolo_labels -.->|"aligns with contract"| node_dataset_yaml
node_train_script -->|"invokes CLI"| node_ultralytics
node_dataset_yaml -->|"configures"| node_training_run
node_pretrained_model -->|"fine-tunes"| node_training_run
node_ultralytics -->|"executes"| node_training_run
node_training_run -->|"selects"| node_best_checkpoint
node_infer_script -->|"invokes prediction"| node_ultralytics
node_best_checkpoint -->|"loads"| node_infer_script
node_webcam -->|"supplies frames"| node_ultralytics
node_ultralytics -->|"renders detections"| node_live_display

click node_requirements "https://github.com/ak2042/yolo_face_detection/blob/main/requirements.txt"
click node_prepare "https://github.com/ak2042/yolo_face_detection/blob/main/scripts/DownloadAndFormatDataset.py"
click node_train_script "https://github.com/ak2042/yolo_face_detection/blob/main/scripts/train.sh"
click node_infer_script "https://github.com/ak2042/yolo_face_detection/blob/main/scripts/infer.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_requirements,node_ultralytics toneBlue
class node_widerface,node_prepare,node_image_splits,node_yolo_labels,node_dataset_yaml toneAmber
class node_pretrained_model,node_train_script,node_training_run,node_best_checkpoint toneMint
class node_infer_script,node_webcam,node_live_display toneRose
```

## Components

### Environment

- [Python dependencies](https://github.com/ak2042/yolo_face_detection/blob/main/requirements.txt): requirements · Python 3.8+ packages, including Ultralytics and PyTorch. · `requirements.txt`
- **Ultralytics YOLOv8**: external ML runtime · Owns training, prediction, capture, and rendering behavior.

### Dataset pipeline

- **WIDER FACE source**: external dataset · Images and annotations from the referenced mirror.
- [Download and format](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/DownloadAndFormatDataset.py): Python entry point · Downloads WIDER FACE and converts annotations to YOLO labels. · `scripts/DownloadAndFormatDataset.py`
- **Train and val images**: generated image splits · YOLO image layout under the generated dataset root.
- **Normalized YOLO labels**: generated labels · One normalized class-0 face bounding-box label set per image.
- **Dataset contract**: YOLO dataset YAML · Defines root, image splits, and the single face class.

### Training

- **YOLOv8 nano base**: pretrained checkpoint · The documented yolov8n.pt fine-tuning starting point.
- [Train model](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/train.sh): shell entry point · Runs the documented 50-epoch, 640px fine-tuning job. · `scripts/train.sh`
- **Detection training run**: Ultralytics experiment · Consumes the dataset contract and produces model artifacts.
- **Selected detector**: trained checkpoint · Typically the best.pt model used for prediction.

### Live inference

- [Run webcam inference](https://github.com/ak2042/yolo_face_detection/blob/main/scripts/infer.sh): shell entry point · Starts live prediction with the trained detector. · `scripts/infer.sh`
- **Local camera**: video source · Default live input source.
- **Rendered detections**: live output · On-screen face bounding boxes rendered by Ultralytics.

## Connections

- Python dependencies → Ultralytics YOLOv8: installs
- WIDER FACE source → Download and format: downloads
- Download and format → Train and val images: materializes
- Download and format → Normalized YOLO labels: converts annotations to
- Download and format → Dataset contract: writes
- Train and val images → Dataset contract: referenced as splits
- Normalized YOLO labels → Dataset contract: aligns with contract
- Train model → Ultralytics YOLOv8: invokes CLI
- Dataset contract → Detection training run: configures
- YOLOv8 nano base → Detection training run: fine-tunes
- Ultralytics YOLOv8 → Detection training run: executes
- Detection training run → Selected detector: selects
- Run webcam inference → Ultralytics YOLOv8: invokes prediction
- Selected detector → Run webcam inference: loads
- Local camera → Ultralytics YOLOv8: supplies frames
- Ultralytics YOLOv8 → Rendered detections: renders detections
