fix box merging logic

This commit is contained in:
Josh Hawkins 2025-04-07 14:52:48 -05:00
parent 1904e3c3f9
commit 6b6f4477da

View File

@ -309,7 +309,11 @@ class LicensePlateProcessingMixin:
return image.transpose((2, 0, 1))[np.newaxis, ...] return image.transpose((2, 0, 1))[np.newaxis, ...]
def _merge_nearby_boxes( def _merge_nearby_boxes(
self, boxes: List[np.ndarray], plate_width: float, gap_fraction: float = 0.1 self,
boxes: List[np.ndarray],
plate_width: float,
gap_fraction: float = 0.1,
min_overlap_fraction: float = -0.2,
) -> List[np.ndarray]: ) -> List[np.ndarray]:
""" """
Merge bounding boxes that are likely part of the same license plate based on proximity, Merge bounding boxes that are likely part of the same license plate based on proximity,
@ -329,6 +333,7 @@ class LicensePlateProcessingMixin:
return [] return []
max_gap = plate_width * gap_fraction max_gap = plate_width * gap_fraction
min_overlap = plate_width * min_overlap_fraction
# Sort boxes by top left x # Sort boxes by top left x
sorted_boxes = sorted(boxes, key=lambda x: x[0][0]) sorted_boxes = sorted(boxes, key=lambda x: x[0][0])
@ -353,9 +358,10 @@ class LicensePlateProcessingMixin:
next_bottom = np.max(next_box[:, 1]) next_bottom = np.max(next_box[:, 1])
# Consider boxes part of the same plate if they are close horizontally or overlap # Consider boxes part of the same plate if they are close horizontally or overlap
if horizontal_gap <= max_gap and max(current_top, next_top) <= min( # within the allowed limit and their vertical positions overlap significantly
current_bottom, next_bottom if min_overlap <= horizontal_gap <= max_gap and max(
): current_top, next_top
) <= min(current_bottom, next_bottom):
merged_points = np.vstack((current_box, next_box)) merged_points = np.vstack((current_box, next_box))
new_box = np.array( new_box = np.array(
[ [
@ -379,7 +385,7 @@ class LicensePlateProcessingMixin:
) )
current_box = new_box current_box = new_box
else: else:
# If the boxes are not close enough, add the current box to the result # If the boxes are not close enough or overlap too much, add the current box to the result
merged_boxes.append(current_box) merged_boxes.append(current_box)
current_box = next_box current_box = next_box