项目作者: tensorturtle

项目描述 :
Easily convert between bounding box annotation formats.
高级语言: Python
项目地址: git://github.com/tensorturtle/rebox.git
创建时间: 2021-07-08T15:57:53Z
项目社区:https://github.com/tensorturtle/rebox

开源协议:MIT License

下载


rebox: Easily convert between bounding box formats

Build
codecov
codacy

Fluidly convert between 2D rectangular bounding box annotation formats.

Instead of writing yet more utility functions to convert between bounding box formats and perform common operations using them, use this instead.

There’s nothing magical about this package, it really is just meant to replace boilerplate code.

Quickstart

Install with pip:

  1. pip install rebox

Convert a bounding box from COCO format to VOC_PASCAL format:

  1. >> from rebox import BBox
  2. >> from rebox.formats import coco, pascal
  3. >> coco_bbox = BBox([40,50,20,15], coco)
  4. >> pascal_bbox = coco_bbox.as_format(pascal)
  5. >> pascal_bbox
  6. "Coordinates: [40 50 59 64], Style: XYXY, Scale: None"
  7. >> pascal_bbox.value
  8. [40 50 59 64]

Supported Bounding Box Formats

Common formats such as YOLO, COCO, PASCAL_VOC, Albumentations, and Label Studio are provided as a convenience. If you wish to make your own coordinates format, instantiate the BBoxFormat class.

Scale Style
YOLO Normalized (0,1) [ x_Center, y_Center, width, height ]
COCO Pixels [ x_min, y_min, width, height ]
PASCAL_VOC Pixels [ x_min, y_min, x_MAX, y_MAX ]
Albumentations Normalized (0,1) [x_min, y_min, x_MAX, y_MAX ]
Label Studio Normalized percentage (0, 100) [x_min, y_min, width, height]

Inspired by varunagrawal/bbox. bbox is not flexible enough to accomodate the various format schemes.

Usage

Creating a BBox and access/modifing its attributes

  1. # readme_examples/create_bbox.py
  2. from rebox import BBox
  3. from rebox.formats import coco
  4. coco_bbox = BBox([40,50,20,15], coco)
  5. print(coco_bbox.format.style) # XmYmWH
  6. print(coco_bbox.format.scale) # None
  7. print(coco_bbox.x1) # 40
  8. print(coco_bbox.y1) # 50
  9. print(coco_bbox.w) # 20
  10. print(coco_bbox.h) # 15
  11. # set values
  12. coco_bbox.x1 = 60
  13. print(coco_bbox.x1) # 60

These attributes are only implemented for the current format,
and are not automatically converted. For example, trying to access x2 for a XmYmWH format returns AttributeError.

Converting between the same absolute/relative formats

The simplest conversion keeps the same scale, but with different styles:

  1. # readme_examples/simple_conversion.py
  2. from rebox import BBox
  3. from rebox.formats import coco, pascal
  4. coco_bbox = BBox([40,50,20,15], coco)
  5. pascal_bbox = coco_bbox.as_format(pascal)
  6. print(pascal_bbox) # "Coordinates: [40 50 59 64], Style: XYXY, Scale: None"
  7. print(pascal_bbox.value) # [40 50 59 64]

Converting across absolute/relative formats

This time, pass in image height and width, to convert across pixel values and relative scale values.

  1. # readme_examples/absolute_relative_conversion.py
  2. from rebox import BBox
  3. from rebox.formats import yolo, coco
  4. image_height = 360 # pixels
  5. image_width = 640 # pixels
  6. yolo_bbox = BBox([0.31, 0.5, 0.2, 0.6], yolo) # using built-in 'yolo_format'
  7. coco_bbox = yolo_bbox.as_format(coco, image_width, image_height) # to convert to built-in format 'coco'
  8. print(coco_bbox.value) # array([134.4, 72. , 128. , 216. ])

Operations on BBoxes

rebox includes several common utility operations on bounding boxes.

IOU (Intersection over Union) of two bounding boxes

  1. # readme_examples/iou.py
  2. from rebox import BBox
  3. from rebox.formats import coco, pascal
  4. from rebox.ops import iou
  5. one_bbox = BBox([40,50,20,10], coco)
  6. two_bbox = BBox([45,60, 30, 20], pascal)
  7. iou = iou(one_bbox, two_bbox)

If you would like to see more operations, please submit an Issue.

Development

I organized often-used commands into scripts.

bump_0.1_patch.sh increments the ‘patch’ version (last place)

pypi_build.sh creates a new distro wheel

pypi_upload.sh uses twine to upload to pypi

testpypi_upload.sh is for uploading to test pypi