Upload 6 files
Browse files
utils.py
CHANGED
|
@@ -238,22 +238,28 @@ 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 |
|
| 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,10 +377,23 @@ def get_text_to_panel_mapping(text_bboxes, sorted_panel_bboxes):
|
|
| 371 |
return text_to_panel_mapping
|
| 372 |
|
| 373 |
def sort_texts_within_panel(rects):
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
def x1y1wh_to_x1y1x2y2(bbox):
|
| 380 |
x1, y1, w, h = bbox
|
|
|
|
| 238 |
def is_there_a_directed_edge(a, b, rects):
|
| 239 |
rectA = rects[a]
|
| 240 |
rectB = rects[b]
|
| 241 |
+
centre_of_A = [rectA[0] + (rectA[2] - rectA[0]) / 2, rectA[1] + (rectA[3] - rectA[1]) / 2]
|
| 242 |
+
centre_of_B = [rectB[0] + (rectB[2] - rectB[0]) / 2, rectB[1] + (rectB[3] - rectB[1]) / 2]
|
| 243 |
+
if np.allclose(np.array(centre_of_A), np.array(centre_of_B)):
|
| 244 |
+
return box(*rectA).area > (box(*rectB)).area
|
| 245 |
+
copy_A = [rectA[0], rectA[1], rectA[2], rectA[3]]
|
| 246 |
+
copy_B = [rectB[0], rectB[1], rectB[2], rectB[3]]
|
| 247 |
+
while True:
|
| 248 |
+
if is_strictly_above(copy_A, copy_B) and not is_strictly_left_of(copy_A, copy_B):
|
| 249 |
+
return 1
|
| 250 |
+
if is_strictly_above(copy_B, copy_A) and not is_strictly_left_of(copy_B, copy_A):
|
| 251 |
+
return 0
|
| 252 |
+
if is_strictly_right_of(copy_A, copy_B) and not is_strictly_below(copy_A, copy_B):
|
| 253 |
+
return 1
|
| 254 |
+
if is_strictly_right_of(copy_B, copy_A) and not is_strictly_below(copy_B, copy_A):
|
| 255 |
+
return 0
|
| 256 |
+
if is_strictly_below(copy_A, copy_B) and is_strictly_right_of(copy_A, copy_B):
|
| 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 |
return text_to_panel_mapping
|
| 378 |
|
| 379 |
def sort_texts_within_panel(rects):
|
| 380 |
+
smallest_y = float("inf")
|
| 381 |
+
greatest_x = float("-inf")
|
| 382 |
+
for i, rect in enumerate(rects):
|
| 383 |
+
x1, y1, x2, y2 = rect
|
| 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
|