Vehicle Detection and Classification using OpenCV and CNNs

by Ranjeet Reddy Manchana
← Projects Model: loading… View Project Code
Live demo · runs in your browser

Count and classify vehicles

Upload a traffic video and the model (YOLOv5 via ONNX Runtime Web, with OpenCV.js) detects, tracks and counts vehicles crossing the line. Nothing is uploaded to a server: your video is processed locally.

Jump to the demo ↓ GitHub repo

What it doesCount and classify traffic, entirely in the browser

EagleEye (the repo is called vehicle-counter) takes a traffic video, finds the vehicles in every frame, follows each one from frame to frame, sorts it into light, medium or heavy, and counts it once as it crosses a line. There is no server and no upload: the video plays locally, each frame is drawn to a canvas, and the neural network runs in your browser through WebAssembly.

Frame→Letterbox 640×640→YOLOv5s→Filter + NMS→Track→Classify→Count

How it worksFive stages per frame

  1. Detect. OpenCV.js converts each frame to RGB and letterboxes it to 640×640 (aspect ratio kept, grey padding). Pixels are scaled to 0–1 and passed to a YOLOv5s model (ONNX, about 28 MB) running on ONNX Runtime Web's WebAssembly backend.
  2. Filter. The model returns 25,200 candidate boxes. I keep those scoring at least 0.35 (objectness × class score) in the five COCO vehicle classes: bicycle, car, motorcycle, bus and truck. Non-maximum suppression at IoU 0.45 removes duplicates, and the boxes are mapped back to the original frame.
  3. Track. A small custom tracker matches each detection to an existing track by box overlap (IoU above 0.1), falling back to centroid distance (under 80 px). A track that goes unmatched survives 10 more frames, which covers brief occlusions.
  4. Classify. The class and the box's size relative to the frame decide the bucket (table below).
  5. Count. A dashed line sits 60% of the way down the frame. When a tracked vehicle's centre moves from one side of the line to the other, it is counted, in either direction, and each track is counted only once. Seeking backwards resets the counters.

ClassificationType first, then size

Weight class is a heuristic, not a measurement: it combines what the model says the object is with how much of the frame its box covers.

Detected asRuleBucket
BusAlwaysHeavy
TruckBox covers more than 4% of the frame, otherwise…Heavy / Medium
CarBox covers more than 2.5% of the frame, otherwise…Medium / Light
Motorcycle, bicycleAlwaysLight

Engineering notesTrade-offs and limits

Built with YOLOv5 by Ultralytics, ONNX Runtime Web and OpenCV.js. The model file is served from this site, so the page needs to be opened over HTTP, not as a local file.

Live demo

Try it with your own video

Upload Video

Drag & drop a traffic video here
0:000:00
Please upload video input.
Light Medium Heavy

Counts

Total

0

Light

0

Medium

0

Heavy

0