Sub topic 5
Drawing and Storing Bounding Boxes
Prerequisites and the working model
Assume you can read JSON, write basic Python, and distinguish an image's width from its height. A bounding box is a rectangle that marks where one object appears in an image. Object detection models usually learn two things from each box:
- The object's class, such as car or pedestrian.
- The object's location, represented by box coordinates.
The image coordinate origin is normally at the top-left corner. The x coordinate increases to the right, and the y coordinate increases downward.
Drawing a consistent box
Draw the smallest rectangle that contains the visible object. Do not include large areas of nearby background. Do include visible parts that belong to the object, such as a car's mirrors or a person's backpack, if your labelling guideline treats them as part of the object.
A box can be stored in several equivalent forms:
| Representation | Values | Example |
|---|---|---|
| Corner coordinates | x_min, y_min, x_max, y_max | 50, 40, 150, 90 |
| Top-left plus size | x_min, y_min, width, height | 50, 40, 100, 50 |
| Centre plus size | x_center, y_center, width, height | 100, 65, 100, 50 |
The top-left plus size form is common in COCO-style annotations. The values are usually pixels. For a box with x = 50 and width = 100, the right edge is x = 150. Its height works the same way.
Keep one convention throughout a project. A frequent error is treating x_max as a width. Another is swapping width and height. Store image width and height with the image metadata so that later conversions can be checked.
Storing a box with its class
A COCO-style annotation connects a box to an image and a category through integer identifiers. The area is the box area in square pixels.
{
"images": [
{
"id": 1,
"file_name": "street_001.jpg",
"width": 640,
"height": 480
}
],
"categories": [
{
"id": 1,
"name": "car"
}
],
"annotations": [
{
"id": 1,
"image_id": 1,
"category_id": 1,
"bbox": [50, 40, 100, 50],
"area": 5000,
"iscrowd": 0
}
]
}The box begins at pixel coordinate 50, 40 and extends 100 pixels horizontally and 50 pixels vertically. Its centre is at 100, 65.
The iscrowd field is used by COCO evaluation for grouped or crowd regions. Follow the format required by your training and evaluation tools. Do not invent fields that your loader ignores, and do not silently discard fields that affect evaluation.
Validating boxes
Every box should have a positive width and height and should fit within the image unless your format explicitly permits clipped coordinates.
def check_box(bbox, img_w, img_h):
x, y, w, h = bbox
problems = []
if w <= 0 or h <= 0:
problems.append("non-positive size")
if x < 0 or y < 0 or x + w > img_w or y + h > img_h:
problems.append("box extends outside image")
return problems
print(check_box([600, 40, 100, 50], 640, 480))This check reports a box whose right edge is beyond the 640-pixel image boundary. Validation should run before training, because a single coordinate conversion error can produce invalid labels or teach the model the wrong object location.
Exercise
An image is 800 pixels wide and 600 pixels high. A box is stored as [120, 90, 200, 150]. What are its bottom-right corner and centre coordinates?
Show answer
The bottom-right corner is (320, 240). The centre is (220, 165).