NicFromLM commited on
Commit
e18fda5
·
verified ·
1 Parent(s): 81787a2

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +20 -39
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
- 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,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
- 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
 
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