How to Compute The Area of a Set of Bounding Boxes in PyTorch?
In this article, we are going to see how to compute the area of a set of bounding boxes in PyTorch. We can compute the area of a set of bounding boxes by using the box_area() method of torchvision.io module.
This method accepts bounding boxes as an input and returns the area of the given bounding boxes. The input bounding boxes must be torch Tensors with [N,4] size, where N represents the number of bounding boxes for which the area will be computed. The bounding boxes are expected to be in the format (x_min, y_min, x_max, y_max), where 0 ≤ x_min < x_max, and 0 ≤ y_min < y_max. Before computing the area of a bounding box we use unsqueeze to make this bounding box tensor into a 2D tensor.
- boxes: This method accepts bounding boxes as input.
Return: This method return area for each box.
Step 1: Import the required libraries.
Step 2: Read the input image from your computer.
Step 3: define a bounding box and convert this box into a torch tensor.
Step 4: unsqueeze the given bounding box to make it a 2D tensor. Execute this step only if we want to compute the area of a single bounding box else skip this step.
Step 5: Compute the above defined bounding box area and store this computed area in a variable for further use.
Step 6: set this computed area on the label.
Step 7: Draw a bounding box on the image and put the above-defined label on box.
Step 8: transform this image into a PIL image
Step 9: Display the output image.
The below image is used for demonstration:
in this example, we are computing the area of a single bounding box and set this computed area as a label.
in this example, we are computing the area of multiple bounding boxes and set this computed area as a label for each box.
Please Login to comment...