পাঠ ৩৩ · ৩৫-এর মধ্যে · মডিউল ৪
Home / AI Courses / Computer Vision / Project: realtime detection

প্রজেক্ট: real-time object detection

Project: real-time object detection — webcam YOLO end-to-end
১৫ মিনিট পড়া মাঝারি · Intermediate Project

এই project-এ যা করবেন

  • Bangladesh traffic dataset assemble (rickshaw, CNG, bus)
  • YOLOv8 finetune
  • Webcam realtime inference
  • Mobile deploy (ONNX export)

১ · Project scope

Goal: Bangladesh traffic-specific object detector — 5 class:

  • Rickshaw
  • CNG (auto)
  • Bus
  • Motorcycle
  • Pedestrian
Project pipeline

Real CV project — academic example beyond. Data collect, label, train, optimize, deploy, monitor — full lifecycle. Bangladesh-specific = local value.

২ · Step 1: Data collection

  • Source: dashcam video, traffic CCTV, smartphone, public footage.
  • Diversity: day/night, weather, location.
  • Quantity: 1000-5000 image initially.
  • Privacy: face/plate blur if public release.

Frame extraction:

import cv2
cap = cv2.VideoCapture('traffic_video.mp4')
i = 0
while True:
    ret, frame = cap.read()
    if not ret: break
    if i % 30 == 0:  # every 30 frame (1 sec at 30fps)
        cv2.imwrite(f'frames/{i:06d}.jpg', frame)
    i += 1
cap.release()

৩ · Step 2: Annotation

  • Tool: Roboflow, CVAT, LabelImg.
  • Format: YOLO (txt: class x_center y_center w h normalized).
  • SAM-assist: click → mask → bbox auto extract.
  • Speed: 5-10 sec per image with SAM.

Dataset structure:

traffic_dataset/
├── images/
│   ├── train/   (80%)
│   ├── val/     (10%)
│   └── test/    (10%)
└── labels/
    ├── train/
    ├── val/
    └── test/

# data.yaml
path: ./traffic_dataset
train: images/train
val: images/val
names:
  0: rickshaw
  1: cng
  2: bus
  3: motorcycle
  4: pedestrian

৪ · Step 3: Train YOLOv8

Python · Ultralytics
from ultralytics import YOLO

# Start from pretrained
model = YOLO('yolov8n.pt')  # nano — fastest

# Train
results = model.train(
    data='traffic_dataset/data.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    device=0,                       # GPU 0
    project='runs/train',
    name='bd_traffic',
    patience=20,                    # early stop
    cos_lr=True,                    # cosine LR schedule
    augment=True,                   # default augmentation
    mixup=0.1,                      # MixUp
    mosaic=1.0,                     # Mosaic 4-image
)

# Validate
metrics = model.val()
print(f"mAP@0.5: {metrics.box.map50:.3f}")
print(f"mAP@0.5:0.95: {metrics.box.map:.3f}")

    

Training time estimate:

  • RTX 3060 (12 GB): 1000 image, 100 epoch — 2-3 hour.
  • Cloud T4 (Colab free): similar.
  • RTX 4090: 30 minute.

৫ · Step 4: Realtime webcam inference

Python · OpenCV + YOLO
import cv2
from ultralytics import YOLO

# Load trained model
model = YOLO('runs/train/bd_traffic/weights/best.pt')

# Webcam (0 = default camera)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

while True:
    ret, frame = cap.read()
    if not ret: break

    # Inference
    results = model(frame, verbose=False, conf=0.5)

    # Visualize
    annotated = results[0].plot()

    # FPS
    fps = 1 / (results[0].speed['inference'] / 1000)
    cv2.putText(annotated, f'FPS: {fps:.1f}', (10, 30),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

    cv2.imshow('BD Traffic Detection', annotated)
    if cv2.waitKey(1) & 0xFF == ord('q'): break

cap.release()
cv2.destroyAllWindows()

    

৬ · Step 5: Optimize for deployment

(ক) ONNX export

model.export(format='onnx', imgsz=640, optimize=True)
# Creates yolov8n.onnx — framework-independent

(খ) Quantization (FP16)

model.export(format='onnx', half=True)
# 50% smaller file, 2x faster on supported hardware

(গ) TensorRT (NVIDIA)

model.export(format='engine', half=True)
# Maximum speed on NVIDIA GPU

(ঘ) Mobile (TFLite)

model.export(format='tflite', int8=True)
# Android/iOS deployment

৭ · Step 6: Bangladesh-specific tuning

  • Bengali label: "rickshaw" → "রিকশা" — UI display.
  • Local context: Pohela Boishakh decoration vehicle, Eid eve traffic.
  • Lighting: Dhaka monsoon, summer heat haze augmentation.
  • Camera: diverse smartphone — augment quality.

৮ · Step 7: Counting + tracking

Detection alone insufficient for "how many cars passed?"। Need tracking.

# Built-in tracking with BotSORT/ByteTrack
results = model.track(
    source='video.mp4',
    tracker='botsort.yaml',
    persist=True
)
# Each box has unique ID across frames

# Count by class crossing line
line_y = 400
counts = {}
for r in results:
    for box, cls, id in zip(r.boxes.xyxy, r.boxes.cls, r.boxes.id):
        cy = (box[1] + box[3]) / 2
        if cy > line_y and id not in counts:
            counts[id.item()] = cls.item()
Real CV project = "data plumbing > model architecture". 80% effort: data collect, label, augment, evaluate. 20%: model train. Production = 99% engineering, 1% magic.
Real-time detection — full project pipeline 📹 Collect video frames 🏷 Annotate SAM-assist 🎯 Train YOLOv8 📊 Validate mAP, precision ⚡ Optimize ONNX, INT8 🚀 Deploy edge/cloud Iterative — each step feedback to previous Production model continuously evolve Monitor → identify failure case → augment dataset → retrain
Real CV project pipeline — collect, annotate, train, validate, optimize, deploy. Iterative.

৯ · Common pitfall

  • Class imbalance: rickshaw 1000 image, bus 100 — bus accuracy poor. Resample বা weight loss.
  • Domain shift: day train, night fail. Augment heavy.
  • Annotation noise: bbox loose — model learn loose. Strict QC.
  • Overfitting: small dataset — heavy regularization, transfer learning critical.
  • Realtime ambition: too-large model — drop FPS. Right-size choose.

১০ · Bangladesh-specific deployment

  • Smart traffic: Dhaka traffic management.
  • Vehicle counting: bridge, intersection.
  • Parking automation: shopping mall.
  • Safety: helmet detection (motorcyclist).
  • Logistics: warehouse package detect.

১১ · Hardware options

HardwareFPS (YOLOv8n)CostUse
RTX 4090500+$1500Server
RTX 3060200$300Workstation
Jetson Nano15$100Edge
Raspberry Pi 55$80IoT
iPhone 15 Pro30flagshipMobile

১২ · Monitor in production

  • Confidence drift: avg confidence drop → model degrade.
  • Class distribution shift: new vehicle type emerging.
  • Failure case capture: low-confidence box log — manual review.
  • Retraining cycle: monthly/quarterly with new data.
Real project — model accuracy 50% effort. Data, deployment, monitoring — others 50%. Beginner-এর mistake: too much focus on architecture. Lesson: pipeline > model.

ভাবনার প্রশ্ন

প্র ০১ Project-এ "data quality" "data quantity"-এর চেয়ে বেশি matter. কেন? Examples?

Andrew Ng popularized "data-centric AI" — quality over quantity.

Quality issues:

  • Loose bbox — model learn loose.
  • Inconsistent label — class boundary blurry.
  • Mislabeled — direct error.
  • Missing annotation — false negative learn.

Quantity vs quality experiment:

  • 10K noisy image: 65% mAP.
  • 2K clean image: 75% mAP.
  • Better quality > 5x quantity.

Quality strategies:

  • Annotation guideline document.
  • Inter-annotator agreement check.
  • Random spot QC.
  • Active learning — focus on uncertain samples.

Bangladesh implication:

  • Local annotator training.
  • Bilingual guideline (Bangla + English).
  • Cultural context understand.

মূল উপলব্ধি: "Garbage in, garbage out" — eternal. Investment in clean data — highest ROI.

প্র ০২ YOLOv8n vs YOLOv8x — accuracy vs speed. Bangladesh CCTV traffic-এ কোনটা?

Realtime production decision.

YOLOv8n (nano):

  • 3.2M params, 37% mAP.
  • 280 FPS T4.
  • Mobile deployable.

YOLOv8x (extra large):

  • 68M params, 53.9% mAP.
  • 40 FPS T4.
  • Server-only.

CCTV consideration:

  • 1080p 30 FPS stream.
  • Multiple camera (10-100).
  • Server scaling matter.
  • Budget — RTX 3060 server bracket.

Decision matrix:

  • Single camera + accuracy priority: YOLOv8m/l.
  • 10+ camera + realtime: YOLOv8n/s.
  • Mobile patrol: YOLOv8n.
  • Forensic offline: YOLOv8x.

Cascade approach:

  • YOLOv8n always — fast first pass.
  • Low confidence → YOLOv8x verify.
  • Hybrid speed + accuracy.

Bangladesh real:

  • Dhaka City Corp pilot — YOLOv8s on 4 GPU server.
  • 4 camera per GPU.
  • Cost: $5K hardware + $1K/year maintenance.

মূল উপলব্ধি: "Best model" task-dependent. Speed/accuracy trade-off explicit reason.

প্র ০৩ Annotation cost reduce — SAM, active learning, synthetic data. Bangladesh-এ কীভাবে combine?

Bangladesh annotation industry opportunity.

SAM-assisted:

  • Click → mask → bbox extract.
  • Annotation 5-10x faster.
  • Quality often higher than manual.

Active learning:

  1. Initial 200 manual annotation.
  2. Train baseline.
  3. Predict on 5000 unlabeled.
  4. Identify low-confidence — annotator priority.
  5. Retrain — accuracy bump.
  6. Iterate.

Synthetic data:

  • Dhaka 3D city model render — annotated automatically.
  • Stable Diffusion + bbox — generative augment.
  • Domain gap — careful sim2real.

Combined strategy:

  • Initial: synthetic 5K.
  • Train baseline.
  • SAM-assist label real 1K.
  • Active learning identify hard real cases.
  • Annotator focus on hard, quality high.
  • Final dataset: 10K mixed.

Cost:

  • Pure manual: $1000.
  • SAM-assisted: $200.
  • Active learning: $100.
  • Combined: $50.

Bangladesh business:

  • Annotation export competitive.
  • Local rate + AI-tool — global service.
  • Skill: SAM operation, Python QC.

মূল উপলব্ধি: Modern annotation = AI + human collaboration. Bangladesh — labor cost advantage + AI tool literacy = lucrative niche.

প্র ০৪ Production deployment-এ MLOps — what minimum infrastructure for traffic detection in Dhaka?

MLOps Bangladesh context — affordable starter.

Minimum components:

  • Compute: 1 GPU server (RTX 3060).
  • Camera: 5-10 IP cameras (Hikvision).
  • Network: dedicated bandwidth.
  • Storage: 1TB for video archive.

Software stack:

  • Inference server: Triton or FastAPI.
  • Stream processing: OpenCV + Kafka (event).
  • Database: PostgreSQL (detection log).
  • Monitor: Prometheus + Grafana.
  • Dashboard: Streamlit বা React.

Pipeline:

  1. Camera → RTSP stream.
  2. Worker reads frame.
  3. YOLO inference.
  4. Post-process: count, alert.
  5. DB log.
  6. Dashboard visualize.

Deployment:

  • Docker Compose initial.
  • Kubernetes scale.
  • Cloud (GCP, AWS) BD startup-এ DigitalOcean.

Monitoring:

  • Inference latency p50/p99.
  • FPS per camera.
  • Detection confidence distribution drift.
  • Alert: latency > threshold, FPS drop.

Retraining:

  • Failure case capture (low-conf, error).
  • Weekly review.
  • Monthly retrain.
  • A/B test new model.

Cost estimate (Bangladesh, 10 camera):

  • Hardware setup: $3000-5000.
  • Camera: $1500-3000.
  • Annual: $500 (electricity, maintenance).
  • Engineer: 1 part-time.

Local consideration:

  • Power outage — UPS, generator.
  • Heat — cooling.
  • Theft — physical security.
  • Internet — fallback.

মূল উপলব্ধি: Production-grade CV — under $10K achievable Bangladesh. Government, private — ROI quick (vs manual operator cost).

অনুশীলন

  1. Mini dataset: 50 image of household items, label, train YOLOv8n.

    Code section ৪ template. 30 minute Colab GPU.

  2. Webcam test: Pretrained YOLOv8 দিয়ে webcam realtime detect.

    Code section ৫। 80 COCO class — कुकुर, person, etc detect.

  3. ভাবুন: Bangladesh garment factory defect — YOLOv8 কীভাবে adapt?

    (1) Defect type 5-10. (2) Controlled lighting fixture. (3) Annotation standardize. (4) YOLOv8s (accuracy). (5) Quality control workflow integrate.

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? Google Colab use করুন।
পূর্ববর্তী পাঠ
পাঠ ৩২ · Pose estimation