# nguyenhoangminh1312/potato_counting architecture

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

- Interactive diagram: https://gitdiagram.com/nguyenhoangminh1312/potato_counting
- Repository: https://github.com/nguyenhoangminh1312/potato_counting

## Overview

#### Purpose and entry points
This is a Python computer-vision pipeline for detecting, tracking, and counting potatoes crossing a defined conveyor-belt region. It has three executable stages:
- Dataset preparation: `dataset.py`
- YOLO model training: `train.py`
- Video inference, tracking, zone counting, and output rendering: `main.py`

The included media artifacts provide a sample input and generated demonstration: `potatoes_input_vid.mp4` and `object_counting_output.avi`.

#### Dataset preparation boundary
`dataset.py` is the data-ingestion/preparation boundary between the externally downloaded dataset and training. The dataset itself is not present in the repository; it must be downloaded from the Google Drive location referenced in `README.md` and extracted into the working environment. Parameters in `dataset.py` determine how source annotations/images are made available to training.

#### Model training
`train.py` trains a custom YOLOv11 detector using the prepared potato dataset. Its output is trained model weights, stored at a path that must later be configured for inference. This separates offline, compute-intensive model development from operational counting:
- Input: prepared dataset from `dataset.py`
- Output: YOLO weights consumed by `main.py`
- Training runtime: Python with Ultralytics and PyTorch (`train.py`, `README.md`)

#### Inference and tracking application
`main.py` is the runtime application. It loads configured trained weights, reads video frames, executes YOLO detection and object tracking, and maintains object identity across frames. Tracking is essential to prevent repeatedly counting the same potato while it remains visible.

The supplied video `potatoes_input_vid.mp4` is the likely local source for this flow, though paths are configured in code rather than represented as separate application configuration.

#### Polygon-zone counting stage
Counting is constrained to a polygonal region configured in `main.py`. The polygon coordinates are manually derived from a representative camera frame using the external Roboflow PolygonZone tool. The application monitors tracked object movement relative to this region and updates the count when tracked potatoes meet the zone-crossing/counting condition.

This creates a hard operational dependency on a fixed camera angle: region coordinates and trained visual assumptions must remain aligned with the conveyor view.

#### Output and visualization
`main.py` renders detection/tracking/count overlays and writes a processed video artifact. `object_counting_output.avi` is the committed example output. This output is intended for visual validation of detection quality, tracking continuity, polygon placement, and final count behavior.

#### Runtime and external dependencies
The material runtime is Linux, Python 3.10+, OpenCV for video I/O/rendering, Ultralytics for YOLO detection/tracking and object counting, and PyTorch for model execution/training. TensorFlow is listed as a prerequisite in `README.md`, but the repository structure does not establish it as part of the main execution path. External services are limited to Google Drive dataset distribution and Roboflow PolygonZone-assisted region-coordinate creation.

Main entry points: [`main.py`](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py) (Frame I/O), [`dataset.py`](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/dataset.py) (Dataset preparation), [`train.py`](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/train.py) (YOLOv11 training), [`potatoes_input_vid.mp4`](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/potatoes_input_vid.mp4) (Conveyor input video)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline model development"]
  node_dataset_prep["Dataset preparation<br/>Python stage<br/>[dataset.py]"]
  node_prepared_dataset[("Prepared potato dataset<br/>training data")]
  node_training["YOLOv11 training<br/>Python stage<br/>[train.py]"]
  node_weights["Trained YOLO weights<br/>model artifact"]
end

subgraph group_runtime["Counting runtime"]
  node_input_video["Conveyor input video<br/>media artifact"]
  node_counting_app["Counting application<br/>Python runtime<br/>[main.py]"]
  node_video_io["Frame I/O<br/>OpenCV layer<br/>[main.py]"]
  node_detection_tracking["Detection &amp; tracking<br/>Ultralytics YOLO<br/>[main.py]"]
  node_polygon_zone{{"Polygon-zone counting<br/>counting layer<br/>[main.py]"}}
  node_rendering["Overlay rendering<br/>OpenCV layer<br/>[main.py]"]
  node_output_video["Processed output video<br/>media artifact"]
  node_camera_alignment["Fixed camera alignment<br/>operational constraint<br/>[main.py]"]
end

subgraph group_external["External inputs &amp; tools"]
  node_gdrive[("Google Drive dataset<br/>external dataset")]
  node_polygon_tool["Roboflow PolygonZone<br/>annotation tool"]
end

node_gdrive -.->|"downloaded source"| node_dataset_prep
node_dataset_prep -->|"prepares"| node_prepared_dataset
node_prepared_dataset -->|"training input"| node_training
node_training -->|"produces"| node_weights
node_input_video -->|"frames"| node_video_io
node_video_io -->|"frame stream"| node_counting_app
node_weights -->|"configured model"| node_counting_app
node_counting_app -->|"runs"| node_detection_tracking
node_detection_tracking -->|"tracked objects"| node_polygon_zone
node_polygon_tool -.->|"zone coordinates"| node_polygon_zone
node_polygon_zone -->|"count and zone state"| node_rendering
node_detection_tracking -->|"detections and tracks"| node_rendering
node_rendering -->|"annotated frames"| node_video_io
node_video_io -->|"writes"| node_output_video
node_camera_alignment -.->|"visual assumptions"| node_detection_tracking
node_camera_alignment -.->|"coordinate alignment"| node_polygon_zone

click node_dataset_prep "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/dataset.py"
click node_training "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/train.py"
click node_input_video "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/potatoes_input_vid.mp4"
click node_counting_app "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"
click node_video_io "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"
click node_detection_tracking "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"
click node_polygon_zone "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"
click node_rendering "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"
click node_output_video "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/object_counting_output.avi"
click node_camera_alignment "https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py"

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_dataset_prep,node_prepared_dataset,node_training,node_weights toneBlue
class node_input_video,node_counting_app,node_video_io,node_detection_tracking,node_polygon_zone,node_rendering,node_output_video,node_camera_alignment toneAmber
class node_gdrive,node_polygon_tool toneMint
```

## Components

### Offline model development

- [Dataset preparation](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/dataset.py): Python stage · Makes source images and annotations available to training. · `dataset.py`
- **Prepared potato dataset**: training data
- [YOLOv11 training](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/train.py): Python stage · Ultralytics and PyTorch training runtime. · `train.py`
- **Trained YOLO weights**: model artifact · Configured as the detector input for counting.

### Counting runtime

- [Conveyor input video](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/potatoes_input_vid.mp4): media artifact · `potatoes_input_vid.mp4`
- [Counting application](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): Python runtime · Loads weights and coordinates the frame processing flow. · `main.py`
- [Frame I/O](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): OpenCV layer · Reads source frames and writes rendered video. · `main.py`
- [Detection & tracking](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): Ultralytics YOLO · Maintains identities across frames to avoid repeat counts. · `main.py`
- [Polygon-zone counting](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): counting layer · Counts tracked potatoes meeting the configured zone condition. · `main.py`
- [Overlay rendering](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): OpenCV layer · Draws detections, tracks, zone, and count. · `main.py`
- [Processed output video](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/object_counting_output.avi): media artifact · Visual validation artifact for count behavior and overlay placement. · `object_counting_output.avi`
- [Fixed camera alignment](https://github.com/nguyenhoangminh1312/potato_counting/blob/main/main.py): operational constraint · Camera view must remain aligned with trained assumptions and polygon coordinates. · `main.py`

### External inputs & tools

- **Google Drive dataset**: external dataset · Downloaded and extracted locally before preparation.
- **Roboflow PolygonZone**: annotation tool · Used to derive fixed conveyor-region coordinates.

## Connections

- Google Drive dataset → Dataset preparation: downloaded source
- Dataset preparation → Prepared potato dataset: prepares
- Prepared potato dataset → YOLOv11 training: training input
- YOLOv11 training → Trained YOLO weights: produces
- Conveyor input video → Frame I/O: frames
- Frame I/O → Counting application: frame stream
- Trained YOLO weights → Counting application: configured model
- Counting application → Detection & tracking: runs
- Detection & tracking → Polygon-zone counting: tracked objects
- Roboflow PolygonZone → Polygon-zone counting: zone coordinates
- Polygon-zone counting → Overlay rendering: count and zone state
- Detection & tracking → Overlay rendering: detections and tracks
- Overlay rendering → Frame I/O: annotated frames
- Frame I/O → Processed output video: writes
- Fixed camera alignment → Detection & tracking: visual assumptions
- Fixed camera alignment → Polygon-zone counting: coordinate alignment
