How to count objects in Tensorflow Object Detection API
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Counting objects with the TensorFlow Object Detection API is usually a post-processing task, not a separate model feature. After the detector returns boxes, classes, and scores, you count the detections that pass your score threshold and optionally filter by class.
What The Detector Actually Returns
A detection model typically produces arrays such as:
- detection boxes
- detection scores
- detection classes
- number of detections
The raw num_detections value is not always the count you want, because many of those candidate detections may have low confidence scores.
That is why counting usually means:
- run inference
- filter detections by confidence threshold
- optionally keep only the classes you care about
- count what remains
A Simple Counting Example
Here is a minimal NumPy-style example using the typical output structure.
This is the core logic behind most object-counting applications.
Applying It To TensorFlow Object Detection API Outputs
A common inference result dictionary looks conceptually like this:
You can count detections above a threshold like this:
That is usually enough for image-by-image counting.
Counting By Class
If you want a count per class rather than one total, group the filtered classes.
This is useful for dashboards or inventory-style applications where you need counts of each detected category.
Why Threshold Selection Matters
The threshold is not just a cosmetic parameter. If it is too low, you overcount false positives. If it is too high, you undercount real objects.
A good threshold depends on:
- model quality
- object size and clutter
- whether false positives or false negatives are more costly
There is no universal correct score threshold such as 0.5. It is a tuning decision tied to your deployment goals.
Beware Of Duplicate Boxes
Even after non-max suppression, detectors can still produce results that are not ideal for counting if the scene is crowded or the model is poorly calibrated.
So object counting quality depends on more than the counting code. It depends on the detector and the post-processing threshold.
If counts matter operationally, evaluate them directly on representative images instead of assuming that "good boxes" automatically means "good counts."
A TensorFlow-Friendly Filtering Pattern
If your outputs stay as tensors, you can do the same filtering with TensorFlow ops.
This is useful if you want to keep more of the logic inside TensorFlow instead of dropping into NumPy immediately.
Common Pitfalls
The most common mistake is using num_detections directly as the final count. That usually ignores score filtering.
Another mistake is counting all classes when the task really cares about one category such as people, cars, or bottles.
Developers also forget to cast class IDs to integers before comparing them, especially when model outputs come back as float arrays.
Finally, if the model produces double detections for the same object, the problem is not fixed by a different counting loop. It usually needs threshold tuning or model improvement.
Summary
- Counting objects is usually done after inference by filtering detections.
- Use confidence thresholds before treating detections as real counted objects.
- Filter by class if you only care about certain object categories.
- Count per class with a grouping step when needed.
- The quality of the count depends on both the detector and the post-processing threshold.
Related reading
- How to count total number of trainable parameters in a tensorflow model?
- How to create a combined tf.keras model with conditional evaluation of sub-models
- How to create a keras layer with a custom gradient in TF2.0?
- How to create a keras layer with a custom gradient in TF2.0?
- How to count the number of spots in this image?
- How to create a Rotation Matrix in Tensorflow
- How to create a neural network for regression?
- How to create a new gym environment in OpenAI?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.