How to count objects in Tensorflow Object Detection API
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Counting detected objects with TensorFlow Object Detection API is usually a post-processing task, not a separate model feature. The model returns bounding boxes, class IDs, and confidence scores. Your job is to apply thresholds, optionally filter by class, and aggregate counts in a way that matches your product requirements. This sounds simple, but counting errors often come from poor threshold choices, duplicate detections, or mixing label maps incorrectly.
This article walks through a practical counting pipeline for images or video frames, with code patterns you can adapt for production inference services.
Core Sections
1) Understand inference outputs first
A typical detection result contains tensors like:
detection_boxesdetection_classesdetection_scoresnum_detections
Only detections above a confidence threshold should be counted. Thresholds vary by model and domain, so calibrate with validation data.
2) Basic counting by class with threshold
This gives deterministic counts per frame. If you need label names, map class IDs through your label_map.pbtxt metadata.
3) Reduce duplicates with NMS-aware configuration
Most models already perform non-max suppression (NMS), but duplicates can still appear with low score thresholds or crowded scenes. Increase threshold modestly and verify whether model config uses appropriate NMS settings.
For strict counting use cases (inventory, traffic), evaluate precision and recall tradeoffs by class instead of using one global threshold.
4) Video counting requires tracking logic
Frame-by-frame counts can overcount the same object across consecutive frames. For "how many are visible now," per-frame count is fine. For "how many passed this line," add a tracker and count unique IDs crossing a region.
Without tracking, cumulative totals are usually inflated.
5) Build evaluation and monitoring around counting
Treat counting as a measurable feature. Keep a labeled evaluation set with expected counts and run regression checks when model versions change. In production, log per-class count distributions and alert on sudden shifts, which often indicate camera drift, lighting changes, or bad deployments.
6) Production checklist for TensorFlow object counting
Before shipping this approach in a real project, validate it in a controlled workflow that mirrors production traffic, data shape, and failure modes. Start with one measurable success metric such as latency, error rate, or precision, then define acceptable limits. Run the implementation with representative inputs, not toy samples, and collect logs that explain both successes and failures. If behavior depends on external services or user input, include at least one negative test path so you can confirm how the system reacts when assumptions are violated.
Next, create an operational checklist for rollout. Document required configuration values, version constraints, and environment variables in one place. Add a lightweight smoke test that can run in CI and after deployment. Decide who owns alerts and what threshold should trigger investigation. For high-impact systems, define a rollback switch or feature flag so you can disable the new behavior without a full release cycle.
Finally, capture maintenance notes that future contributors will need: edge cases, known limitations, and links to test fixtures. This short documentation step reduces regressions during refactors and keeps the implementation understandable after the original author rotates to another project.
Common Pitfalls
- Counting detections below an uncalibrated threshold, producing unstable false positives.
- Forgetting to cast class IDs consistently, causing label map mismatches and wrong class totals.
- Summing frame counts over time without tracking, which double-counts persistent objects.
- Using one threshold for all classes even when classes have very different confidence behavior.
- Skipping evaluation after model updates and discovering count regressions only in production.
Summary
Object counting with TensorFlow Object Detection API is a reliable post-processing step when you apply confidence filtering, class mapping, and task-specific counting rules. Start with per-frame counts, then add tracking for unique-event totals in video pipelines. Most accuracy gains come from threshold calibration and evaluation discipline, not from changing a few lines of aggregation code.

