AI Hints
Home›Computer Vision Tutorial›Object Detection
Computer Vision · Lesson 17 of 111

Object Detection

What, where, and how many.

⏱ 2 min read▶ Video at 28:37🎓 Free
Watch this lesson in the Computer Vision courseWatch on YouTube ↗Full Computer Vision course

A classifier looks at a whole photo, and gives one answer. But a sorting line has twenty apples in view, moving at speed. We need to find every single one, and draw a box around it. That's detection, and it's the workhorse of real world vision.

Object Detection
From the video: Object Detection
  • Twenty apples at once
  • Find every single one
  • The workhorse of real vision

How a modern detector works

Early detectors were slow, and they looked at thousands of regions, one by one. Then, in twenty fifteen, Joseph Redmon asked, what if you only look once? YOLO splits the image into a grid. Every cell predicts boxes and classes, all in one pass through the network. That made real time detection possible, on video. Today, nearly all fast detectors use this same idea.

Measuring overlap

How do you score a box? Compare it to the true box. Intersection over union divides their overlap by their combined area. Watch as the prediction slides over the truth. The score climbs from zero up to almost one. Over point five is the usual bar for a correct detection.

IoU in code

It's only a few lines. Find the overlapping rectangle, and divide. We try three predictions. Run it. A close box scores high, an offset one scores modestly, and a box in the wrong place scores zero. This one number is used in training, in filtering, and in evaluation.

iou.pypython
def iou(a, b):
    x1, y1 = max(a[0], b[0]), max(a[1], b[1])
    x2, y2 = min(a[2], b[2]), min(a[3], b[3])
    both = max(0, x2 - x1) * max(0, y2 - y1)
    size = lambda r: (r[2] - r[0]) * (r[3] - r[1])
    return both / (size(a) + size(b) - both)

truth = (100, 100, 200, 200)
for name, box in [("Excellent", (105, 102, 205, 203)), ("Fair", (130, 130, 230, 230)),
                  ("Missed", (300, 300, 400, 400))]:
    print(f"{name:<10} IoU {iou(truth, box):.2f}")
Output
Excellent  IoU 0.86
Fair       IoU 0.32
Missed     IoU 0.00

Summary

Detectors look once, and I o U measures the result. But detectors produce many boxes for each object. How do we keep just one?

Key takeaways
  • YOLO predicts in one pass
  • Grid cells predict boxes
  • IoU measures overlap
Next up: Cleaning Up and Scoring — NMS and mean average precision