# himaenshuu/crop_weed_detection-app architecture

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

- Interactive diagram: https://gitdiagram.com/himaenshuu/crop_weed_detection-app
- Repository: https://github.com/himaenshuu/crop_weed_detection-app

## Overview

##### Project type and purpose
- This is a full-stack, single-page Flask web application whose primary purpose is to let a user upload an image via a browser, run object-detection inference with a pre-trained YOLO model (`best.pt`), and return the annotated image.

##### File structure
- Top-level:
- `app.py` – main Flask application with route(s) and business logic.
- `best.pt` – the pre-trained Ultralytics YOLO model file.
- `requirements.txt` – Python dependencies (Flask, ultralytics, opencv-python).
- `render.yaml` – deployment/config (e.g., Helm chart or CI/CD pipeline settings).
- `templates/index.html` – HTML template for the upload form and result display.
- Implicit directory:
- `uploads/` – created at runtime to store uploaded images and processed outputs.

##### Key insights from the README
- The app initializes an `UPLOAD_FOLDER` directory if absent.
- On receiving an HTTP POST with an image, it:
a. Saves the raw file
b. Feeds it into the YOLO model via the `ultralytics` API (with OpenCV support)
d. Stores or streams the annotated image back to the template

##### Main components
a. **User Browser (Client)**
- Renders `index.html`
- Sends HTTP GET for the form, HTTP POST for the image
b. **Flask Web Server**
- `app.py` with two routes:
1. GET `/` → return upload form
2. POST `/detect` → handle file, invoke inference
- Manages the filesystem (uploads folder)
c. **File Storage (Local Disk)**
- Raw uploads directory
- Processed output directory
d. **YOLO Inference Engine**
- Ultralytics YOLO loaded with `best.pt`
- Invoked via Python API
e. **Image Processing Library**
- OpenCV (`cv2`) for I/O and drawing
f. **Deployment/Config**
- `render.yaml` for containerization, environment variables, resource limits

##### Relationships & data-flow
1. Client → HTTP GET → Flask
2. Flask renders `index.html` → Client
3. Client selects image & HTTP POST → Flask `/detect`
4. Flask saves file → Local disk
5. Flask calls YOLO inference → loads `best.pt` model into memory once at startup
7. Processed image saved/streamed → Flask sends URL or image bytes back to template
8. Client displays annotated image

##### Architectural patterns & design principles
- **Separation of concerns**:
- Presentation (HTML template) vs. Application logic (`app.py`) vs. Model (YOLO file)
- **Singleton Model Loader**: YOLO model loaded once to avoid repeated startup overhead
- **File-based message passing**: using the local file system as a buffer between HTTP layer and inference engine
- **Stateless endpoints**: each request is independent; no database needed

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Actor
    Browser["User Browser"]:::external
    %% Backend Services
    Flask["Flask Web Server\n(app.py)"]:::backend
    YOLO["YOLO Inference Engine\n(best.pt)\n(Model loaded at startup)"]:::service
    OpenCV["Image Processing Library\n(OpenCV & Ultralytics API)"]:::service
    %% Storage
    Storage["Local File Storage\n(uploads/)"]:::storage
    %% Configuration & Docs
    Config["Deployment/Config\n(render.yaml)"]:::config
    Requirements["Dependencies\n(requirements.txt)"]:::config
    README["Project Documentation\n(README.md)"]:::docs
    License["License\n(LICENSE)"]:::docs
    Gitignore["Source Control Ignore\n(.gitignore)"]:::docs

    %% Data Flows
    Browser -->|GET /| Flask
    Flask -->|renders index.html| Browser
    Browser -->|POST /detect| Flask
    Flask -->|save raw image| Storage
    Flask -->|load model| YOLO
    Flask -->|invoke inference| YOLO
    YOLO -->|bounding boxes| Flask
    Flask -->|draw boxes| OpenCV
    OpenCV -->|save annotated image| Storage
    Storage -->|read output| Flask
    Flask -->|respond image| Browser

    %% Configuration & Docs References
    Flask --> Requirements
    Flask --> Config
    Flask --> README
    Flask --> License
    Flask --> Gitignore

    %% Click Events
    click Browser "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/templates/index.html"
    click Flask "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/app.py"
    click Storage "https://github.com/himaenshuu/crop_weed_detection-app/tree/main/uploads/"
    click YOLO "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/best.pt"
    click Requirements "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/requirements.txt"
    click Config "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/render.yaml"
    click README "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/README.md"
    click License "https://github.com/himaenshuu/crop_weed_detection-app/tree/main/LICENSE"
    click Gitignore "https://github.com/himaenshuu/crop_weed_detection-app/blob/main/.gitignore"

    %% Styles
    classDef external fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#000,stroke-width:1px
    classDef service fill:#98FB98,stroke:#000,stroke-width:1px
    classDef storage fill:#FFFF99,stroke:#000,stroke-width:1px
    classDef config fill:#D3D3D3,stroke:#000,stroke-width:1px
    classDef docs fill:#F0F0F0,stroke:#000,stroke-width:1px
```
