Resources from the book

Bounding Box Visualizer

Section 21.2 · Understand the sizes and distribution of your dataset’s bounding boxes.

Section 21.2.1

Purpose

Visualize the bounding-box sizes in your training dataset before calculating custom anchors. The scatterplot makes it easier to identify possible groups of box sizes.

Section 21.2.2

Why It Exists

It can be difficult to judge how many anchors may be appropriate without first seeing the distribution of bounding-box sizes. This utility provides that visual starting point; it does not calculate anchors or select an anchor count for you.

Section 21.2.3

Input

Use an exported YOLO / Darknet training dataset with clip folders containing images and labels subfolders. All images must have the same resolution.

Annotations contain five whitespace-separated numbers per object:

class_id  center_x  center_y  width  height
0         0.50      0.50      0.25   0.20

The script searches recursively through folders named labels, combines their boxes, and obtains the image resolution from the first readable PNG, JPG, or JPEG image. It ignores empty label files and skips lines without exactly five values.

Check your dataset first.

The code assumes matching image dimensions; it does not verify every image. A five-value annotation line must contain numeric values. Use the lowercase labels folder name and .txt extension.

Review 21.1 · Data Structure & Python Setup →

Section 21.2.4

Output

One figure contains two side-by-side plots: bounding-box width versus height, and a 20-bin histogram of bounding-box areas. Each point represents a labeled box; the histogram counts boxes by area.

Configuration anchors are based solely on bounding-box size, regardless of class name. Visualizing box sizes is a critical step in building custom configurations.

Pixel dimensions

The default view plots width and height in pixels, and area in square pixels. Read the axis values before judging how different the box sizes are.

Book example: bounding box width-versus-height scatterplot and area histogram in pixels.
Example output from section 21.2 of the book.

Relative dimensions

With --scaled, width and height are fractions of the image dimensions, and area is a fraction of the full image area. The scatterplot axes span 0 to 1. The underlying box-size relationships remain unchanged.

Book example: normalized bounding box dimensions and relative-area histogram.
Example output from section 21.2 of the book.

Review both views in sequence for two perspectives on the same data. Close the first plot window before running the second command.

The guide behind the codeFrom the page
to the plant floor.
Practical Guide to AI Computer Vision Integration in Industrial ManufacturingExplore the Book