18 from typing
import List, Dict, Optional, Tuple, Union
19 from typing_extensions
import Self
21 from ....codable
import Codable, KeyDescriptor
27 Bounding Box as a python class with utility methods
36 width of the bounding box
38 height of the bounding box
41 def __init__(self, minX: int = 0, minY: int = 0, width: int = 0, height: int = 0) ->
None:
45 self.
widthwidth = width
53 int -> bottom right x coordinate
63 int -> bottom right y coordinate
73 List[int] -> Bounding box represented as a polygon (x, y) values
85 def area(self) -> int:
89 def _keyDescriptors(cls) -> Dict[str, KeyDescriptor]:
90 descriptors = super()._keyDescriptors()
98 def create(cls, minX: int, minY: int, maxX: int, maxY: int) -> Self:
100 Utility constructor which has maxX and maxY as parameters instead
106 top left x coordinate
108 top left y coordinate
110 bottom right x coordinate
112 bottom right y coordinate
119 return cls(minX, minY, maxX - minX, maxY - minY)
124 Creates bounding box from a polygon, by finding
125 the minimum x and y coordinates and calculating
126 width and height of the polygon
131 list of x, y points - length must be even
139 >>> from coretex import Bbox
141 >>> polygon = [0, 0, 0, 3, 4, 3, 4, 0]
142 >>> bbox = Bbox.fromPoly(polygon)
143 >>> print(f"minX: {bbox.minX}, minY: {bbox.minY}, width: {bbox.width}, height: {bbox.height}")
144 "minX: 0, minY: 0, width: 4, height: 3"
150 for index, value
in enumerate(polygon):
156 return cls.
createcreate(min(x), min(y), max(x), max(y))
158 def iou(self, other:
'BBox') -> float:
160 Calculate Intersection over Union (IoU) between two bounding boxes
165 bounding box for which the IoU will be calculated
172 x1 = max(self.
minXminX, other.minX)
173 y1 = max(self.
minYminY, other.minY)
174 x2 = min(self.
maxXmaxX, other.maxX)
175 y2 = min(self.
maxYmaxY, other.maxY)
177 intersectionArea = max(0, x2 - x1) * max(0, y2 - y1)
179 unionArea = self.
areaarea + other.area - intersectionArea
180 return intersectionArea / unionArea
if unionArea > 0
else 0.0
182 def inflate(self, percentage: int, imageSize: Optional[Tuple[Union[int, float], Union[int, float]]] =
None) ->
None:
184 Increases the size of the bounding box by a percentage
189 the percentage by which the bounding box will be inflated
190 imageSize : Optional[Tuple[int, int]]
191 bounding box will not be able to go beyond these dimensions (width, height)
194 if imageSize
is None:
195 imageSize = (float(
"inf"), float(
"inf"))
197 imageWidth, imageHeight = imageSize
199 inflateFactor = percentage / 100.0
200 inflateWidth = self.
widthwidth * inflateFactor / 2
201 inflateHeight = self.
heightheight * inflateFactor / 2
203 self.
minXminX = int(max(0, self.
minXminX - inflateWidth))
204 self.
minYminY = int(max(0, self.
minYminY - inflateHeight))
205 self.
widthwidth = int(min(imageWidth, self.
widthwidth + inflateWidth * 2))
206 self.
heightheight = int(min(imageHeight, self.
heightheight + inflateHeight * 2))
Self fromPoly(cls, List[int] polygon)
None inflate(self, int percentage, Optional[Tuple[Union[int, float], Union[int, float]]] imageSize=None)
Self create(cls, int minX, int minY, int maxX, int maxY)
float iou(self, 'BBox' other)