tensorbay.geometry.box#

The implementation of the TensorBay bounding box.

class tensorbay.geometry.box.Box2D(xmin, ymin, xmax, ymax)[source]#

Bases: tensorbay.utility.user.UserSequence[float]

This class defines the concept of Box2D.

Box2D contains the information of a 2D bounding box, such as the coordinates, width and height. It provides Box2D.iou() to calculate the intersection over union of two 2D boxes.

Parameters
  • xmin – The x coordinate of the top-left vertex of the 2D box.

  • ymin – The y coordinate of the top-left vertex of the 2D box.

  • xmax – The x coordinate of the bottom-right vertex of the 2D box.

  • ymax – The y coordinate of the bottom-right vertex of the 2D box.

Examples

>>> Box2D(1, 2, 3, 4)
Box2D(1, 2, 3, 4)
static iou(box1, box2)[source]#

Calculate the intersection over union of two 2D boxes.

Parameters
Returns

The intersection over union between the two input boxes.

Return type

float

Examples

>>> box2d_1 = Box2D(1, 2, 3, 4)
>>> box2d_2 = Box2D(2, 2, 3, 4)
>>> Box2D.iou(box2d_1, box2d_2)
0.5
classmethod from_xywh(x, y, width, height)[source]#

Create a Box2D instance from the top-left vertex and the width and the height.

Parameters
  • x (float) – X coordinate of the top left vertex of the box.

  • y (float) – Y coordinate of the top left vertex of the box.

  • width (float) – Length of the box along the x axis.

  • height (float) – Length of the box along the y axis.

Returns

The created Box2D instance.

Return type

tensorbay.geometry.box._B2

Examples

>>> Box2D.from_xywh(1, 2, 3, 4)
Box2D(1, 2, 4, 6)
classmethod loads(contents)[source]#

Load a Box2D from a dict containing coordinates of the 2D box.

Parameters

contents (Mapping[str, float]) – A dict containing coordinates of a 2D box.

Returns

The loaded Box2D object.

Return type

tensorbay.geometry.box._B2

Examples

>>> contents = {"xmin": 1.0, "ymin": 2.0, "xmax": 3.0, "ymax": 4.0}
>>> Box2D.loads(contents)
Box2D(1.0, 2.0, 3.0, 4.0)
property xmin: float#

Return the minimum x coordinate.

Returns

Minimum x coordinate.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.xmin
1
property ymin: float#

Return the minimum y coordinate.

Returns

Minimum y coordinate.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.ymin
2
property xmax: float#

Return the maximum x coordinate.

Returns

Maximum x coordinate.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.xmax
3
property ymax: float#

Return the maximum y coordinate.

Returns

Maximum y coordinate.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.ymax
4
property tl: tensorbay.geometry.vector.Vector2D#

Return the top left point.

Returns

The top left point.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.tl
Vector2D(1, 2)
property br: tensorbay.geometry.vector.Vector2D#

Return the bottom right point.

Returns

The bottom right point.

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.br
Vector2D(3, 4)
property width: float#

Return the width of the 2D box.

Returns

The width of the 2D box.

Examples

>>> box2d = Box2D(1, 2, 3, 6)
>>> box2d.width
2
property height: float#

Return the height of the 2D box.

Returns

The height of the 2D box.

Examples

>>> box2d = Box2D(1, 2, 3, 6)
>>> box2d.height
4
dumps()[source]#

Dumps a 2D box into a dict.

Returns

A dict containing vertex coordinates of the box.

Return type

Dict[str, float]

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.dumps()
{'xmin': 1, 'ymin': 2, 'xmax': 3, 'ymax': 4}
area()[source]#

Return the area of the 2D box.

Returns

The area of the 2D box.

Return type

float

Examples

>>> box2d = Box2D(1, 2, 3, 4)
>>> box2d.area()
4
class tensorbay.geometry.box.Box3D(size, translation=(0, 0, 0), rotation=(1, 0, 0, 0), *, transform_matrix=None)[source]#

Bases: tensorbay.utility.repr.ReprMixin

This class defines the concept of Box3D.

Box3D contains the information of a 3D bounding box such as the transform, translation, rotation and size. It provides Box3D.iou() to calculate the intersection over union of two 3D boxes.

Parameters
  • translation (Iterable[float]) – Translation in a sequence of [x, y, z].

  • rotation (Union[Iterable[float], quaternion.quaternion]) – Rotation in a sequence of [w, x, y, z] or numpy quaternion.

  • size (Iterable[float]) – Size in a sequence of [x, y, z].

  • transform_matrix (Optional[Union[Sequence[Sequence[float]], numpy.ndarray]]) – A 4x4 or 3x4 transform matrix.

Return type

None

Examples

Initialization Method 1: Init from size, translation and rotation.

>>> Box3D([1, 2, 3], [0, 1, 0, 0], [1, 2, 3])
Box3D(
  (size): Vector3D(1, 2, 3)
  (translation): Vector3D(1, 2, 3),
  (rotation): quaternion(0, 1, 0, 0),
)

Initialization Method 2: Init from size and transform matrix.

>>> from tensorbay.geometry import Transform3D
>>> matrix = [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3]]
>>> Box3D(size=[1, 2, 3], transform_matrix=matrix)
Box3D(
  (size): Vector3D(1, 2, 3)
  (translation): Vector3D(1, 2, 3),
  (rotation): quaternion(1, -0, -0, -0),
)
classmethod loads(contents)[source]#

Load a Box3D from a dict containing the coordinates of the 3D box.

Parameters

contents (Mapping[str, Mapping[str, float]]) – A dict containing the coordinates of a 3D box.

Returns

The loaded Box3D object.

Return type

tensorbay.geometry.box._B3

Examples

>>> contents = {
...     "size": {"x": 1.0, "y": 2.0, "z": 3.0},
...     "translation": {"x": 1.0, "y": 2.0, "z": 3.0},
...     "rotation": {"w": 0.0, "x": 1.0, "y": 0.0, "z": 0.0},
... }
>>> Box3D.loads(contents)
Box3D(
  (size): Vector3D(1.0, 2.0, 3.0)
  (translation): Vector3D(1.0, 2.0, 3.0),
  (rotation): quaternion(0, 1, 0, 0),
)
classmethod iou(box1, box2, angle_threshold=5)[source]#

Calculate the intersection over union between two 3D boxes.

Parameters
Returns

The intersection over union of the two 3D boxes.

Return type

float

Examples

>>> box3d_1 = Box3D(size=[1, 1, 1])
>>> box3d_2 = Box3D(size=[2, 2, 2])
>>> Box3D.iou(box3d_1, box3d_2)
0.125
property translation: tensorbay.geometry.vector.Vector3D#

Return the translation of the 3D box.

Returns

The translation of the 3D box.

Examples

>>> box3d = Box3D(size=(1, 1, 1), translation=(1, 2, 3))
>>> box3d.translation
Vector3D(1, 2, 3)
property rotation: quaternion.quaternion#

Return the rotation of the 3D box.

Returns

The rotation of the 3D box.

Examples

>>> box3d = Box3D(size=(1, 1, 1), rotation=(0, 1, 0, 0))
>>> box3d.rotation
quaternion(0, 1, 0, 0)
property transform: tensorbay.geometry.transform.Transform3D#

Return the transform of the 3D box.

Returns

The transform of the 3D box.

Examples

>>> box3d = Box3D(size=(1, 1, 1), translation=(1, 2, 3), rotation=(1, 0, 0, 0))
>>> box3d.transform
Transform3D(
  (translation): Vector3D(1, 2, 3),
  (rotation): quaternion(1, 0, 0, 0)
)
property size: tensorbay.geometry.vector.Vector3D#

Return the size of the 3D box.

Returns

The size of the 3D box.

Examples

>>> box3d = Box3D(size=(1, 1, 1))
>>> box3d.size
Vector3D(1, 1, 1)
volume()[source]#

Return the volume of the 3D box.

Returns

The volume of the 3D box.

Return type

float

Examples

>>> box3d = Box3D(size=(1, 2, 3))
>>> box3d.volume()
6
dumps()[source]#

Dumps the 3D box into a dict.

Returns

A dict containing translation, rotation and size information.

Return type

Dict[str, Dict[str, float]]

Examples

>>> box3d = Box3D(size=(1, 2, 3), translation=(1, 2, 3), rotation=(0, 1, 0, 0))
>>> box3d.dumps()
{
    "translation": {"x": 1, "y": 2, "z": 3},
    "rotation": {"w": 0.0, "x": 1.0, "y": 0.0, "z": 0.0},
    "size": {"x": 1, "y": 2, "z": 3},
}