Turn any camera overlooking a road or car park into an intelligent traffic analytics system. Count vehicles, classify them by type, generate flow heatmaps, and detect congestion — all in real time on the Jetson.
What you will learn
- How to detect and classify vehicles (car, truck, bus, motorbike, bicycle)
- How to count vehicles crossing a virtual line (tripwire counting)
- How to track individual vehicles across frames using ByteTrack
- How to generate positional heatmaps from traffic flow data
- How to export stats to CSV and visualise in Grafana
Step 1 — Run the traffic demo
cd ~/tutorials/08-traffic-analytics
python3 traffic.py --source 0 --show --count-line 400
A horizontal line appears at y=400 pixels. Every vehicle that crosses it is counted and classified. The running total is displayed in the corner.
Step 2 — Tripwire vehicle counting
from ultralytics import YOLO
from ultralytics.solutions import ObjectCounter
model = YOLO("yolov8n.engine")
counter = ObjectCounter(
names = model.names,
reg_pts= [(0, 400), (1280, 400)], # horizontal counting line
count_reg_color = (0, 255, 0)
)
while True:
frame = cap.read()[1]
results = model.track(frame, persist=True)
frame = counter.start_counting(frame, results)
cv2.imshow("Traffic", frame)
Step 3 — Generate a heatmap
import numpy as np
heatmap = np.zeros((720, 1280), dtype=np.float32)
def update_heatmap(detections):
for box in detections:
cx = int((box.xyxy[0][0] + box.xyxy[0][2]) / 2)
cy = int((box.xyxy[0][1] + box.xyxy[0][3]) / 2)
cv2.circle(heatmap, (cx, cy), 20, 1, -1)
# Normalise and overlay on frame
norm_heat = cv2.normalize(heatmap, None, 0, 255, cv2.NORM_MINMAX)
coloured = cv2.applyColorMap(norm_heat.astype(np.uint8), cv2.COLORMAP_JET)
✅ Next: Tutorial 9 — Robot Vision | Back to Jetson Kit