Skip to main content

Utilities for comparing bounding boxes

Project description

box-diff

Utilities for comparing bounding boxes

Author: Kevin Barnard, kbarnard@mbari.org

tests

Installation

box-diff is available on PyPI as boxdiff:

pip install boxdiff

Usage

All of box-diff is housed within the boxdiff package.

import boxdiff

The core data models are defined in the boxdiff.models module. These are split into three groups:

  • BoundingBox: ID + labeled 2D bounding box
  • Image: ID + collection of bounding boxes
  • ImageSet: ID + collection of images

Note: IDs may be integers, UUIDs, or strings.

Each group has a data model (defined in boxdiff.models.core), a delta (boxdiff.models.deltas), and a difference flag (boxdiff.models.flags). The data model represents the object and its attributes, whereas a delta represents the difference in attribute values between two objects. Difference flags represent the presence of attribute differences between two objects as derived from a delta object.

All of the data models are serializable to JSON. For example,

json_str = bounding_box.to_json(indent=2)

print(json_str)

might give

{
  "id": 0,
  "label": "label",
  "x": 0.0,
  "y": 0.0,
  "width": 1.0,
  "height": 1.0
}

Similarly, data model objects may be parsed from JSON. For example,

bounding_box = BoundingBox.from_json(json_str)

print(bounding_box)
# BoundingBox(id=0, label='label', x=0.0, y=0.0, width=1.0, height=1.0)

Bounding Boxes

A BoundingBox is defined by an ID, a label, and a 2D box (x, y, width, height).

from boxdiff import BoundingBox

car_box = BoundingBox(
    id=0,
    label='car',
    x=100, y=200,
    width=300, height=80
)

Equality can be checked using the == operator:

same_car_box = BoundingBox(
    id=0,
    label='car',
    x=100, y=200,
    width=300, height=80
)

print(car_box == same_car_box)
# True

corrected_car_box = BoundingBox(
    id=0,
    label='car',
    x=90, y=210,
    width=320, height=85
)

print(car_box == corrected_car_box)
# False

A BoundingBoxDelta between two boxes can be computed with the - operator:

box_delta = corrected_car_box - car_box

print(box_delta)
# BoundingBoxDelta(id=1, label_old='car', label_new='car', x_delta=-10.0, y_delta=10.0, width_delta=20.0, height_delta=5.0)

BoundingBoxDifference flags may then be computed from the delta:

print(box_delta.flags)
# BoundingBoxDifference.RESIZED|MOVED

Deltas may be applied to a bounding box using the + operator:

new_car_box = car_box + box_delta

print(new_car_box)
# BoundingBox(id=1, label='car', x=90.0, y=210.0, width=320.0, height=85.0)

print(new_car_box == corrected_car_box)
# True

Area may be computed from a bounding box:

print(car_box.area)
# 24000.0

Intersection over union between bounding boxes can be computed using the iou method:

car_iou = car_box.iou(corrected_car_box)

print(car_iou)
# 0.695364238410596

Images

An Image is defined by an ID and a collection of bounding boxes.

from boxdiff import Image
from uuid import UUID

image = Image(
    id=UUID('78d76772-4664-467c-ae88-a25496234966'),
    bounding_boxes=[car_box]
)

Likewise, equality can be checked using the == operator and deltas computed using the - operator:

corrected_image = Image(
    id=UUID('78d76772-4664-467c-ae88-a25496234966'),
    bounding_boxes=[corrected_car_box]
)

image_delta = corrected_image - image

print(image_delta)
# ImageDelta(
#   id=0, 
#   boxes_added=[], 
#   boxes_removed=[], 
#   box_deltas=[
#     BoundingBoxDelta(
#       id=0, 
#       label_old='car', 
#       label_new='car', 
#       x_delta=-10.0, 
#       y_delta=10.0, 
#       width_delta=20.0, 
#       height_delta=5.0
#     )
#   ]
# )

Similarly, flags may be computed from the delta:

print(image_delta.flags)
# ImageDifference.BOXES_MODIFIED

Image Sets

An ImageSet is defined by an ID and a collection of images.

from boxdiff import ImageSet

image_set = ImageSet(
    id='my_image_set',
    images=[image, ...]
)

Its syntax and structure is analogous to that of an Image.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boxdiff-0.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

boxdiff-0.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file boxdiff-0.2.0.tar.gz.

File metadata

  • Download URL: boxdiff-0.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.5.0-15-generic

File hashes

Hashes for boxdiff-0.2.0.tar.gz
Algorithm Hash digest
SHA256 02f1fbead0f9710b4260b71ed69be6b31d066c614efff439b0af6697ce7fef1a
MD5 0ea59bca178ee66d700dcf608744f968
BLAKE2b-256 5e4e3946ae2c95d81034513d8c4e9dde55abb59e0ac0d9770e9d04c9069d368c

See more details on using hashes here.

File details

Details for the file boxdiff-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: boxdiff-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.5.0-15-generic

File hashes

Hashes for boxdiff-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e771da66a7b6c5cbeb7582a5e35eb0a43b814415d86b62fd5aa35c066f40f7ed
MD5 52bd56ab23e7bede8b1c3daf15b241b4
BLAKE2b-256 dcbeacbd8b41e0d45cffe37ad341d9ef99464f61018a2aae41f61d8e4db00ab0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page