# mnnlr/chess_vision_backend architecture

> Architecture diagram of the GitHub repository mnnlr/chess_vision_backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mnnlr/chess_vision_backend
- Repository: https://github.com/mnnlr/chess_vision_backend

## Overview

Key Components to Include:

##### Client Layer
- A note about image upload functionality

##### API Layer
- Main endpoint "/fen-string"
- Form-data handling for image upload

##### Core Processing Layer (divide into three main components)
- Board Detection Module (board.py)
- Chess Piece Detection Module (predict.py)
- FEN String Generation Module (fenstring.py)

##### ML Model Layer
- YOLOv7 Model component (stored in models/best.pt)
- PyTorch runtime environment

- [Client Layer]
- ↓
- [API Layer (Flask)]
- ↓
- [Core Processing Layer]
- ↓
- [ML Model Layer]

##### Component Relationships
- Client → Flask Server
- Flask Server → Board Detection
- Board Detection → Piece Detection
- Piece Detection → YOLOv7 Model
- Piece Detection → FEN Generator
- FEN Generator → Flask Server
- Flask Server → Client

##### Processing Steps
Numbered steps showing the process:
1. Image Upload
2. Board Detection & Cropping
3. Piece Detection using YOLOv7
4. FEN String Generation
5. Response Return

##### Technical Details
- Input: Image file (form-data)
- Output: JSON response with FEN string
- Model: YOLOv7 (best.pt)
- Framework: Flask
- Key Dependencies: OpenCV, PyTorch

##### Additional Elements
- Input/output data formats at each step
- System requirements in a side note

##### Optimization Notes
- Caching mechanisms
- Where parallel processing could occur
- Potential scaling points

- The clear separation between the API interface and processing logic
- The sequential nature of the image processing pipeline
- The integration of the ML model within the system
- The modular design allowing for component replacement or upgrade

Remember to:
- Keep the main flow clear and centered
- Necessary labels and annotations
- Maintain proper spacing between components
- Version information and date
- Relevant environment variables or configuration requirements

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Styles and classes
    classDef client fill:#3498db,stroke:#2980b9,color:white
    classDef api fill:#2ecc71,stroke:#27ae60,color:white
    classDef core fill:#f1c40f,stroke:#f39c12,color:black
    classDef ml fill:#e74c3c,stroke:#c0392b,color:white
    classDef error fill:#95a5a6,stroke:#7f8c8d,color:white

    %% Client Layer
    Client[Client Application]:::client

    %% API Layer
    subgraph API
        Flask[Flask Server]:::api
        Endpoint["/fen-string POST"]:::api
    end
    click Flask "https://github.com/mnnlr/Chess_Vision_backend/blob/main/app.py"

    %% Core Processing Layer
    subgraph CoreProcessing
        Board[Board Detection]:::core
        PieceDetection[Chess Piece Detection]:::core
        FENGen[FEN String Generation]:::core
    end
    click Board "https://github.com/mnnlr/Chess_Vision_backend/blob/main/chess/board.py"
    click PieceDetection "https://github.com/mnnlr/Chess_Vision_backend/blob/main/chess/predict.py"
    click FENGen "https://github.com/mnnlr/Chess_Vision_backend/blob/main/chess/fenstring.py"
    click CoreProcessing "https://github.com/mnnlr/Chess_Vision_backend/blob/main/chess/__init__.py"

    %% ML Model Layer
    subgraph MLLayer
        Model[YOLOv7 Model]:::ml
        PyTorch[PyTorch Runtime]:::ml
    end
    click MLLayer "https://github.com/mnnlr/Chess_Vision_backend/tree/main/models/"

    %% Dependencies
    Dependencies[System Dependencies]
    click Dependencies "https://github.com/mnnlr/Chess_Vision_backend/blob/main/requirements.txt"

    %% Relationships
    Client -->|1. Upload Image| Flask
    Flask --> Endpoint
    Endpoint -->|2. Process Request| Board
    Board -->|3. Cropped Board| PieceDetection
    PieceDetection -->|4. Image Analysis| Model
    Model -->|5. Piece Positions| PieceDetection
    PieceDetection -->|6. Piece Map| FENGen
    FENGen -->|7. FEN String| Endpoint
    Endpoint -->|8. JSON Response| Client
    PyTorch --- Model

    %% Error Paths
    Flask -.->|Error Response| Client
    Board -.->|Detection Failed| Endpoint
    PieceDetection -.->|Analysis Failed| Endpoint

    %% Legend
    subgraph Legend
        Client1[Client]:::client
        API1[API Component]:::api
        Core1[Core Processing]:::core
        ML1[ML Component]:::ml
        Error1[Error Path]:::error
    end
```
