Spaces:
Runtime error
Runtime error
Update utils/bubble_utils.py
Browse files- utils/bubble_utils.py +21 -11
utils/bubble_utils.py
CHANGED
|
@@ -33,29 +33,41 @@ def match_translations_to_bubbles(translations, bubble_polygons, min_overlap=0.1
|
|
| 33 |
return translations
|
| 34 |
|
| 35 |
# ======================== Debug Visualization ============================
|
| 36 |
-
|
| 37 |
-
|
| 38 |
"""
|
| 39 |
Draw:
|
| 40 |
- Bubble polygons (BLUE)
|
| 41 |
-
-
|
|
|
|
| 42 |
- Corrected polygons (GREEN)
|
| 43 |
- OCR centers (YELLOW)
|
| 44 |
-
Saves a PNG for inspection.
|
| 45 |
"""
|
| 46 |
debug = img.copy()
|
| 47 |
draw = ImageDraw.Draw(debug, "RGBA")
|
| 48 |
|
| 49 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
for bp in bubble_polygons:
|
| 51 |
if not bp:
|
| 52 |
continue
|
| 53 |
draw.polygon(bp, outline=(30, 144, 255, 200), width=4)
|
|
|
|
| 54 |
cx = int(np.mean([p[0] for p in bp]))
|
| 55 |
cy = int(np.mean([p[1] for p in bp]))
|
| 56 |
draw.ellipse((cx - 4, cy - 4, cx + 4, cy + 4), fill=(0, 255, 255, 220))
|
| 57 |
|
|
|
|
| 58 |
# OCR polygons
|
|
|
|
| 59 |
for t in translations:
|
| 60 |
orig = t.get("original_polygon")
|
| 61 |
corr = t.get("polygon")
|
|
@@ -67,8 +79,7 @@ def visualize_all_debug(img, translations, bubble_polygons, step_name="debug", p
|
|
| 67 |
|
| 68 |
poly_for_center = corr or orig
|
| 69 |
if poly_for_center:
|
| 70 |
-
|
| 71 |
-
cx, cy = int(center[0]), int(center[1])
|
| 72 |
draw.ellipse((cx - 3, cy - 3, cx + 3, cy + 3), fill=(255, 255, 0, 220))
|
| 73 |
|
| 74 |
out_path = f"{prefix}_{step_name}.png"
|
|
@@ -76,7 +87,6 @@ def visualize_all_debug(img, translations, bubble_polygons, step_name="debug", p
|
|
| 76 |
print(f"π Saved debug visualization β {out_path}")
|
| 77 |
return out_path
|
| 78 |
|
| 79 |
-
|
| 80 |
# ===================== Main Bubble Translation Pipeline ===================
|
| 81 |
def bubble_pipeline_single(file_obj, num_chunks=1, polygon_strategy="hybrid", debug=True):
|
| 82 |
"""
|
|
@@ -98,12 +108,12 @@ def bubble_pipeline_single(file_obj, num_chunks=1, polygon_strategy="hybrid", de
|
|
| 98 |
# -------------------------------------------------------
|
| 99 |
# 2. Detect rectangles + refine shapes
|
| 100 |
# -------------------------------------------------------
|
| 101 |
-
bubble_polygons, interior_polygons = detect_and_refine_bubbles(full_img)
|
| 102 |
print(f"π Found {len(bubble_polygons)} refined bubbles")
|
| 103 |
|
| 104 |
if debug:
|
| 105 |
p = visualize_all_debug(
|
| 106 |
-
full_img, [], bubble_polygons,
|
| 107 |
step_name="bubbles_only", prefix="bubble_dbg"
|
| 108 |
)
|
| 109 |
debug_files.append(p)
|
|
@@ -136,7 +146,7 @@ def bubble_pipeline_single(file_obj, num_chunks=1, polygon_strategy="hybrid", de
|
|
| 136 |
|
| 137 |
if debug:
|
| 138 |
p = visualize_all_debug(
|
| 139 |
-
full_img, translations, bubble_polygons,
|
| 140 |
step_name="after_correction", prefix="bubble_dbg"
|
| 141 |
)
|
| 142 |
debug_files.append(p)
|
|
|
|
| 33 |
return translations
|
| 34 |
|
| 35 |
# ======================== Debug Visualization ============================
|
| 36 |
+
def visualize_all_debug(img, translations, bubble_polygons, bubble_boxes=None,
|
| 37 |
+
step_name="debug", prefix="debug"):
|
| 38 |
"""
|
| 39 |
Draw:
|
| 40 |
- Bubble polygons (BLUE)
|
| 41 |
+
- Bounding boxes (ORANGE)
|
| 42 |
+
- OCR polygons (RED)
|
| 43 |
- Corrected polygons (GREEN)
|
| 44 |
- OCR centers (YELLOW)
|
|
|
|
| 45 |
"""
|
| 46 |
debug = img.copy()
|
| 47 |
draw = ImageDraw.Draw(debug, "RGBA")
|
| 48 |
|
| 49 |
+
# -------------------------------
|
| 50 |
+
# Draw Bubble bounding boxes
|
| 51 |
+
# -------------------------------
|
| 52 |
+
if bubble_boxes:
|
| 53 |
+
for (x1, y1, x2, y2) in bubble_boxes:
|
| 54 |
+
draw.rectangle((x1, y1, x2, y2), outline=(255, 165, 0, 180), width=3)
|
| 55 |
+
|
| 56 |
+
# -------------------------------
|
| 57 |
+
# Draw Bubble polygons
|
| 58 |
+
# -------------------------------
|
| 59 |
for bp in bubble_polygons:
|
| 60 |
if not bp:
|
| 61 |
continue
|
| 62 |
draw.polygon(bp, outline=(30, 144, 255, 200), width=4)
|
| 63 |
+
|
| 64 |
cx = int(np.mean([p[0] for p in bp]))
|
| 65 |
cy = int(np.mean([p[1] for p in bp]))
|
| 66 |
draw.ellipse((cx - 4, cy - 4, cx + 4, cy + 4), fill=(0, 255, 255, 220))
|
| 67 |
|
| 68 |
+
# -------------------------------
|
| 69 |
# OCR polygons
|
| 70 |
+
# -------------------------------
|
| 71 |
for t in translations:
|
| 72 |
orig = t.get("original_polygon")
|
| 73 |
corr = t.get("polygon")
|
|
|
|
| 79 |
|
| 80 |
poly_for_center = corr or orig
|
| 81 |
if poly_for_center:
|
| 82 |
+
cx, cy = np.mean(poly_for_center, axis=0)
|
|
|
|
| 83 |
draw.ellipse((cx - 3, cy - 3, cx + 3, cy + 3), fill=(255, 255, 0, 220))
|
| 84 |
|
| 85 |
out_path = f"{prefix}_{step_name}.png"
|
|
|
|
| 87 |
print(f"π Saved debug visualization β {out_path}")
|
| 88 |
return out_path
|
| 89 |
|
|
|
|
| 90 |
# ===================== Main Bubble Translation Pipeline ===================
|
| 91 |
def bubble_pipeline_single(file_obj, num_chunks=1, polygon_strategy="hybrid", debug=True):
|
| 92 |
"""
|
|
|
|
| 108 |
# -------------------------------------------------------
|
| 109 |
# 2. Detect rectangles + refine shapes
|
| 110 |
# -------------------------------------------------------
|
| 111 |
+
bubble_polygons, interior_polygons, bubble_boxes = detect_and_refine_bubbles(full_img)
|
| 112 |
print(f"π Found {len(bubble_polygons)} refined bubbles")
|
| 113 |
|
| 114 |
if debug:
|
| 115 |
p = visualize_all_debug(
|
| 116 |
+
full_img, [], bubble_polygons, bubble_boxes=bubble_boxes,
|
| 117 |
step_name="bubbles_only", prefix="bubble_dbg"
|
| 118 |
)
|
| 119 |
debug_files.append(p)
|
|
|
|
| 146 |
|
| 147 |
if debug:
|
| 148 |
p = visualize_all_debug(
|
| 149 |
+
full_img, translations, bubble_polygons, bubble_boxes=bubble_boxes,
|
| 150 |
step_name="after_correction", prefix="bubble_dbg"
|
| 151 |
)
|
| 152 |
debug_files.append(p)
|