Skip to main content

[Vision AI] Hierarchical Object Detection: YOLOv3 Vehicle ROI, YOLOv5 Plate Detector & CSRT Tracking (#3)

ยท 3 min read
Jaybee
AI & Robotics Engineer

YOLOv5 Plate Detection Localization

๐Ÿ“Œ Key Takeaways (TL;DR)

  • Problem: Searching for tiny plates (30x15 pixels) across the raw 640x480 frame generates massive false positives from roadside signs and drops inference speed below 10 FPS.
  • Solution: Designed a two-stage hierarchical architecture: broad vehicle detection (YOLOv3-tiny) crops the vehicle ROI, within which a fine-tuned YOLOv5 (lp_det.pt, conf=0.6) locates the license plate. Integrated OpenCV CSRT correlation filter tracking with a 15-frame redetection interval.
  • Result: Eliminated 98% of background false positives and slashed GPU overhead by 80%, sustaining real-time 50+ FPS continuous tracking.

1. The Pitfalls of Global Detection & The Hierarchical Solutionโ€‹

Running plate detectors across the full 640x480 image causes two critical flaws:

  1. Background Clutter: Street advertisements, road billboards, and store signs trigger high false-alarm rates.
  2. Resolution Downsampling Loss: Resizing 640x480 to typical model inputs (e.g., 640x640) squashes tiny 30-pixel plates into sub-feature extinction.

Our two-stage hierarchical pipeline addresses this by constraining the search space:

vehicle_boxes = detect_vehicles_yolov3(frame, net=vehicle_net, conf_thresh=0.5)

for box in vehicle_boxes:
x1, y1, x2, y2 = box
vehicle_roi = frame[y1:y2, x1:x2]
plate_boxes = plate_model(vehicle_roi)
for pbox in plate_boxes:
px1, py1, px2, py2 = pbox
global_plate_box = [x1 + px1, y1 + py1, x1 + px2, y1 + py2]

2. Real-Time Latency Mitigation: CSRT Tracker Integrationโ€‹

Continuous Tracking with OpenCV CSRT

if frame_count % CONFIG["detection_interval"] == 0:
boxes = detect_vehicles_yolov3(frame)
tracker = cv2.TrackerCSRT_create()
tracker.init(frame, tuple(boxes[0]))
else:
success, tracked_box = tracker.update(frame)
if not success:
tracker = None

3. Real Runtime Errors & Debugging Logโ€‹

Bug 1: cv2.error: (-215:Assertion failed) on Boundary Vehicle Cropsโ€‹

When a violating vehicle partially exited the left or bottom edge of the frame, YOLOv3 returned bounding boxes with negative coordinates or coordinates exceeding frame dimensions (x1 = -14, y2 = 1092 on a 1080p frame), causing frame[y1:y2, x1:x2] to produce an empty 0xN NumPy slice and crashing YOLOv5 plate detection:

cv2.error: OpenCV(4.9.0) /io/opencv/modules/imgproc/src/color.cpp:196:
error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
  • Fix: Clamped all ROI coordinates to [0, width] and [0, height] and enforced a minimum vehicle patch size of 48 ร— 48 pixels before invoking plate_model(vehicle_roi).
h, w = frame.shape[:2]
x1, y1 = max(0, int(box[0])), max(0, int(box[1]))
x2, y2 = min(w, int(box[2])), min(h, int(box[3]))
if (x2 - x1) < 48 or (y2 - y1) < 48:
continue
vehicle_roi = frame[y1:y2, x1:x2]

Bug 2: AttributeError: module 'cv2' has no attribute 'TrackerCSRT_create'โ€‹

After upgrading opencv-python in our environment alongside ultralytics YOLOv5, cv2.TrackerCSRT_create() threw an AttributeError even though opencv-contrib-python was listed in requirements.txt.

  • Root Cause: ultralytics pulls opencv-python as a transitive dependency, which overwrites the shared cv2 namespace and strips contrib tracking modules (legacy.TrackerCSRT_create).
  • Fix: Uninstalled both packages (pip uninstall -y opencv-python opencv-contrib-python), installed opencv-contrib-python==4.9.0.80 exclusively, and added a fallback factory resolving cv2.TrackerCSRT_create vs cv2.legacy.TrackerCSRT_create.

4. Next in This Seriesโ€‹

In Post #4: Deep Super-Resolution (EDSR vs FSRCNN) & Multi-Frame Image Stacking, we explore deep EDSR_x4 super-resolution to reconstruct crisp alphanumeric strokes from blurry low-resolution plate crops, combined with multi-frame image stacking.