অধ্যায়Phase 8 · রিয়েল-ওয়ার্ল্ড প্রজেক্ট
8.3 14 মিনিট পড়া

Traffic Monitoring System

Vehicle count + speed estimation।

🎬 গল্প দিয়ে শুরু
ঢাকার যানজট — কোন রাস্তায় কতগুলো গাড়ি, কত গতিতে চলছে? Manual count impossible। একটি ক্যামেরা + AI দিয়ে সম্পূর্ণ traffic analytics system বানাবো — count, speed, classification, congestion।

Features

  • Vehicle classification — car, bus, truck, motorbike, CNG, rickshaw।
  • Direction-aware counting — line crossing।
  • Speed estimation (km/h)।
  • Lane occupancy ও congestion level।
  • Time-series dashboard — peak hour analytics।

Pipeline

text
Frame ► YOLOv8 (vehicle classes) ► ByteTrack
       ► Counter (line crossing per direction)
       ► Speed (pixel distance × calibration / Δt)
       ► Aggregate per minute → InfluxDB/Postgres
       ► Grafana dashboard

Line crossing counter

python
LINE_Y = 500             # horizontal line
prev_cy = {}             # track_id → previous center y
count_in, count_out = 0, 0

def update(tid, cx, cy, cls):
    global count_in, count_out
    py = prev_cy.get(tid)
    if py is not None:
        if py < LINE_Y <= cy:   count_in  += 1  # downward
        if py > LINE_Y >= cy:   count_out += 1  # upward
    prev_cy[tid] = cy
ROI line per lane
প্রতিটি lane-এর জন্য আলাদা line নিন — lane-wise count পাবেন।

Speed estimation

Pixel গতি → real-world গতিতে রূপান্তরের জন্য calibrationদরকার। সবচেয়ে সহজ: রাস্তার একটি known length (যেমন lane marking 3m) frame-এ মেপে scale বের করুন।

python
PPM = 8.0   # pixels per meter (calibrated)
FPS = 25

track_hist = {}   # tid → [(t, x, y), ...]

def speed_kmh(tid, t, x, y):
    h = track_hist.setdefault(tid, [])
    h.append((t, x, y))
    if len(h) < 5: return None
    h = h[-10:]                          # last 10 points
    (t0, x0, y0), (t1, x1, y1) = h[0], h[-1]
    dist_m = ((x1-x0)**2 + (y1-y0)**2) ** 0.5 / PPM
    dt     = t1 - t0
    return (dist_m / dt) * 3.6 if dt > 0 else None
Perspective error
ক্যামেরা যদি কোণাকুণি দেখে, দূরের গাড়ি কাছেরটার চেয়ে কম pixel-এ চলে — ভুল speed। সঠিক উপায়: Homography (Phase 3.3) দিয়ে bird's-eye view-এ project করে measure।

Bird's-eye projection

python
import cv2, numpy as np

# রাস্তার ৪টি reference point (image) → real-world meter
src = np.float32([[420,520],[860,520],[1180,710],[100,710]])
dst = np.float32([[0,0],[20,0],[20,40],[0,40]])   # 20m × 40m patch
H = cv2.getPerspectiveTransform(src, dst)

# track point কে world coord-এ project
wx, wy = cv2.perspectiveTransform(
    np.array([[[cx, cy]]], "float32"), H)[0][0]
# meter-এ Euclidean দিয়ে accurate speed

Class mapping (COCO → BD vehicle)

python
MAP = {2: "car", 3: "motorbike", 5: "bus", 7: "truck"}
# CNG/rickshaw COCO-তে নেই → custom YOLO fine-tune
# 500–1000টি local labelled image যথেষ্ট (Phase 5.5)

Aggregation & storage

python
# প্রতি মিনিটে এক row
INSERT INTO traffic_stats(ts, lane, vclass, count, avg_speed)
VALUES (now(), 'lane_1', 'car', 42, 38.5);

# Grafana query: hourly traffic by class
SELECT time_bucket('1h', ts) AS hr, vclass, sum(count)
FROM traffic_stats GROUP BY hr, vclass;

Advanced

  • Wrong-way detection — track direction vs allowed direction।
  • Helmet detection (motorbike rider) — extra YOLO class।
  • ANPR (Number plate) — EasyOCR/PaddleOCR on plate crop।
  • Congestion heatmap — average occupancy per ROI।
  • Emergency vehicle priority — siren color detect।
প্র্যাকটিস টাস্ক
  1. YouTube traffic video-তে line crossing counter চালান।
  2. Homography দিয়ে speed estimate করুন, real GPS-এর সাথে compare।
  3. Custom YOLO fine-tune করে CNG/rickshaw class যোগ করুন।