Update utils.py
Browse files
utils.py
CHANGED
|
@@ -238,28 +238,22 @@ def intersects(rectA, rectB):
|
|
| 238 |
def is_there_a_directed_edge(a, b, rects):
|
| 239 |
rectA = rects[a]
|
| 240 |
rectB = rects[b]
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
if
|
| 244 |
-
return
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
return use_cuts_to_determine_edge_from_a_to_b(a, b, rects)
|
| 258 |
-
if is_strictly_below(copy_B, copy_A) and is_strictly_right_of(copy_B, copy_A):
|
| 259 |
-
return use_cuts_to_determine_edge_from_a_to_b(a, b, rects)
|
| 260 |
-
# otherwise they intersect
|
| 261 |
-
copy_A = erode_rectangle(copy_A, 0.05)
|
| 262 |
-
copy_B = erode_rectangle(copy_B, 0.05)
|
| 263 |
|
| 264 |
def get_distance(rectA, rectB):
|
| 265 |
return box(rectA[0], rectA[1], rectA[2], rectA[3]).distance(box(rectB[0], rectB[1], rectB[2], rectB[3]))
|
|
@@ -377,23 +371,10 @@ def get_text_to_panel_mapping(text_bboxes, sorted_panel_bboxes):
|
|
| 377 |
return text_to_panel_mapping
|
| 378 |
|
| 379 |
def sort_texts_within_panel(rects):
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
smallest_y = min(smallest_y, y1)
|
| 385 |
-
greatest_x = max(greatest_x, x2)
|
| 386 |
-
|
| 387 |
-
reference_point = Point(greatest_x, smallest_y)
|
| 388 |
-
|
| 389 |
-
polygons_and_index = []
|
| 390 |
-
for i, rect in enumerate(rects):
|
| 391 |
-
x1, y1, x2, y2 = rect
|
| 392 |
-
polygons_and_index.append((box(x1,y1,x2,y2), i))
|
| 393 |
-
# sort points by closest to reference point
|
| 394 |
-
polygons_and_index = sorted(polygons_and_index, key=lambda x: reference_point.distance(x[0]))
|
| 395 |
-
indices = [x[1] for x in polygons_and_index]
|
| 396 |
-
return indices
|
| 397 |
|
| 398 |
def x1y1wh_to_x1y1x2y2(bbox):
|
| 399 |
x1, y1, w, h = bbox
|
|
|
|
| 238 |
def is_there_a_directed_edge(a, b, rects):
|
| 239 |
rectA = rects[a]
|
| 240 |
rectB = rects[b]
|
| 241 |
+
|
| 242 |
+
# Check if rectA is to the left of rectB
|
| 243 |
+
if is_strictly_left_of(rectA, rectB):
|
| 244 |
+
return True
|
| 245 |
+
elif is_strictly_left_of(rectB, rectA):
|
| 246 |
+
return False
|
| 247 |
+
|
| 248 |
+
# If overlapping on the x-axis, we'll perhaps rely on top-to-bottom
|
| 249 |
+
if is_strictly_above(rectA, rectB):
|
| 250 |
+
return True
|
| 251 |
+
elif is_strictly_below(rectA, rectB):
|
| 252 |
+
return False
|
| 253 |
+
|
| 254 |
+
# If none of these strict conditions is met,
|
| 255 |
+
# fall back to checking if one intersects the other and using size (this may rarely happen)
|
| 256 |
+
return box(*rectA).area > box(*rectB).area
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
def get_distance(rectA, rectB):
|
| 259 |
return box(rectA[0], rectA[1], rectA[2], rectA[3]).distance(box(rectB[0], rectB[1], rectB[2], rectB[3]))
|
|
|
|
| 371 |
return text_to_panel_mapping
|
| 372 |
|
| 373 |
def sort_texts_within_panel(rects):
|
| 374 |
+
# Sort primarily by the x-coordinate (left to right), then by y-coordinate if needed
|
| 375 |
+
rects_with_index = [(i, rect) for i, rect in enumerate(rects)]
|
| 376 |
+
sorted_rects = sorted(rects_with_index, key=lambda x: (x[1][0], x[1][1]))
|
| 377 |
+
return [x[0] for x in sorted_rects]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
def x1y1wh_to_x1y1x2y2(bbox):
|
| 380 |
x1, y1, w, h = bbox
|