Light Weight Toolkit for Bounding Boxes
Project description
PyBboxes
Light weight toolkit for bounding boxes providing conversion between bounding box types and simple computations. Supported bounding box types:
- albumentations : Albumentations Format
- coco : COCO (Common Objects in Context)
- fiftyone : FiftyOne
- voc : Pascal VOC
- yolo : YOLO
Important Notice
Support for Python<3.8 will be dropped starting version 0.2 though the development for Python3.6 and Python3.7 may
continue where it will be developed under version 0.1.x for future versions. This may introduce; however, certain
discrepancies and/or unsupported operations in the 0.1.x versions. To fully utilize and benefit from the entire
package, we recommend using Python3.8 at minimum (Python>=3.8).
Installation
Through pip (recommended),
pip install pybboxes
or build from source,
git clone https://github.com/devrimcavusoglu/pybboxes.git
cd pybboxes
python setup.py install
Bounding Boxes
You can easily create bounding box as easy as
from pybboxes import BoundingBox
my_coco_box = [98, 345, 322, 117]
coco_bbox = BoundingBox.from_coco(*my_coco_box) # <[98 345 322 117] (322x117) | Image: (?x?)>
# or alternatively
# coco_bbox = BoundingBox.from_array(my_coco_box)
Conversion
With the BoundingBox class the conversion is as easy as one method call.
from pybboxes import BoundingBox
my_coco_box = [98, 345, 322, 117]
coco_bbox = BoundingBox.from_coco(*my_coco_box) # <[98 345 322 117] (322x117) | Image: (?x?)>
voc_bbox = coco_bbox.to_voc() # <[98 345 420 462] (322x117) | Image: (?x?)>
voc_bbox_values = coco_bbox.to_voc(return_values=True) # (98, 345, 420, 462)
However, if you try to make conversion between two bounding boxes that require scaling/normalization it'll give an error
from pybboxes import BoundingBox
my_coco_box = [98, 345, 322, 117]
coco_bbox = BoundingBox.from_coco(*my_coco_box) # <[98 345 322 117] (322x117) | Image: (?x?)>
# yolo_bbox = coco_bbox.to_yolo() # this will raise an exception
# You need to set image_size for coco_bbox and then you're good to go
coco_bbox.image_size = (640, 480)
yolo_bbox = coco_bbox.to_yolo() # <[0.4047 0.8406 0.5031 0.2437] (322x117) | Image: (640x480)>
Image size associated with the bounding box can be given at the instantiation or while using classmethods e.g
from_coco().
from pybboxes import BoundingBox
my_coco_box = [98, 345, 322, 117]
coco_bbox = BoundingBox.from_coco(*my_coco_box, image_size=(640, 480)) # <[98 345 322 117] (322x117) | Image: (640x480)>
# no longer raises exception
yolo_bbox = coco_bbox.to_yolo() # <[0.4047 0.8406 0.5031 0.2437] (322x117) | Image: (640x480)>
Box operations
Box operations now available as of v0.1.0.
from pybboxes import BoundingBox
my_coco_box = [98, 345, 322, 117]
my_coco_box2 = [90, 350, 310, 122]
coco_bbox = BoundingBox.from_coco(*my_coco_box, image_size=(640, 480))
coco_bbox2 = BoundingBox.from_coco(*my_coco_box2, image_size=(640, 480))
iou = coco_bbox.iou(coco_bbox2) # 0.8117110631149508
area_union = coco_bbox + coco_bbox2 # 41670 | alternative way: coco_bbox.union(coco_bbox2)
total_area = coco_bbox.area + coco_bbox2.area # 75494 (not union)
intersection_area = coco_bbox + coco_bbox2 # 33824 | alternative way: coco_bbox.intersection(coco_bbox2)
first_bbox_diff = coco_bbox - coco_bbox2 # 3850
second_bbox_diff = coco_bbox2 - coco_bbox # 3996
bbox_ratio = coco_bbox / coco_bbox2 # 0.9961396086726599 (not IOU)
Functional
Note: functional computations are moved under pybboxes.functional starting with the version 0.1.0. The only
exception is that convert_bbox() which still can be used by importing pybboxes only (for backward compatibility).
Conversion
You are able to convert from any bounding box type to another.
import pybboxes as pbx
coco_bbox = (1,2,3,4) # COCO Format bbox as (x-tl,y-tl,w,h)
voc_bbox = (1,2,3,4) # Pascal VOC Format bbox as (x-tl,y-tl,x-br,y-br)
pbx.convert_bbox(coco_bbox, from_type="coco", to_type="voc") # (1, 2, 4, 6)
pbx.convert_bbox(voc_bbox, from_type="voc", to_type="coco") # (1, 2, 2, 2)
Some formats require image width and height information for scaling, e.g. YOLO bbox (resulting are round coordinates are rounded to 2 decimals to ease reading).
import pybboxes as pbx
voc_bbox = (1,2,3,4) # Pascal VOC Format bbox as (x-tl,y-tl,x-br,y-br)
pbx.convert_bbox(voc_bbox, from_type="voc", to_type="yolo", image_size=(28, 28)) # (0.07, 0.11, 0.07, 0.07)
Computation
You can also make computations on supported bounding box formats.
import pybboxes.functional as pbf
coco_bbox = (1,2,3,4) # COCO Format bbox as (x-tl,y-tl,w,h)
voc_bbox = (1,2,3,4) # Pascal VOC Format bbox as (x-tl,y-tl,x-br,y-br)
pbf.compute_area(coco_bbox, bbox_type="coco") # 12
pbf.compute_area(voc_bbox, bbox_type="voc") # 4
Contributing
Installation
Install the package as follows, which will set you ready for the development mode.
pip install -e .[dev]
Tests
To tests simply run.
python tests/run_tests.py
Code Style
To check code style,
python tests/run_code_style.py check
To format codebase,
python tests/run_code_style.py format
License
Licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pybboxes-0.1.2.tar.gz.
File metadata
- Download URL: pybboxes-0.1.2.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83740f0ca0d3cc868bbb5d1a64aa0f44507ea6091eff9ace6906a7e40d6dc82b
|
|
| MD5 |
70561f69426eff8dd20311dec19f8ee1
|
|
| BLAKE2b-256 |
89d5799f518e7fc63ba5567a2df8c0e4063badfe753b543f7df0f7cc2f068cc9
|
File details
Details for the file pybboxes-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pybboxes-0.1.2-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9912821f7455b204e05354452f7f68bb334da9bfea78a852ffb313c40a364806
|
|
| MD5 |
ef9d6ffd80875181ada6b4d522fc34e8
|
|
| BLAKE2b-256 |
4ef98d96ac19474d1eb696e02b639c4f0f75f1afde8f4d305045a8501558b854
|