# phamdangnguyen/yolov5_animals architecture

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

- Interactive diagram: https://gitdiagram.com/phamdangnguyen/yolov5_animals
- Repository: https://github.com/phamdangnguyen/yolov5_animals

## Overview

##### Project type and purpose
- This is a standalone computer‑vision application built on YOLOv5 for detecting 25 different animal classes in video streams or files.
- It includes two primary use cases:
- Inference: A desktop GUI app (Main.py + Screen.py/Screen.ui) for loading videos, running detection, displaying/saving results.
- Training: A Jupyter notebook (Train_model.ipynb) to train or fine‑tune the YOLOv5 model on 8,000 labeled images.

##### File structure
- Top‑level scripts:
- Main.py – application entry point (likely uses PyInstaller .spec to bundle).
- Screen.py + Screen.ui – GUI controller and Qt Designer form.
- Model resources:
- best_8_tieng.pt – the trained YOLOv5 weights.
- Data inputs:
- Labeled_Video.avi, Animals.mp4, Buom.mp4 – sample video inputs.
- Output/storage:
- Save_pic/ – folder where detected frames or snapshots are saved.
- Training artifacts:
- Train_model.ipynb – Jupyter notebook for training.
- Deployment:
- Main.spec – PyInstaller configuration to package the app.

##### Extract additional insights from README
- Confirms YOLOv5 usage for animal detection.
- Mentions training duration (~8 hours), dataset size (25 classes, 8k images).

##### Main components and how to represent them
a. Components
– User Interface Layer
- GUI (Screen.ui + Screen.py)
– Application Core
- Main.py (app bootstrap, ties UI and inference)
- Inference Engine (YOLOv5 model loader + frame processor)
– Data Layer
- Video Input (file system or camera)
- Save_pic directory (output snapshots)
- Model Storage (best_8_tieng.pt)
– Training Module (Jupyter notebook + dataset)
– External Dependencies
- PyTorch (model inference & training)
- OpenCV (video I/O, image processing)
- PyQt5 (GUI)
- PyInstaller (packaging)

b. Relationships & interactions
– The User interacts with the GUI -> GUI sends “start detection” command to Main.
– Main bootstraps the Inference Engine, loads best_8_tieng.pt via PyTorch.
– Inference Engine reads frames from Video Input via OpenCV, runs YOLOv5 detection.
– Detected frames are passed back to the GUI for display and saved to Save_pic.
– Separately, the Training Module (Train_model.ipynb) reads the same labeled dataset, uses PyTorch & YOLOv5 training scripts to produce best_8_tieng.pt.
– PyInstaller (Main.spec) consumes Main.py + dependencies and bundles them into a standalone executable.

c. Architectural patterns & design principles
– MVC‑like separation:
- View = Screen.ui
- Controller = Screen.py / Main.py
- Model = YOLOv5 weights + inference logic
– Modular separation of concerns: inference vs. training vs. packaging
– Data flow is unidirectional: Input → Processing → Output

d. Key technologies/frameworks
– YOLOv5 (PyTorch)
– PyQt5 (GUI)
– OpenCV (Video/Frame I/O)
– Jupyter / IPython for training
– PyInstaller for deployment

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External user
    User["User"]:::external

    %% User Interface Layer
    subgraph "User Interface Layer"
        ScreenUI["GUI Form (Screen.ui)"]:::ui
        ScreenPY["GUI Controller (Screen.py)"]:::ui
    end

    %% Application Core
    subgraph "Application Core"
        Main["App Bootstrap (Main.py)"]:::core
        Inference["Inference Engine"]:::core
    end

    %% Data Layer
    subgraph "Data Layer"
        Animals["Animals.mp4"]:::data
        Buom["Buom.mp4"]:::data
        LabeledVid["Labeled_Video.avi"]:::data
        SavePic[(Save_pic)]:::data
        ModelFile[(best_8_tieng.pt)]:::data
    end

    %% Training Module
    subgraph "Training Module"
        Train["Train_model.ipynb"]:::core
    end

    %% Deployment
    subgraph "Deployment"
        Spec["PyInstaller Config (Main.spec)"]:::core
    end

    %% Documentation
    Doc["README.md"]:::data

    %% External Dependencies
    subgraph "External Dependencies"
        PT["PyTorch"]:::ext
        CV["OpenCV"]:::ext
        QT["PyQt5"]:::ext
        PI["PyInstaller"]:::ext
    end

    %% Data Flow & Interactions
    User -->|"interacts with"| ScreenUI
    User -->|"interacts with"| ScreenPY
    ScreenPY -->|"calls"| Main
    Main -->|"initializes"| Inference
    ModelFile -->|"loads weights"| Inference
    Animals -->|"frame input"| Inference
    Buom -->|"frame input"| Inference
    LabeledVid -->|"frame input"| Inference
    Inference -->|"display results"| ScreenPY
    Inference -->|"save snapshots"| SavePic
    Train -->|"outputs model"| ModelFile
    Train -->|"reads dataset"| LabeledVid
    Main -->|"packaged by"| Spec
    PT -->|"used by"| Inference
    PT -->|"used by"| Train
    CV -->|"used by"| Inference
    QT -->|"used by"| ScreenUI
    PI -->|"used by"| Spec

    %% Documentation link
    Doc --- Main

    %% Click Events
    click ScreenUI "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Screen.ui"
    click ScreenPY "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Screen.py"
    click Main "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Main.py"
    click Spec "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Main.spec"
    click ModelFile "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/best_8_tieng.pt"
    click Animals "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Animals.mp4"
    click Buom "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Buom.mp4"
    click LabeledVid "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Labeled_Video.avi"
    click SavePic "https://github.com/phamdangnguyen/yolov5_animals/tree/main/Big_exercise/Save_pic/"
    click Train "https://github.com/phamdangnguyen/yolov5_animals/blob/main/Big_exercise/Train_model.ipynb"
    click Doc "https://github.com/phamdangnguyen/yolov5_animals/blob/main/README.md"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#0062cc,color:#004085;
    classDef core fill:#d4edda,stroke:#155724,color:#155724;
    classDef data fill:#fff3cd,stroke:#856404,color:#856404;
    classDef ext fill:#e2e3e5,stroke:#6c757d,color:#383d41;
    classDef external fill:#f8d7da,stroke:#721c24,color:#721c24;
```
