graphid.util.util_boxes module

graphid.util.util_boxes.box_ious_py(boxes1, boxes2, bias=1)[source]

This is the fastest python implementation of bbox_ious I found

class graphid.util.util_boxes.Boxes(data, format='xywh')[source]

Bases: NiceRepr

Converts boxes between different formats as long as the last dimension contains 4 coordinates and the format is specified.

This is a convinience class, and should not not store the data for very long. The general idiom should be create class, convert data, and then get the raw data and let the class be garbage collected. This will help ensure that your code is portable and understandable if this class is not available.

Example

>>> # xdoctest: +IGNORE_WHITESPACE
>>> Boxes([25, 30, 15, 10], 'xywh')
<Boxes(xywh, array([25, 30, 15, 10]))>
>>> Boxes([25, 30, 15, 10], 'xywh').to_xywh()
<Boxes(xywh, array([25, 30, 15, 10]))>
>>> Boxes([25, 30, 15, 10], 'xywh').to_cxywh()
<Boxes(cxywh, array([32.5, 35. , 15. , 10. ]))>
>>> Boxes([25, 30, 15, 10], 'xywh').to_tlbr()
<Boxes(tlbr, array([25, 30, 40, 40]))>
>>> Boxes([25, 30, 15, 10], 'xywh').scale(2).to_tlbr()
<Boxes(tlbr, array([50., 60., 80., 80.]))>

Example

>>> datas = [
>>>     [1, 2, 3, 4],
>>>     [[1, 2, 3, 4], [4, 5, 6, 7]],
>>>     [[[1, 2, 3, 4], [4, 5, 6, 7]]],
>>> ]
>>> formats = ['xywh', 'cxywh', 'tlbr']
>>> for format1 in formats:
>>>     for data in datas:
>>>         self = box1 = Boxes(data, format1)
>>>         for format2 in formats:
>>>             box2 = box1.toformat(format2)
>>>             back = box2.toformat(format1)
>>>             assert box1 == back
classmethod random(num=1, scale=1.0, format='xywh', rng=None)[source]

Makes random boxes

Example

>>> # xdoctest: +IGNORE_WHITESPACE
>>> Boxes.random(3, rng=0, scale=100)
<Boxes(xywh,
    array([[27, 35, 30, 27],
           [21, 32, 21, 44],
           [48, 19, 39, 26]]))>
copy()[source]
scale(factor)[source]

works with tlbr, cxywh, xywh, xy, or wh formats

Example

>>> # xdoctest: +IGNORE_WHITESPACE
>>> Boxes(np.array([1, 1, 10, 10])).scale(2).data
array([ 2.,  2., 20., 20.])
>>> Boxes(np.array([[1, 1, 10, 10]])).scale((2, .5)).data
array([[ 2. ,  0.5, 20. ,  5. ]])
>>> Boxes(np.array([[10, 10]])).scale(.5).data
array([[5., 5.]])
shift(amount)[source]

Example

>>> # xdoctest: +IGNORE_WHITESPACE
>>> Boxes([25, 30, 15, 10], 'xywh').shift(10)
<Boxes(xywh, array([35., 40., 15., 10.]))>
>>> Boxes([25, 30, 15, 10], 'xywh').shift((10, 0))
<Boxes(xywh, array([35., 30., 15., 10.]))>
>>> Boxes([25, 30, 15, 10], 'tlbr').shift((10, 5))
<Boxes(tlbr, array([35., 35., 25., 15.]))>
property center
property shape
property area
property components
classmethod _cat(datas)[source]
toformat(format, copy=True)[source]
to_extent(copy=True)[source]
to_xywh(copy=True)[source]
to_cxywh(copy=True)[source]
to_tlbr(copy=True)[source]
clip(x_min, y_min, x_max, y_max, inplace=False)[source]

Clip boxes to image boundaries. If box is in tlbr format, inplace operation is an option.

Example

>>> # xdoctest: +IGNORE_WHITESPACE
>>> boxes = Boxes(np.array([[-10, -10, 120, 120], [1, -2, 30, 50]]), 'tlbr')
>>> clipped = boxes.clip(0, 0, 110, 100, inplace=False)
>>> assert np.any(boxes.data != clipped.data)
>>> clipped2 = boxes.clip(0, 0, 110, 100, inplace=True)
>>> assert clipped2.data is boxes.data
>>> assert np.all(clipped2.data == clipped.data)
>>> print(clipped)
<Boxes(tlbr,
    array([[  0,   0, 110, 100],
           [  1,   0,  30,  50]]))>
transpose()[source]
compress(flags, axis=0, inplace=False)[source]

Filters boxes based on a boolean criterion

Example

>>> self = Boxes([[25, 30, 15, 10]], 'tlbr')
>>> flags = [False]