# sahiliot/cnc_detection architecture

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

- Interactive diagram: https://gitdiagram.com/sahiliot/cnc_detection
- Repository: https://github.com/sahiliot/cnc_detection

## Overview

This is an on-premises CNC work-area monitoring system. An operator configures machines and zone limits in the dashboard, which persists settings and asks the backend to start monitoring; the backend reads camera streams, detects and tracks people, evaluates zone occupancy and safety conditions, records events, and serves annotated video and status back to the dashboard. The graph preserves the distinct capture, inference, zone, rules, presentation, and event stages. Source excerpts are partial, so some documented capabilities are shown without speculative internal call links.

Main entry points: [`cnc_backend/camera/worker.py`](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/camera/worker.py) (Camera worker), [`cnc_backend/monitoring/api.py`](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/monitoring/api.py) (Detection API), [`cnc_frontend/web/app.py`](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/web/app.py) (Dashboard service), [`cnc_frontend/monitoring/adapter.py`](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/monitoring/adapter.py) (Backend adapter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_dashboard["Dashboard"]
  node_dashboard_app["Dashboard service<br/>[app.py]"]
  node_dashboard_ui["Dashboard pages<br/>[dashboard.html]"]
  node_machine_db[("Machine settings<br/>[db.py]")]
  node_backend_client["Backend adapter<br/>[adapter.py]"]
  node_event_ui["Machine and event views"]
end

subgraph group_capture["Capture and Detection"]
  node_config["Backend configuration<br/>[config.py]"]
  node_reader["Stream reader<br/>[reader.py]"]
  node_worker["Camera worker<br/>[worker.py]"]
  node_person_detection["Person tracking<br/>[person_detector.py]"]
  node_pose_detection["Pose detection<br/>[pose_detector.py]"]
  node_phone_detection["Phone detection<br/>[phone_detector.py]"]
end

subgraph group_analysis["Zones and Safety"]
  node_zone_manager["Zone manager<br/>[zone_manager.py]"]
  node_safety_rules["Safety rules<br/>[rules.py]"]
  node_track_state["Track matching<br/>[track_state.py]"]
end

subgraph group_output["Monitoring and Records"]
  node_backend_api["Detection API<br/>[api.py]"]
  node_renderer["Video renderer<br/>[renderer.py]"]
  node_event_manager[("Event history<br/>[event_manager.py]")]
end

node_operator(("Operator"))
node_camera_source(("Camera or NVR"))

node_operator -->|"configures"| node_dashboard_app
node_dashboard_app -.->|"serves"| node_dashboard_ui
node_dashboard_app -->|"reads and writes"| node_machine_db
node_dashboard_app -->|"dispatches requests"| node_backend_client
node_backend_client -->|"calls over HTTP"| node_backend_api
node_dashboard_app -.->|"serves"| node_event_ui
node_camera_source -->|"provides stream"| node_reader
node_backend_api -->|"uses settings"| node_config
node_backend_api -->|"starts and stops"| node_worker
node_backend_api -->|"manages zones"| node_zone_manager
node_backend_api -->|"serves event data"| node_event_manager
node_worker -->|"reads frames"| node_reader
node_worker -.->|"tracks people"| node_person_detection
node_worker -->|"evaluates posture"| node_pose_detection
node_worker -->|"checks for phones"| node_phone_detection
node_worker -->|"assigns zones"| node_zone_manager
node_worker -.->|"evaluates conditions"| node_safety_rules
node_safety_rules -->|"matches tracks"| node_track_state
node_safety_rules -->|"logs violations"| node_event_manager
node_worker -->|"renders frames"| node_renderer
node_worker -->|"captures screenshots"| node_event_manager
node_renderer -.->|"provides video"| node_backend_api
node_backend_client -->|"fetches status and zones"| node_backend_api

click node_dashboard_app "https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/web/app.py"
click node_dashboard_ui "https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/templates/dashboard.html"
click node_machine_db "https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/database/db.py"
click node_backend_client "https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/monitoring/adapter.py"
click node_backend_api "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/monitoring/api.py"
click node_config "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/config.py"
click node_reader "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/camera/reader.py"
click node_worker "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/camera/worker.py"
click node_person_detection "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/person_detector.py"
click node_pose_detection "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/pose_detector.py"
click node_phone_detection "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/phone_detector.py"
click node_zone_manager "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/zones/zone_manager.py"
click node_safety_rules "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/safety/rules.py"
click node_track_state "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/tracking/track_state.py"
click node_renderer "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/display/renderer.py"
click node_event_manager "https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/events/event_manager.py"
click node_event_ui "https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/templates/machine_preview.html"

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_dashboard_app,node_dashboard_ui,node_machine_db,node_backend_client,node_event_ui toneBlue
class node_config,node_reader,node_worker,node_person_detection,node_pose_detection,node_phone_detection toneAmber
class node_zone_manager,node_safety_rules,node_track_state toneMint
class node_backend_api,node_renderer,node_event_manager toneRose
class node_operator,node_camera_source toneIndigo
```

## Components

### Dashboard

- [Dashboard service](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/web/app.py): `cnc_frontend/web/app.py`
- [Dashboard pages](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/templates/dashboard.html): `cnc_frontend/templates/dashboard.html`
- [Machine settings](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/database/db.py): `cnc_frontend/database/db.py`
- [Backend adapter](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/monitoring/adapter.py): `cnc_frontend/monitoring/adapter.py`
- [Machine and event views](https://github.com/sahiliot/cnc_detection/blob/main/cnc_frontend/templates/machine_preview.html): `cnc_frontend/templates/machine_preview.html`

### Capture and Detection

- [Backend configuration](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/config.py): `cnc_backend/config.py`
- [Stream reader](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/camera/reader.py): `cnc_backend/camera/reader.py`
- [Camera worker](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/camera/worker.py): `cnc_backend/camera/worker.py`
- [Person tracking](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/person_detector.py): `cnc_backend/detection/person_detector.py`
- [Pose detection](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/pose_detector.py): `cnc_backend/detection/pose_detector.py`
- [Phone detection](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/detection/phone_detector.py): `cnc_backend/detection/phone_detector.py`

### Zones and Safety

- [Zone manager](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/zones/zone_manager.py): `cnc_backend/zones/zone_manager.py`
- [Safety rules](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/safety/rules.py): `cnc_backend/safety/rules.py`
- [Track matching](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/tracking/track_state.py): `cnc_backend/tracking/track_state.py`

### Monitoring and Records

- [Detection API](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/monitoring/api.py): `cnc_backend/monitoring/api.py`
- [Video renderer](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/display/renderer.py): `cnc_backend/display/renderer.py`
- [Event history](https://github.com/sahiliot/cnc_detection/blob/main/cnc_backend/events/event_manager.py): `cnc_backend/events/event_manager.py`

### Other components

- **Operator**
- **Camera or NVR**

## Connections

- Operator → Dashboard service: configures (evidence: `README.md`)
- Dashboard service → Dashboard pages: serves
- Dashboard service → Machine settings: reads and writes (evidence: `cnc_frontend/web/app.py`)
- Dashboard service → Backend adapter: dispatches requests (evidence: `cnc_frontend/web/app.py`)
- Backend adapter → Detection API: calls over HTTP (evidence: `README.md`)
- Dashboard service → Machine and event views: serves
- Camera or NVR → Stream reader: provides stream (evidence: `README.md`)
- Detection API → Backend configuration: uses settings (evidence: `cnc_backend/monitoring/api.py`)
- Detection API → Camera worker: starts and stops (evidence: `cnc_backend/monitoring/api.py`)
- Detection API → Zone manager: manages zones (evidence: `cnc_backend/monitoring/api.py`)
- Detection API → Event history: serves event data (evidence: `cnc_backend/monitoring/api.py`)
- Camera worker → Stream reader: reads frames (evidence: `cnc_backend/camera/worker.py`)
- Camera worker → Person tracking: tracks people
- Camera worker → Pose detection: evaluates posture (evidence: `cnc_backend/camera/worker.py`)
- Camera worker → Phone detection: checks for phones (evidence: `cnc_backend/camera/worker.py`)
- Camera worker → Zone manager: assigns zones (evidence: `cnc_backend/camera/worker.py`)
- Camera worker → Safety rules: evaluates conditions (evidence: `cnc_backend/camera/worker.py`)
- Safety rules → Track matching: matches tracks (evidence: `cnc_backend/safety/rules.py`)
- Safety rules → Event history: logs violations (evidence: `cnc_backend/safety/rules.py`)
- Camera worker → Video renderer: renders frames (evidence: `cnc_backend/camera/worker.py`)
- Camera worker → Event history: captures screenshots (evidence: `cnc_backend/camera/worker.py`)
- Video renderer → Detection API: provides video
- Backend adapter → Detection API: fetches status and zones (evidence: `cnc_frontend/monitoring/adapter.py`)
