Spaces:
Runtime error
Runtime error
Update utils/bubble_utils.py
Browse files- utils/bubble_utils.py +79 -33
utils/bubble_utils.py
CHANGED
|
@@ -33,58 +33,104 @@ def match_translations_to_bubbles(translations, bubble_polygons, min_overlap=0.1
|
|
| 33 |
return translations
|
| 34 |
|
| 35 |
# ======================== Debug Visualization ============================
|
| 36 |
-
def visualize_all_debug(
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
-
|
| 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 |
-
#
|
| 51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if bubble_boxes:
|
| 53 |
-
for
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
|
| 65 |
-
|
| 66 |
-
draw.ellipse((cx - 4, cy - 4, cx + 4, cy + 4), fill=(0, 255, 255, 220))
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
for t in translations:
|
| 72 |
orig = t.get("original_polygon")
|
| 73 |
corr = t.get("polygon")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
poly_for_center = corr or orig
|
| 81 |
if poly_for_center:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
out_path = f"{prefix}_{step_name}.png"
|
| 86 |
debug.save(out_path)
|
| 87 |
print(f"📌 Saved debug visualization → {out_path}")
|
|
|
|
| 88 |
return out_path
|
| 89 |
|
| 90 |
# ===================== Main Bubble Translation Pipeline ===================
|
|
|
|
| 33 |
return translations
|
| 34 |
|
| 35 |
# ======================== Debug Visualization ============================
|
| 36 |
+
def visualize_all_debug(
|
| 37 |
+
img, translations, bubble_polygons, bubble_boxes=None,
|
| 38 |
+
step_name="debug", prefix="debug"
|
| 39 |
+
):
|
| 40 |
"""
|
| 41 |
+
Robust debug visualization.
|
| 42 |
+
Handles malformed polygons, empty lists, None values, and degenerate shapes.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
+
|
| 45 |
+
import numpy as np
|
| 46 |
+
from PIL import ImageDraw
|
| 47 |
+
|
| 48 |
debug = img.copy()
|
| 49 |
draw = ImageDraw.Draw(debug, "RGBA")
|
| 50 |
|
| 51 |
+
# ==========================
|
| 52 |
+
# Helper: validate polygon
|
| 53 |
+
# ==========================
|
| 54 |
+
def valid_poly(poly):
|
| 55 |
+
if not poly or len(poly) < 3:
|
| 56 |
+
return False
|
| 57 |
+
# Filter invalid coords
|
| 58 |
+
cleaned = [(int(x), int(y)) for x, y in poly if isinstance(x, (int, float))]
|
| 59 |
+
# Must have >= 3 *distinct* points
|
| 60 |
+
return len(set(cleaned)) >= 3
|
| 61 |
+
|
| 62 |
+
# ==========================
|
| 63 |
+
# Draw bounding boxes (ORANGE)
|
| 64 |
+
# ==========================
|
| 65 |
if bubble_boxes:
|
| 66 |
+
for box in bubble_boxes:
|
| 67 |
+
try:
|
| 68 |
+
x1, y1, x2, y2 = map(int, box)
|
| 69 |
+
draw.rectangle((x1, y1, x2, y2), outline=(255,165,0,180), width=3)
|
| 70 |
+
except Exception:
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
# ==========================
|
| 74 |
+
# Draw bubble polygons (BLUE)
|
| 75 |
+
# ==========================
|
| 76 |
for bp in bubble_polygons:
|
| 77 |
+
if not valid_poly(bp):
|
| 78 |
continue
|
|
|
|
| 79 |
|
| 80 |
+
try:
|
| 81 |
+
draw.polygon(bp, outline=(30,144,255,200), width=4)
|
|
|
|
| 82 |
|
| 83 |
+
xs = [p[0] for p in bp]
|
| 84 |
+
ys = [p[1] for p in bp]
|
| 85 |
+
cx, cy = int(np.mean(xs)), int(np.mean(ys))
|
| 86 |
+
draw.ellipse((cx-4, cy-4, cx+4, cy+4), fill=(0,255,255,220))
|
| 87 |
+
except Exception:
|
| 88 |
+
continue
|
| 89 |
+
|
| 90 |
+
# ==========================
|
| 91 |
+
# Draw OCR polygons
|
| 92 |
+
# ==========================
|
| 93 |
for t in translations:
|
| 94 |
orig = t.get("original_polygon")
|
| 95 |
corr = t.get("polygon")
|
| 96 |
|
| 97 |
+
# Draw original polygon (RED)
|
| 98 |
+
if valid_poly(orig):
|
| 99 |
+
try:
|
| 100 |
+
draw.polygon(orig, outline=(255,50,50,180), width=3)
|
| 101 |
+
except Exception:
|
| 102 |
+
pass
|
| 103 |
+
|
| 104 |
+
# Draw corrected polygon (GREEN)
|
| 105 |
+
if valid_poly(corr):
|
| 106 |
+
try:
|
| 107 |
+
draw.polygon(corr, outline=(50,255,50,220), width=3)
|
| 108 |
+
except Exception:
|
| 109 |
+
pass
|
| 110 |
+
|
| 111 |
+
# Center point (YELLOW)
|
| 112 |
+
poly_for_center = None
|
| 113 |
+
if valid_poly(corr):
|
| 114 |
+
poly_for_center = corr
|
| 115 |
+
elif valid_poly(orig):
|
| 116 |
+
poly_for_center = orig
|
| 117 |
|
|
|
|
| 118 |
if poly_for_center:
|
| 119 |
+
try:
|
| 120 |
+
xs = [p[0] for p in poly_for_center]
|
| 121 |
+
ys = [p[1] for p in poly_for_center]
|
| 122 |
+
cx, cy = int(np.mean(xs)), int(np.mean(ys))
|
| 123 |
+
draw.ellipse((cx-3, cy-3, cx+3, cy+3), fill=(255,255,0,220))
|
| 124 |
+
except Exception:
|
| 125 |
+
pass
|
| 126 |
+
|
| 127 |
+
# ==========================
|
| 128 |
+
# Save output
|
| 129 |
+
# ==========================
|
| 130 |
out_path = f"{prefix}_{step_name}.png"
|
| 131 |
debug.save(out_path)
|
| 132 |
print(f"📌 Saved debug visualization → {out_path}")
|
| 133 |
+
|
| 134 |
return out_path
|
| 135 |
|
| 136 |
# ===================== Main Bubble Translation Pipeline ===================
|