Lecture 5 of 1419 minInteractiveFree lecture

Object detection

Classification says what is in an image; detection says what and where. Learn how detectors predict boxes from a grid, how IoU decides whether a box is correct, how non-maximum suppression removes duplicates, and how detectors are scored.

In this lecture you will

  • Describe the output of an object detector — boxes, classes and confidence scores
  • Compute intersection over union (IoU) and use it to judge a detection
  • Explain non-maximum suppression and why detectors need it
  • Understand how single-stage detectors such as YOLO read an image, and how mAP scores them

Many real applications need more than a label for the whole image. A self-checkout needs to find every item on the counter; a quality system must point to the scratch; a safety camera must count people in a danger zone. Object detection answers two questions at once: what objects are present, and where each one is.

A detector’s output is a list like this:

[
  {"class": "house", "confidence": 0.93, "box": [26, 73, 96, 86]},
  {"class": "car",   "confidence": 0.88, "box": [198, 136, 96, 44]},
  {"class": "tree",  "confidence": 0.81, "box": [147, 44, 58, 114]}
]

Each box is usually written as its top-left corner plus width and height (or as two corners), in pixels.

Is this box right? Intersection over union

A predicted box is never pixel-perfect. To decide whether it counts, we measure how much it overlaps the true (“ground-truth”) box:

IoU=area of overlaparea of union\text{IoU} = \frac{\text{area of overlap}}{\text{area of union}}

IoU is 1 for a perfect match and 0 for boxes that do not touch. A common rule is that IoU ≥ 0.5 counts as a correct detection.

InteractiveIntersection over unionDrag the amber box or its corner
Loading visual…

The shaded area is the overlap. A box that is too big is penalized just like a box that is too small, because the union grows.

How detectors see an image

Early detectors ran a classifier over thousands of windows at different positions and sizes — accurate but slow. Modern single-stage detectors such as the YOLO (“you only look once”) family do everything in one pass: the network divides the image into a grid, and every cell predicts boxes, a confidence score and class probabilities at the same time.

InteractiveReading an image with a grid of predictorsHover or tap a cell
Loading visual…

The cell containing an object’s center is responsible for it — the box itself can extend far beyond the cell. Most cells see only background and predict low confidence.

Lower the threshold to 0.2 and noise appears: background cells with slightly higher confidence start to count. Raise it to 0.9 and even the tree disappears. Choosing the threshold is a business decision about which mistake — a false alarm or a miss — is more expensive.

Removing duplicates: non-maximum suppression

A detector typically fires several times for the same object — neighboring cells and differently shaped anchor boxes all see part of it. Non-maximum suppression (NMS) cleans this up.

AnimationNon-maximum suppression
Loading animation…
  1. Many overlapping proposalsThe raw output contains several boxes per object, each with a confidence score, plus a low-confidence false alarm on the sun.
  2. Drop low scoresFirst, discard every box below the confidence threshold. The spurious 0.32 box goes.
  3. Keep the best, suppress its overlapsTake the highest-scoring box, house 0.93, and keep it. Remove every other box whose IoU with it is above 0.5 — they are duplicates of the same house.
  4. RepeatTake the best remaining box, car 0.88, keep it and suppress its duplicates; then the same for the tree.
  5. One box per objectThree clean detections remain: house, car and tree, each with its best box and score.
Bounding box
The rectangle that encloses an object, given by corner coordinates or by center, width and height.
Confidence score
The detector’s belief that the box contains an object of the predicted class.
Anchor boxes
Predefined box shapes the network adjusts; different anchors specialize in tall, wide or small objects.
NMS threshold
The IoU above which a lower-scoring box is treated as a duplicate and removed.
mAP
Mean average precision: the area under the precision–recall curve, averaged over classes (and often over IoU thresholds from 0.5 to 0.95).

Measuring a detector

Detection is scored with precision and recall at a chosen IoU threshold. Sweeping the confidence threshold traces a precision–recall curve; the area under it is the average precision for a class, and averaging over classes gives mAP. The widely used COCO benchmark averages mAP over IoU thresholds from 0.5 to 0.95, rewarding tightly fitting boxes as well as correct ones.

from ultralytics import YOLO

model = YOLO("yolov8n.pt")                    # a small pretrained detector
results = model("street.jpg", conf=0.5)       # confidence threshold
for box in results[0].boxes:
    print(model.names[int(box.cls)], float(box.conf), box.xyxy.tolist())

You now know how computers turn pixels into numbers, detect patterns with convolutions, learn filters in CNNs, train robust classifiers and find objects. The rest of the course goes further — segmenting every pixel, reading poses and faces, understanding video, vision transformers and the diffusion models that generate images.

Key takeaways

  • A detector outputs a list of boxes, each with a class and a confidence score.
  • IoU — overlap divided by union — measures how well a predicted box matches the true one; 0.5 is a common threshold for a correct detection.
  • Detectors propose many overlapping boxes; non-maximum suppression keeps the best box per object.
  • Detection quality is summarized by mean average precision (mAP) across classes and IoU thresholds.

Check your understanding

4 questions · answers explained

Q1A predicted box and a ground-truth box overlap in 30 square pixels; the union of the two covers 120. What is their IoU?

IoU = intersection ÷ union = 30 ÷ 120 = 0.25. At the common 0.5 threshold this prediction would not count as a correct detection.

Q2Why do object detectors need non-maximum suppression?

Neighboring grid cells and anchors often fire for the same object. NMS keeps the highest-scoring box and removes boxes that overlap it heavily.

Q3In a YOLO-style detector, which grid cell is responsible for predicting an object?

The cell that contains the object’s center predicts its box (the box itself can extend well beyond the cell) along with a confidence and class probabilities.

Q4What happens when you raise a detector’s confidence threshold?

A higher threshold keeps only confident detections. Precision usually rises and recall falls; the right setting depends on whether a miss or a false alarm costs more.

You’ve finished the free lectures.

Continue with the remaining 9 lectures of Computer Vision — one-time purchase of $79₹5,499.