# pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer architecture

> Architecture diagram of the GitHub repository pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer
- Repository: https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer

## Overview

##### Project type and purpose
- This is a full-stack real-time video-streaming application that integrates a YOLO object detection model with a Flask web server.
- It captures frames from a camera, runs YOLO inference, streams annotated frames to a browser, and sends control signals to industrial PLCs over OPC-UA.

##### File structure
- Top-level:
- run.py / wsgi.py — application entry points
- config.py + instance/config.py — configuration management
- models/ — YOLO model artifacts (.pt, .onnx, TensorRT engine)
- phantom_client.py — auxiliary PLC-client script
- app/ (Flask application):
- __init__.py — Flask app factory
- routes.py — HTTP endpoints (index, video stream)
- camera.py — Video capture & OPC-UA client logic
- detector.py — YOLO inference logic
- utils.py — shared helpers
- static/ (css, js) & templates/ (index.html, video.html) — front-end

##### Extract insights from the README
- Tech stack: Python 3.6+, Flask, OpenCV, Ultralytics YOLO, python-opcua, NumPy, optional Docker with GPU.
- Flow: capture → detect → post-process → stream → OPC-UA signals → browser.
- Docker packaging uses an Ultralytics base image, exposes port 5000, devices for camera & GPU.

##### Diagramming guidance
a. Main components
- Video Source (e.g., /dev/video0)
- Camera & OPC-UA Client module (app/camera.py)
- Detector module (app/detector.py + models/)
- Flask Web Server (app/routes.py + run.py/wsgi.py)
- Front-end (templates + static/js & static/css)
- PLC / OPC-UA Server
- Auxiliary script (phantom_client.py)
- Configuration store (config.py + instance/config.py)
b. Relationships & Interactions
1. Video Source → Camera module (frame fetch)
2. Camera → Detector (frame pass)
3. Detector → Camera (annotated frame return)
4. Camera/Detector → OPC-UA client → PLC (write node values)
5. Annotated frames → Flask streaming endpoint → Browser (video.html via JS)
6. Browser requests index.html → Flask → templates + static assets
7. run.py/wsgi.py wraps the Flask app and optionally Docker/Gunicorn
c. Architectural patterns / design principles
- Modular separation: capture, detection, web routes, utilities
- Streaming architecture: server-push MJPEG or multipart/x-mixin frames
- Dependency injection via app factory and config layers (instance/config.py)
- External service integration (PLC via OPC-UA)
d. Key technologies / frameworks
- Flask (routes, app factory)
- OpenCV (video capture)
- Ultralytics YOLO (torch, ONNX, TensorRT)
- python-opcua (PLC communication)
- Docker + GPU passthrough

##### Detailed checklist
- [ ] All sub-modules (camera.py, detector.py, routes.py, utils.py) shown
- [ ] Model storage (models/) indicated
- [ ] Config layers (config.py vs instance/config.py) captured
- [ ] Auxiliary phantom_client.py process (if relevant)
- [ ] Docker boundary with device mappings (if used)
- [ ] External system interactions: camera hardware, PLC server, browser client

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Devices
    CameraDevice["Physical Camera\n(/dev/video0)"]:::external
    PLCServer["PLC / OPC-UA Server\ntcp://<host>:4840"]:::external
    GPUDevice["GPU Device"]:::external
    BrowserClient["Browser Client"]:::external

    subgraph "Docker Container" 
        direction TB

        subgraph "Configuration"
            direction TB
            ConfigMain["config.py"]:::config
            ConfigInstance["instance/config.py"]:::config
        end

        subgraph "Flask Application"
            direction TB
            Factory["Flask App Factory\n(app/__init__.py)"]:::web
            Routes["HTTP Endpoints\n(app/routes.py)"]:::web
            Utils["Shared Utilities\n(app/utils.py)"]:::util
        end

        subgraph "Processing Modules"
            direction TB
            Camera["Camera & OPC-UA Client\n(app/camera.py)"]:::io
            Detector["YOLO Detector\n(app/detector.py)"]:::detection
            PLCClient["PLC Client Module\n(app/plc_client.py)"]:::io
            Models["Model Artifacts\n(models/)"]:::storage
        end

        subgraph "Front-End"
            direction TB
            Templates["HTML Templates\n(index.html, video.html)"]:::web
            StaticCSS["CSS Styles\n(app/static/css/style.css)"]:::web
            StaticJS["JS Client\n(app/static/js/main.js)"]:::web
        end

        subgraph "Entry Points"
            direction TB
            RunPy["run.py"]:::config
            WSGI["wsgi.py"]:::config
        end
    end

    %% External to Docker
    CameraDevice -->|frames| Camera
    GPUDevice -->|accelerates inference| Detector
    PLCServer -->|OPC-UA| PLCClient
    BrowserClient -->|HTTP GET /| Routes
    BrowserClient -->|MJPEG stream| Routes

    %% Internal Flow
    Camera -->|extract frame| Detector
    Detector -->|annotated frame| Camera
    Camera -->|stream frames| Routes
    Detector -->|metrics| Utils
    Utils -->|helpers| Camera
    Utils -->|helpers| Detector
    Camera -->|OPC-UA write| PLCClient
    PLCClient -->|OPC-UA write| PLCServer
    Routes -->|render| Templates
    Templates -->|loads| StaticCSS
    Templates -->|loads| StaticJS
    RunPy -->|start| Factory
    WSGI -->|start| Factory
    Factory -->|register| Routes
    Factory -->|load| ConfigMain
    Factory -->|load| ConfigInstance

    %% Click Events
    click Factory "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/__init__.py"
    click Routes "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/routes.py"
    click Camera "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/camera.py"
    click PLCClient "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/plc_client.py"
    click Detector "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/detector.py"
    click Models "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/tree/main/models/"
    click Utils "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/utils.py"
    click StaticCSS "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/static/css/style.css"
    click StaticJS "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/static/js/main.js"
    click Templates "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/app/templates/index.html"
    %% (video.html is part of templates directory)
    click ConfigMain "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/config.py"
    click ConfigInstance "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/instance/config.py"
    click RunPy "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/run.py"
    click WSGI "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/wsgi.py"
    click PHantom "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/phantom_client.py"
    click CheckDetect "https://github.com/pguillenortiz/https---github.com-pguillenortiz-yolo-flask-streamer/blob/main/check_detection.py"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef web fill:#bbf,stroke:#333,stroke-width:1px
    classDef io fill:#fbf,stroke:#333,stroke-width:1px
    classDef detection fill:#fbb,stroke:#333,stroke-width:1px
    classDef util fill:#bfb,stroke:#333,stroke-width:1px
    classDef config fill:#ffb,stroke:#333,stroke-width:1px
    classDef storage fill:#dbd,stroke:#333,stroke-width:1px
```
