How nguyenhoangminh1312/potato_counting works
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 Frame I/O · dataset.py Dataset preparation · train.py YOLOv11 training · potatoes_input_vid.mp4 Conveyor input video
Components 14
Offline model development
- Dataset preparation · Makes source images and annotations available to training.
dataset.py - Prepared potato dataset
- YOLOv11 training · Ultralytics and PyTorch training runtime.
train.py - Trained YOLO weights · Configured as the detector input for counting.
Counting runtime
- Conveyor input video
potatoes_input_vid.mp4 - Counting application · Loads weights and coordinates the frame processing flow.
main.py - Frame I/O · Reads source frames and writes rendered video.
main.py - Detection & tracking · Maintains identities across frames to avoid repeat counts.
main.py - Polygon-zone counting · Counts tracked potatoes meeting the configured zone condition.
main.py - Overlay rendering · Draws detections, tracks, zone, and count.
main.py - Processed output video · Visual validation artifact for count behavior and overlay placement.
object_counting_output.avi - Fixed camera alignment · Camera view must remain aligned with trained assumptions and polygon coordinates.
main.py
External inputs & tools
- Google Drive dataset · Downloaded and extracted locally before preparation.
- Roboflow PolygonZone · Used to derive fixed conveyor-region coordinates.
Connections 16
- 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