Project Description:
This project implements a smart real-time counting system using the YOLOv8 deep learning model to detect, track, and count objects (like people or animals such as sheep or any other object) as they cross a designated virtual line in a video feed. Unlike simple counters, this version introduces a line segment, allowing you to limit counting only when objects pass through a specific part of the frame — useful in constrained environments like conveyor belts, doors, or fencing exits.
Using Ultralytics YOLOv8 with OpenCV and CVZone, the system identifies each object, assigns a persistent ID, and detects when the object crosses the line from top to bottom within the defined horizontal bounds. A cumulative “OUT” count is maintained and displayed in real time.
Core Objectives:
-
Detect and track multiple objects in real time using a custom-trained YOLOv8 model.
-
Count objects only when they cross a specific segment of a virtual line (not the full width).
-
Avoid double-counting by maintaining ID history and direction detection.
-
Visually annotate the video with labels, line, centroid, and counters.
Key Features:
-
✅ Customizable Counting Zone: Count only when objects cross a segment (line with X boundaries).
-
🧠 Persistent Object ID Tracking: Each object is uniquely identified and followed across frames.
-
🎨 Visual Feedback: Real-time drawing of bounding boxes, line segment, centroids, and total count.
-
🎯 Direction-Aware Counting: Only counts objects crossing the line from top to bottom.
-
🐑 Works with Any Class: Although tested with sheep, it works for any detectable class like people, cars, etc.
Technologies Used:
| Component | Details |
|---|---|
| Language | Python 3.x |
| Model | YOLOv8 (best.pt, custom-trained) |
| Libraries | ultralytics, cv2, cvzone, os, NumPy |
| Tracking | Built-in YOLOv8.track() with ID persistence |
Use Cases:
-
🐑 Sheep Exit Counter: Count sheep leaving a barn or pen.
-
🧍♂️ People Flow Monitor: Monitor people exiting through a doorway or alley.
-
🛍️ Retail Analytics: Count customers exiting a store within a specific zone.
-
🚧 Construction Zones: Count entries/exits through site gates.
-
🛤️ Conveyor Belt Object Tracking: Count items passing through a defined checkpoint.
Possible Enhancements:
-
🔁 Add “IN” counter for reverse (bottom-to-top) movement.
-
💾 Save annotated video using
cv2.VideoWriter. -
🔔 Trigger alarms or actuators when counts exceed limits.
-
📈 Log data to CSV or cloud dashboards (e.g., Firebase, InfluxDB).
-
📍 Extend to multiple lines or zones for complex flow monitoring.
Conclusion:
This smart directional counting system demonstrates an effective real-time solution for flow analysis in controlled environments. It enhances standard object detection by integrating motion analysis, ID tracking, and spatial filtering, making it highly applicable in agriculture, retail, logistics, and security.


Reviews
There are no reviews yet.