Skip to main content

boxobj

boxobj is a simple library that makes working with computer vision, OCR, and other tools that interact with regions of an image or document, simpler and more predictable.

Box

The core class in this library is Box. It handles many of the common approaches that different libraries use to reference the region containing the returned results including:

  • coordinates: [x1, y1, x2, y2]
  • a dict containing the position defined by top, left, width, height
  • relative coordinates or position defined as a percentage of the size of the source: [0.2, 0.5, 0.3, 0.8]

In addition, it supports conversion from bottom indexed boxes like those used in older file types and libraries such as PDFs.

Box internally represents its coordinates in pixels that are measured from the top-left, but it can return results using other approaches as needed.

from boxobj import Box


tlwh = { 'top': 10, 'left': 10, 'width': 30, 'height': 30 }
coords = [10, 10, 40, 40]
coords_ratio = [0.1, 0.1, 0.4, 0.4]
obj = {
    'coordinates': [10, 10, 40, 40],
    'label': 'green',
    'name': 'my box'
}

box = Box.from_position(tlwh)
box = Box.from_coords(coords)
box = Box.from_position_percentage(coords_ratio, width=100, height=100)
box = Box.from_position_percentage(coords_ratio, top_origin=False, size=(100,100))
box = Box.from_dict(obj)

The Box object supports a range of pythonic interactions including sort, addition, subtraction, multiplication, division and area calculations.

from boxobj import Box


box_a = Box([10, 10, 40, 40])
box_b = Box([15, 10, 30, 50])
boxes = [box_b, box_a]

# sort the boxes vertically
boxes.sort()
# sort the boxes horizontally
boxes.sort(key=lambda x: x.left)

# shift the position of a box
box_a_shifted_down = box_a + [0, 10] # [10, 20, 40, 50]
box_a_shifted_left = box_a - [5, 0] # [5, 10, 35, 40]

# scale up the size of a box
box_a_bigger = box_a * 2 # [20, 20, 80, 80]
box_a_smaller = box_a / 3 # [3.333333, 3.333333, 13.333333, 13.333333]
box_a_smaller = round(box_a_smaller) # [3.3, 3.3, 13.3, 13.3]
box_a_smaller_floor = box_a // 3 # [3, 3, 13, 13]

# Combine two boxes (union)
box_c = box_a + box_b # [10, 10, 40, 50]

# Get the intersection of two boxes
box_i = box_a & box_b # [15, 10, 30, 40]

# Get the area of a box
box_a_area = abs(box_a) # 900
box_b_area = box_a.area # 600

Note that to avoid the excessive precision that frequently makes its way into Boxes as they are moved and scaled, the repr of a Box object limits the output to six digits after the decimal.

Of course, these operands can be combined to perform common operations such as the common intersection over union calculation used to evaluate agreement between two models.

iou = abs(box_a & box_b) / abs(box_a + box_b) # 0.375

Attributes

The Box object also supports assigning attributes such as a label or text to the object to associate it with a set of values.

from boxobj import Box

cv_box = Box([10, 10, 30, 50], {"label": "cat", "confidence": 0.84})
cv_box_2 = Box({"coordinates": [10, 10, 30, 50], "label": "cat", "confidence": 0.84})
ocr_box = Box([3, 4, 67, 10], text="Boxo is great for working with boxes")

As you can see, there are a variety of options for creating a Box object from whatever source system produced it. Here the cv_box value is created by passing the coordinates first, then a dict containing the associated attributes. cv_box_2 is the same value, but here the coordinates and attributes are contained in a single dict object. Finally, the ocr_box is created by passing coordinates and using keyword arguments to assign attributes. This flexibility is intentional to make it easier to handle the varying approaches different tools use to represent data.

Internally the boxobj classes keep the attributes and coordinate values separate to make them easier to work with. This shows up in how the objects are represented when printed or displayed in the output. The following is the representation of the objects above.

Box([10, 10, 30, 50], {'label': 'cat', 'confidence': 0.84})
Box([10, 10, 30, 50], {'label': 'cat', 'confidence': 0.84})
Box([3, 4, 67, 10], {'text': 'Boxo is great for working with boxes'})

data and serialization

page pages page indices

Metadata

Release files for boxobj 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for boxobj 0.1.2
File Size Uploaded
boxobj-0.1.2.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for boxobj 0.1.2
File Interpreter ABI Platform
boxobj-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / boxobj-0.1.2.tar.gz

Download URL boxobj-0.1.2.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
631b5554a6b6a73c366db127c6476767a7378d1ef0f78b7292ac16a4198ff880
BLAKE2b-256 checksum
How to use checksums
45d5f3ac1412b452e6a9547629e877903611d9a8748ce5d536ed3189b97f90a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.1 Windows/11

Release files / boxobj-0.1.2-py3-none-any.whl

Download URL boxobj-0.1.2-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8252e911b06e058cc868a5a1c6585e84b348c4746439bf1f911ad66d61e5a597
BLAKE2b-256 checksum
How to use checksums
99bda500085cadd5c2df4f0a35fd794f0b1f26675d0cd83abb8cd80eea1d350c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.1 Windows/11

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page