Coretex
coretex.entities.annotation.image.bbox.BBox Class Reference
Inheritance diagram for coretex.entities.annotation.image.bbox.BBox:
coretex.codable.codable.Codable

Public Member Functions

int maxX (self)
 
int maxY (self)
 
List[int] polygon (self)
 
Self create (cls, int minX, int minY, int maxX, int maxY)
 
Self fromPoly (cls, List[int] polygon)
 
float iou (self, 'BBox' other)
 
None inflate (self, int percentage, Optional[Tuple[Union[int, float], Union[int, float]]] imageSize=None)
 
- Public Member Functions inherited from coretex.codable.codable.Codable
Dict[str, Any] encode (self)
 
None onDecode (self)
 
Self decode (cls, Dict[str, Any] encodedObject)
 

Detailed Description

    Bounding Box as a python class with utility methods

    Properties
    ----------
    minX : int
        top left x coordinate
    minY : int
        top left y coordinate
    width : int
        width of the bounding box
    height : int
        height of the bounding box

Definition at line 24 of file bbox.py.

Member Function Documentation

◆ create()

Self coretex.entities.annotation.image.bbox.BBox.create (   cls,
int  minX,
int  minY,
int  maxX,
int  maxY 
)
    Utility constructor which has maxX and maxY as parameters instead
    of width and height

    Parameters
    ----------
    minX : int
        top left x coordinate
    minY : int
        top left y coordinate
    maxX : int
        bottom right x coordinate
    maxY : int
        bottom right y coordinate

    Returns
    -------
    Self -> bounding box

Definition at line 98 of file bbox.py.

98  def create(cls, minX: int, minY: int, maxX: int, maxY: int) -> Self:
99  """
100  Utility constructor which has maxX and maxY as parameters instead
101  of width and height
102 
103  Parameters
104  ----------
105  minX : int
106  top left x coordinate
107  minY : int
108  top left y coordinate
109  maxX : int
110  bottom right x coordinate
111  maxY : int
112  bottom right y coordinate
113 
114  Returns
115  -------
116  Self -> bounding box
117  """
118 
119  return cls(minX, minY, maxX - minX, maxY - minY)
120 

◆ fromPoly()

Self coretex.entities.annotation.image.bbox.BBox.fromPoly (   cls,
List[int]  polygon 
)
    Creates bounding box from a polygon, by finding
    the minimum x and y coordinates and calculating
    width and height of the polygon

    Parameters
    ----------
    polygon : List[int]
        list of x, y points - length must be even

    Returns
    -------
    Self -> bounding box

    Example
    -------
    >>> from coretex import Bbox
    \b
    >>> polygon = [0, 0, 0, 3, 4, 3, 4, 0]
    >>> bbox = Bbox.fromPoly(polygon)
    >>> print(f"minX: {bbox.minX}, minY: {bbox.minY}, width: {bbox.width}, height: {bbox.height}")
    "minX: 0, minY: 0, width: 4, height: 3"

Definition at line 122 of file bbox.py.

122  def fromPoly(cls, polygon: List[int]) -> Self:
123  """
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
127 
128  Parameters
129  ----------
130  polygon : List[int]
131  list of x, y points - length must be even
132 
133  Returns
134  -------
135  Self -> bounding box
136 
137  Example
138  -------
139  >>> from coretex import Bbox
140  \b
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"
145  """
146 
147  x: List[int] = []
148  y: List[int] = []
149 
150  for index, value in enumerate(polygon):
151  if index % 2 == 0:
152  x.append(value)
153  else:
154  y.append(value)
155 
156  return cls.create(min(x), min(y), max(x), max(y))
157 

◆ inflate()

None coretex.entities.annotation.image.bbox.BBox.inflate (   self,
int  percentage,
Optional[Tuple[Union[int, float], Union[int, float]]]   imageSize = None 
)
    Increases the size of the bounding box by a percentage

    Parameters
    ----------
    percentage : int
        the percentage by which the bounding box will be inflated
    imageSize : Optional[Tuple[int, int]]
        bounding box will not be able to go beyond these dimensions (width, height)

Definition at line 182 of file bbox.py.

182  def inflate(self, percentage: int, imageSize: Optional[Tuple[Union[int, float], Union[int, float]]] = None) -> None:
183  """
184  Increases the size of the bounding box by a percentage
185 
186  Parameters
187  ----------
188  percentage : int
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)
192  """
193 
194  if imageSize is None:
195  imageSize = (float("inf"), float("inf"))
196 
197  imageWidth, imageHeight = imageSize
198 
199  inflateFactor = percentage / 100.0
200  inflateWidth = self.width * inflateFactor / 2
201  inflateHeight = self.height * inflateFactor / 2
202 
203  self.minX = int(max(0, self.minX - inflateWidth))
204  self.minY = int(max(0, self.minY - inflateHeight))
205  self.width = int(min(imageWidth, self.width + inflateWidth * 2))
206  self.height = int(min(imageHeight, self.height + inflateHeight * 2))

◆ iou()

float coretex.entities.annotation.image.bbox.BBox.iou (   self,
'BBox other 
)
    Calculate Intersection over Union (IoU) between two bounding boxes

    Parameters
    ----------
    other : BBox
        bounding box for which the IoU will be calculated

    Returns
    -------
    float -> IoU score

Definition at line 158 of file bbox.py.

158  def iou(self, other: 'BBox') -> float:
159  """
160  Calculate Intersection over Union (IoU) between two bounding boxes
161 
162  Parameters
163  ----------
164  other : BBox
165  bounding box for which the IoU will be calculated
166 
167  Returns
168  -------
169  float -> IoU score
170  """
171 
172  x1 = max(self.minX, other.minX)
173  y1 = max(self.minY, other.minY)
174  x2 = min(self.maxX, other.maxX)
175  y2 = min(self.maxY, other.maxY)
176 
177  intersectionArea = max(0, x2 - x1) * max(0, y2 - y1)
178 
179  unionArea = self.area + other.area - intersectionArea
180  return intersectionArea / unionArea if unionArea > 0 else 0.0
181 

◆ maxX()

int coretex.entities.annotation.image.bbox.BBox.maxX (   self)
    Returns
    -------
    int -> bottom right x coordinate

Definition at line 49 of file bbox.py.

49  def maxX(self) -> int:
50  """
51  Returns
52  -------
53  int -> bottom right x coordinate
54  """
55 
56  return self.minX + self.width
57 

◆ maxY()

int coretex.entities.annotation.image.bbox.BBox.maxY (   self)
    Returns
    -------
    int -> bottom right y coordinate

Definition at line 59 of file bbox.py.

59  def maxY(self) -> int:
60  """
61  Returns
62  -------
63  int -> bottom right y coordinate
64  """
65 
66  return self.minY + self.height
67 

◆ polygon()

List[int] coretex.entities.annotation.image.bbox.BBox.polygon (   self)
    Returns
    -------
    List[int] -> Bounding box represented as a polygon (x, y) values

Definition at line 69 of file bbox.py.

69  def polygon(self) -> List[int]:
70  """
71  Returns
72  -------
73  List[int] -> Bounding box represented as a polygon (x, y) values
74  """
75 
76  return [
77  self.minX, self.minY, # top left
78  self.maxX, self.minY, # top right
79  self.maxX, self.maxY, # bottom right
80  self.minX, self.maxY, # bottom left
81  self.minX, self.minY # top left
82  ]
83 

The documentation for this class was generated from the following file: