Computer Vision

Object Detection Precision, Recall and IoU: A Python Example

Work through true positives, extra detections and missed objects with a small Python example and an explicit bounding-box matching policy.

Qvantus AI · EditorialPublished 4 min read
Close-up photograph of a camera lens with reflected light
Photo by Alexander Andrews / Unsplash.

Quick answer

Object detection precision is TP / (TP + FP), and recall is TP / (TP + FN), after predictions are matched to labels under a defined class and overlap policy. IoU measures box overlap. A score at one IoU threshold is not mAP, tracking accuracy or evidence of production performance.

What you will take away

  • Run a local bounding-box evaluation without a detector or camera.
  • See why a duplicate detection can count as a false positive.
  • Report the matching threshold and operating conditions with your metrics.
On this page

1. Define a matching policy before counting errors

A predicted box is not correct merely because it overlaps something. The class must match, the overlap must meet your stated threshold and a labeled object must not already have been assigned to a higher-priority prediction under the chosen policy. Record these decisions before comparing runs.

The teaching example uses normalized x1, y1, x2, y2 coordinates between zero and one. It checks positive box dimensions and valid class names, sorts predictions by confidence and greedily matches each prediction to an unmatched same-class label when the IoU threshold is met. This is one explicit policy, not every evaluation protocol.

2. Calculate intersection over union

IoU is intersection area divided by union area. For boxes [0, 0, 0.5, 0.5] and [0.25, 0.25, 0.75, 0.75], the intersection is 0.25 × 0.25 = 0.0625. Each box has area 0.25, so their union is 0.4375 and IoU is about 0.143. At an IoU requirement of 0.5, these boxes would not match.

IoU is a geometric comparison, not a model confidence. A confidence threshold decides which predictions you retain; an IoU threshold decides whether retained boxes match the labels. Changing either can change the reported errors, so keep both visible when interpreting a result.

3. Run the synthetic bounding-box example

Download and unzip the Vision evaluation starter. Run the commands from its directory using Python 3.10 or later. The code uses the standard library and reads detections.json. It does not open an image, run model inference or access a camera.

The supplied sample reports two true positives, one false positive and one false negative. Precision and recall are each 2/3, approximately 0.667, under the included labels, predictions and matching policy. Inspect the duplicate and missed examples, then change a box or the threshold and explain which count changes.

cd vision-evaluation-starter
python3 evaluate_vision.py --iou-threshold 0.5 --output report.json
python3 -m unittest discover -s . -p 'test_*.py'

4. Interpret precision and recall together

With two correct detections and one extra detection, precision is 2 / (2 + 1). With two found objects and one missed label, recall is 2 / (2 + 1). A second prediction for an already-matched object can be an extra detection rather than another true positive. This matters in counting workflows.

Undefined denominators also need a policy. The example returns a null precision when there are no predictions and a null recall when there are no labeled objects. Other tools may use a documented zero-division convention. Do not silently compare reports that handle empty cases differently.

MetricCalculation in this synthetic sample
True positives2 matched objects
False positives1 unmatched or duplicate prediction
False negatives1 unmatched labeled object
Precision2 / 3 ≈ 0.667
Recall2 / 3 ≈ 0.667

5. Move from a calculation to a site evaluation

Average precision summarizes a precision-recall curve, and mAP averages according to the chosen benchmark protocol, often over classes and sometimes multiple IoU thresholds. The starter does neither. Report it as a small single-threshold box-matching exercise and use an appropriate evaluator for a detector release.

For an operational trial, collect representative labeled images from the intended cameras. Keep related frames together when splitting data and review results by class, lighting, object size and occlusion. Validate the exact model, preprocessing and threshold configuration. In Qvantus, explore compatible Vision Agents and the site, camera and event-review workflow before planning a rollout.

Common questions

Is high precision enough for object counting?

No. A system may emit few incorrect detections but miss many objects. Review recall, duplicate handling and the counting policy as well as precision, using labeled examples from the intended conditions.

Is IoU the same as detection confidence?

No. IoU compares the geometry of two boxes. Confidence is a score attached to a prediction. Their thresholds serve different purposes and should be recorded separately.

Do these sample metrics describe a Qvantus detector?

No. They are calculated from synthetic boxes included in a teaching example. They do not measure a trained model, customer footage or factory performance.

Sources & further reading

Published by Qvantus AI. These guides provide practical planning frameworks; measured results depend on the model, data, software and hardware configuration.

All guides

Find your starting point

Explore a workflow around your AI task.

LLM evaluation

A useful answer needs more than fluent language. Define what your assistant should get right, test representative responses and review the failures before changing your prompt, model or training data.

Explore the workflow →

AI model fine-tuning

Start with a specific behavior your baseline model does not handle well. Prepare reviewed examples, select a supported training configuration and measure whether the resulting model improves the task.

Explore the workflow →
Object Detection Precision, Recall & IoU with Python | Qvantus AI