Skip to content

Commit aab9d95

Browse files
committed
use composition instead of inheritance for BoundingBox
This prevents issues with version 2 of shapely
1 parent a24d0c2 commit aab9d95

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/mtm/detection.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def contains(self, detection2):
155155
pass
156156

157157

158-
class BoundingBox(polygon.Polygon, Detection):
158+
class BoundingBox(Detection):
159159
"""
160160
Describe a detection as a rectangular axis-aligned bounding box.
161161
@@ -176,7 +176,7 @@ class BoundingBox(polygon.Polygon, Detection):
176176

177177
def __init__(self, bbox, score, templateIndex=0, label=""):
178178
x, y, width, height = bbox
179-
super().__init__( [(x,y), (x+width-1,y), (x+width-1, y+height-1), (x, y+height-1)] )
179+
self.polygon = polygon.Polygon( [(x,y), (x+width-1,y), (x+width-1, y+height-1), (x, y+height-1)] )
180180
self.xywh = bbox
181181
self.score = score
182182
self.templateIndex = templateIndex
@@ -211,25 +211,31 @@ def get_xywh(self):
211211
def __repr__(self):
212212
return self.__str__()
213213

214-
def intersection_area(self, detection2):
214+
def intersection_area(self, bbox2):
215215
"""Compute the interesection area between this bounding-box and another detection (bounding-box or other shape)."""
216-
return self.intersection(detection2).area
216+
return self.polygon.intersection(bbox2.polygon).area
217217

218-
def union_area(self, detection2):
218+
def union_area(self, bbox2):
219219
"""Compute the union area between this bounding-box and another detection (bounding-box or other shape)."""
220-
return self.union(detection2).area
220+
return self.polygon.union(bbox2.polygon).area
221221

222-
def intersection_over_union(self, detection2):
222+
def intersection_over_union(self, bbox2):
223223
"""
224224
Compute the ratio intersection/union (IoU) between this bounding-box and another detection (bounding-box or other shape).
225225
The IoU is 1 is the shape fully overlap (ie identical sizes and positions).
226226
It is 0 if they dont overlap.
227227
"""
228-
return self.intersection_area(detection2)/self.union_area(detection2)
228+
return self.intersection_area(bbox2)/self.union_area(bbox2)
229229

230230
def get_lists_xy(self):
231-
return self.exterior.xy
231+
return self.polygon.exterior.xy
232+
233+
def contains(self, bbox2):
234+
return self.polygon.contains(bbox2.polygon)
232235

236+
def overlaps(self, bbox2):
237+
return self.polygon.overlaps(bbox2.polygon)
238+
233239
@staticmethod
234240
def rescale_bounding_boxes(listDetectionsDownscaled, downscaling_factor):
235241
"""

0 commit comments

Comments
 (0)