qqwjq1981 commited on
Commit
9b8e87c
·
verified ·
1 Parent(s): 2c2f517

Update utils/bubble_utils.py

Browse files
Files changed (1) hide show
  1. utils/bubble_utils.py +44 -4
utils/bubble_utils.py CHANGED
@@ -17,9 +17,49 @@ from utils.bubble_detect import detect_speech_bubbles_robust
17
  from utils.u2net_detector import detect_bubbles_u2net
18
  from utils.bubble_detect_rtdetr import detect_and_refine_bubbles, polygon_to_mask
19
 
20
- def rect_to_poly(box):
21
- x1, y1, x2, y2 = map(int, box)
22
- return [(x1,y1), (x2,y1), (x2,y2), (x1,y2)]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def match_translations_to_bubbles(translations, bubble_polygons, min_overlap=0.10):
25
  """
@@ -173,7 +213,7 @@ def bubble_pipeline_single(file_obj, num_chunks=1, polygon_strategy="hybrid", de
173
  # 3. MASK-BASED OCR (replaces standard OCR)
174
  # -------------------------------------------------------
175
  print("📝 Running masked OCR inside bubble interiors...")
176
- translations = extract_and_translate_with_masks(full_img, rect_to_poly(bubble_boxes))
177
 
178
  if not translations:
179
  print("⚠️ Masked OCR found no text → fallback to full OCR")
 
17
  from utils.u2net_detector import detect_bubbles_u2net
18
  from utils.bubble_detect_rtdetr import detect_and_refine_bubbles, polygon_to_mask
19
 
20
+ def normalize_bubble_regions(bubble_boxes):
21
+ """
22
+ Accepts:
23
+ - list of rectangles
24
+ - list of polygons
25
+ - mixture of both
26
+
27
+ Returns a list of valid polygons.
28
+ """
29
+
30
+ def rect_to_poly(box):
31
+ # Accept tuple OR list length = 4
32
+ if (
33
+ isinstance(box, (list, tuple)) and
34
+ len(box) == 4 and
35
+ all(isinstance(v, (int, float)) for v in box)
36
+ ):
37
+ x1, y1, x2, y2 = map(int, box)
38
+ return [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
39
+ return None # not a rectangle
40
+
41
+ polygons = []
42
+
43
+ for region in bubble_boxes:
44
+
45
+ # Case 1 — region is a rectangle
46
+ poly = rect_to_poly(region)
47
+ if poly is not None:
48
+ polygons.append(poly)
49
+ continue
50
+
51
+ # Case 2 — polygon
52
+ if (
53
+ isinstance(region, (list, tuple)) and
54
+ len(region) >= 3 and
55
+ all(len(pt) == 2 for pt in region)
56
+ ):
57
+ polygons.append([(int(x), int(y)) for x, y in region])
58
+ continue
59
+
60
+ print(f"⚠️ Skipping invalid bubble box: {region}")
61
+
62
+ return polygons
63
 
64
  def match_translations_to_bubbles(translations, bubble_polygons, min_overlap=0.10):
65
  """
 
213
  # 3. MASK-BASED OCR (replaces standard OCR)
214
  # -------------------------------------------------------
215
  print("📝 Running masked OCR inside bubble interiors...")
216
+ translations = extract_and_translate_with_masks(full_img, normalize_bubble_regions(bubble_boxes))
217
 
218
  if not translations:
219
  print("⚠️ Masked OCR found no text → fallback to full OCR")