liveBook · Manning

Object detection

As explained in Hugging Face in Action

Object detection is the machine learning task of identifying and locating objects within images or video.

Object detection identifies objects in images or video, classifies them, and locates them with bounding boxes. section 4.2

Hugging Face makes pretrained detection models available through browser-based Hosted Inference API requests, confidence scores, and Python integrations. section 1.2 section 4.2

A detection result commonly provides:

  • Object labels identifying detected categories. section 6.2.1
  • Confidence scores indicating the model's probabilities. section 4.2
  • Bounding-box coordinates locating each object in the image. section 6.2.1

Detecting objects with DETR

Load facebook/detr-resnet-50 with DetrImageProcessor and DetrForObjectDetection, process an image, run inference, then post-process detections with a confidence threshold. Draw each returned box and label using PIL. section 6.2.1

Listing 6.10 Using the facebook/detr-resnet-50 model to detect objects in images
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch

image_processor = DetrImageProcessor.from_pretrained(
    "facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

inputs = image_processor(images = image,  #1
                         return_tensors = "pt")

model.eval()

with torch.no_grad():
    outputs = model(**inputs)  #2
target_sizes = torch.tensor([image.size[::-1]])  #3

results = image_processor.post_process_object_detection(  #4
              outputs,
              target_sizes = target_sizes,
              threshold = 0.9)[0]
print(results)

Use the complete Transformers listing to connect image preprocessing, model inference, target-size conversion, and thresholded object-detection results. section 6.2.1

How Hugging Face in Action covers it

How deep it goes
Explains labels, probabilities, bounding boxes, browser inference, and Python execution without deriving detection internals.
Examples
Demonstrates office-image detection, a dog-and-cat image, confidence scores, and plotted bounding boxes.
What it assumes
Uses Python and the Transformers library while relying on pretrained models rather than requiring model training.
What you can do after
Apply it via the Hosted Inference API or Transformers to return labels, scores, and bounding boxes.
How it connects
Connects computer vision, image classification, image segmentation, Transformers, DETR, and bounding boxes.

Also explained in

Compare how 5 books explain object detection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.