The problem
Shops want footfall, car parks want to know how full they are, and offices and clinics want to see when a queue is growing. The usual answer is a person with a clicker, a sensor on the door that counts every pass the same, or a camera feed sent to a cloud service.
A detector that runs next to the camera gives the same numbers without anyone watching and without the video going anywhere: it reads each frame, keeps the count, and throws the picture away.
The data
The model learnt from COCO, a public collection of everyday photos labelled with 80 kinds of object, from people, cars and buses to cups, bottles and chairs. I did not retrain it.
The five sample photos are from Wikimedia Commons, each dedicated to the public domain (CC0) by its author: Crowd in Canning Street, Kolkata by Billjones94; Marygate car park, York by Malcolmxl5; Pavement café, Champagnole by Spielvogel; Queue at the Taj Mahal's West Gate, Agra by Gary Todd; Queue at a telecom shop's entrance, Taipei by 玄史生. I resized them to 1,280 pixels wide.
Your own photos and your camera's frames are read in this page and never uploaded.
The model
SSDLite with a MobileNet v2 backbone, the smallest of COCO-SSD's three versions. It is built for phones: it looks at the picture once, squeezed to 300 × 300 pixels, and scores 1,917 candidate boxes at fixed places and sizes against every class in that single pass. That is what makes it fast enough for live video in a browser.
Half the download. Google's copy stores every weight as a 32-bit number: 18.6 MB. I store them as 16-bit numbers instead (9.3 MB), and the detector finds 45 of the 46 objects the original finds in the sample photos. Eight bits would have made the file 4.8 MB, but then it found only 29 of them, so I kept sixteen.
My own last step. COCO-SSD's helper ties its rule for overlapping boxes to the confidence you ask for, which merges people standing close together as you lower it. So I run the network myself, keep each box's best class above 20%, and remove a box only when a stronger box of the same group covers it by more than 50%. A rider never hides the motorbike under them, and a van the network calls both a car and a truck counts once. The confidence slider then filters what is left instantly, without running the network again.
Groups and zones. People count as people; car, bus, truck, motorcycle and bicycle count as vehicles; everything else counts by its own name. An object is inside a zone when at least half of its box is, so someone half through a doorway counts, and a car's bonnet over the line of a bay does not.
The network runs in a background worker on the graphics chip (WebGL), so the page stays smooth while it works. In Safari, TensorFlow.js uses the graphics chip only from the page itself, so there it runs on the page; its processor-only mode would take seconds a picture. Warming up on the page can freeze it for several seconds, so in Safari the detector waits until you press Load the detector.
The pipeline
- 01Load
When the demo scrolls into view, the detector downloads from this site (9.3 MB) and warms up once. In Safari it waits until you press Load the detector.
- 02Capture
The photo, or the latest camera frame, is scaled to 300 × 300 pixels, the size the network works at, and handed to the worker.
- 03Detect
COCO-SSD scores 1,917 candidate boxes in one pass: about 37 ms a photo on the graphics chip of the computer I measured on.
- 04Clean up
Each box keeps its best class; weak boxes and duplicates within a group go.
- 05Count
The confidence threshold, the group filters and the zone decide what is counted.
- 06Draw
Boxes and labels go on a canvas over the picture. With the camera, the loop repeats up to eight times a second and charts the people count.
Results
Measured with tools/object-counter/bench.mjs on 23 November 2026: it runs the network in a browser and passes the output through the demo's own counting code. Times vary with the device; a phone takes longer, and without WebGL the processor takes several times longer.
For a client
For a shop or a car park I would run the same kind of detector on a small computer beside the camera, or in a browser on a tablet at the entrance, and send out only the numbers: people per minute, spaces taken, queue length. No video needs to be stored.
Counting visitors over a day needs tracking as well as detection, so one person walking past is counted once: I would follow each box from frame to frame and count when it crosses a line you set, such as a shop's threshold.
Each camera gets its own zones, drawn once on its view. Before anyone relies on the numbers, I would check them against a manual count for a few days, and retrain the detector on the client's own footage when their scenes are unusual: products on shelves, a dim basement car park, auto-rickshaws.
Stack
| Part | What it does here | Licence |
|---|---|---|
| COCO-SSD (Lite MobileNet v2) | The detector: the model and its loader, from TensorFlow's model library | Apache 2.0 |
| TensorFlow.js | Runs the network on the graphics chip (WebGL), or the processor if there is no WebGL | Apache 2.0 |
| My counting code | Box clean-up, groups, zones, the per-second camera chart, the 16-bit weights and the benchmark, in TypeScript with unit tests | Mine |
| Sample photos | Five photos from Wikimedia Commons, credited above | CC0 |
| Preact | The interactive demo | MIT |
| Astro | The page, built as static HTML | MIT |
Privacy
Your photos and your camera's frames stay in your browser. The detector downloads from this site, then everything happens on your device: no picture is uploaded, recorded or kept after you leave.
The camera starts only when you press Start, after your browser asks you, and stops when you press Stop, pick a photo or leave the page.
Limits
- The detector sees the picture at 300 × 300 pixels, so small or distant objects are missed: in the busy street it counts the people near the camera, not the crowd down the road.
- People standing close together can merge into one box, so a dense crowd is undercounted.
- It knows only COCO's 80 classes. An auto-rickshaw may come out as a car, a motorcycle or nothing at all.
- Each frame is counted on its own. The camera chart shows how many people are in view each second, not how many different people passed.
- In Safari the detector warms up on the page itself, which can pause the page for several seconds, so there it loads only when you press Load the detector.