This project demonstrates how to monitor queues and count people using YOLOv8 (from Ultralytics) with a single video, by simulating a multi-camera setup.
π½οΈ Video Credit: March Networks on YouTube π― Goal: Track and count people in two queue zones with real-time visualization.
- People detection and tracking using YOLOv8m
- Real-time zone-based counting
- Estimation of waiting time for each person
- Simulated 2-camera view from a single video
- Annotated video output with bounding boxes, labels, and statistics
Install the necessary packages:
pip install ultralytics opencv-python imageio matplotlibβββ main.py # Core processing and annotation logic
βββ input/
β βββ que.mp4 # Input video
βββ output/
β βββ step4_fully_customizable_output.mp4
βββ display.py # Code to visualize middle frames
βββ README.md-
The original video shows a top-down camera above a cashier counter.
-
Since only one video is available, it is logically split into two zones:
- Camera 1 Zone (Left): Where people enter
- Camera 2 Zone (Right): Where people reach the cashier
-
Using YOLOv8 tracking IDs, each person is tracked across both zones to avoid double-counting.
- Load Video
- Split frame into two logical zones
- Track people using YOLOv8 with object ID persistence
- Count individuals in each zone
- Estimate wait time in queue
- Overlay statistics and bounding boxes
- Save annotated video
from IPython.display import HTML
HTML(display_middle_300("output/step4_fully_customizable_output.mp4").to_html5_video())get_video_details(path): Shows video metadatadisplay_middle_300(path): Preview video using 300 central framesis_inside_area(): Determines if person is in cashier/queue areagenerate_color(id): Assigns consistent color per person IDboxes_intersect(): Checks bounding box overlap with zone
- Model:
yolov8m.pt - Class:
0(person) - Confidence Threshold: Auto-handled by model default
- Tracking: Enabled (
model.track())
The wait time analysis in this project focuses on measuring how long each person spends in the queue β from the moment they enter a defined βqueue areaβ until they reach the cashier.
-
Unique ID Tracking Each person is assigned a persistent ID by the YOLOv8 tracker.
-
First Seen Frame The system records the frame number when each personβs ID is first detected in the queue area:
if track_id not in id_first_seen: id_first_seen[track_id] = frame_number
-
Time Calculation For every frame after, the time waited is calculated as:
time_waited = (current_frame - first_seen_frame) / fps
-
Queue Zone Filtering Time is only counted if the person is:
- In the queue area (not random walking)
- Not yet at the cashier area
-
Real-Time Overlay On the video, the system displays:
ID,Confidence, andWait Time- Highlighted zones for Queue and Cashier
- β Customer Flow Insight β Understand how long customers are waiting
- β Queue Management β Detect bottlenecks in real-time
- β Performance Monitoring β Evaluate service efficiency at counters
- β No Duplication β IDs ensure people are counted only once
A person in the queue might get a label like:
ID 12 | 87.3% | Queue | 9.5s
This shows:
- Their unique ID
- Detection confidence
- Current zone: Queue
- Total time spent in queue so far
results = model.track(frame, persist=True, classes=[0], verbose=False)
for box in results[0].boxes:
if box.id is None:
continue
# Extract tracking ID, bounding box, and confidence- The final annotated video is saved at:
/kaggle/working/step4_fully_customizable_output.mp4
It includes:
- Bounding boxes with ID and confidence
- Zone-based person counts
- Wait time for each tracked person
- Visualized cashier and queue areas
This project is licensed under the terms of the LICENSE.txt. See the file for details.