Dealing with the bounding box is pretty common and important in the related tasks of object detection or tracking, such as Yolo and Deepsort. Handling with bbox can greatly reduce the development time and reduce the chance of errors on bounding box.
Installing bbox and the dependencies via pip
pip install git+https://github.com/Pandede/bboxfrombboximportBoundingBox# Create a bounding box in format `xywh`bbox=BoundingBox(x=0, y=0, w=10, h=10)
# ... or in format `xyxy`bbox=BoundingBox.from_xyxy(0, 0, 10, 10)
# ... or in format `tlwh`bbox=BoundingBox.from_tlwh(0, 0, 10, 10)
# Get the width and heightprint(bbox.width, bbox.height) # 10 10# ... or in short formw, h=bbox.w, bbox.h# 10 10# Get the areaprint(bbox.area) # 100# Check if the bounding boxes are identicalbbox_a=BoundingBox(x=0, y=0, w=10, h=10)
bbox_b=BoundingBox.from_xyxy(-5, -5, 5, 5)
print(bbox_a==bbox_a) # TruefrombboximportBoundingBox# Get the coordinate of edge point by calling `anchor(index)`# The returned point refers to the position of number on NumPad:# 7---8---9# | |# 4 5 6# | |# 1---2---3bbox=BoundingBox.from_xyxy(0, 0, 10, 10)
# Get the coordinate of bottom-left pointprint(bbox.anchor(1)) # (0, 10)# Get the coordinate of center pointprint(bbox.anchor(5)) # (5, 5)frombboximportBoundingBox# The `BoundingBox` object stores the attributes `xywh`bbox=BoundingBox(x=5, y=5, w=10, h=10)
print((bbox.x, bbox.y, bbox.w, bbox.h)) # (5, 5, 10, 10)# Format it to `xyxy`print(bbox.to_xyxy()) # (0, 0, 10, 10)# Format it to `tlwh`print(bbox.to_tlwh()) # (0, 0, 10, 10)frombboximportBoundingBoxfrombbox.measureimportunion, intersectbbox_a=BoundingBox.from_xyxy(0, 0, 10, 10)
bbox_b=BoundingBox.from_xyxy(5, 5, 15, 15)
# Get the union area of bounding boxesprint(union(bbox_a, bbox_b)) # 175# Get the intersection area of bounding boxesprint(intersect(bbox_a, bbox_b)) # 25frombboximportBoundingBoxfrombbox.measureimportiou, giou, diou, cioubbox_a=BoundingBox.from_xyxy(0, 0, 10, 10)
bbox_b=BoundingBox.from_xyxy(5, 5, 15, 15)
# Compute the IoU and its variationprint(f'IoU: {iou(bbox_a, bbox_b):.6f}') # IoU: 0.142857print(f'GIoU: {giou(bbox_a, bbox_b):.6f}') # GIoU: -0.079365print(f'DIoU: {diou(bbox_a, bbox_b):.6f}') # DIoU: 0.0153061print(f'CIoU: {ciou(bbox_a, bbox_b):.6f}') # CIoU: 0.0153061