tensorbay.geometry.box¶
Box2D, Box3D.
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.
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.
- class tensorbay.geometry.box.Box2D(xmin: float, ymin: float, xmax: float, ymax: float)[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 providesBox2D.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: tensorbay.geometry.box.Box2D, box2: tensorbay.geometry.box.Box2D) float [source]¶
Calculate the intersection over union of two 2D boxes.
- Parameters
box1 – A 2D box.
box2 – A 2D box.
- Returns
The intersection over union between the two input boxes.
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: float, y: float, width: float, height: float) tensorbay.geometry.box._B2 [source]¶
Create a
Box2D
instance from the top-left vertex and the width and the height.- Parameters
x – X coordinate of the top left vertex of the box.
y – Y coordinate of the top left vertex of the box.
width – Length of the box along the x axis.
height – Length of the box along the y axis.
- Returns
The created
Box2D
instance.
Examples
>>> Box2D.from_xywh(1, 2, 3, 4) Box2D(1, 2, 4, 6)
- classmethod loads(contents: Dict[str, float]) tensorbay.geometry.box._B2 [source]¶
Load a
Box2D
from a dict containing coordinates of the 2D box.- Parameters
contents – A dict containing coordinates of a 2D box.
- Returns
The loaded
Box2D
object.
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
- class tensorbay.geometry.box.Box3D(size: Iterable[float], translation: Iterable[float] = (0, 0, 0), rotation: Union[Iterable[float], quaternion.quaternion] = (1, 0, 0, 0), *, transform_matrix: Optional[Union[Sequence[Sequence[float]], numpy.ndarray]] = 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 providesBox3D.iou()
to calculate the intersection over union of two 3D boxes.- Parameters
translation – Translation in a sequence of [x, y, z].
rotation – Rotation in a sequence of [w, x, y, z] or numpy quaternion.
size – Size in a sequence of [x, y, z].
transform_matrix – A 4x4 or 3x4 transform matrix.
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: Dict[str, Dict[str, float]]) tensorbay.geometry.box._B3 [source]¶
Load a
Box3D
from a dict containing the coordinates of the 3D box.- Parameters
contents – A dict containing the coordinates of a 3D box.
- Returns
The loaded
Box3D
object.
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: tensorbay.geometry.box.Box3D, box2: tensorbay.geometry.box.Box3D, angle_threshold: float = 5) float [source]¶
Calculate the intersection over union between two 3D boxes.
- Parameters
box1 – A 3D box.
box2 – A 3D box.
angle_threshold – The threshold of the relative angles between two input 3d boxes in degree.
- Returns
The intersection over union of the two 3D boxes.
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() float [source]¶
Return the volume of the 3D box.
- Returns
The volume of the 3D box.
Examples
>>> box3d = Box3D(size=(1, 2, 3)) >>> box3d.volume() 6
- dumps() Dict[str, Dict[str, float]] [source]¶
Dumps the 3D box into a dict.
- Returns
A dict containing translation, rotation and size information.
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}, }