Spaces:
Running
Running
Update utils/bubble_detect_rtdetr.py
Browse files- utils/bubble_detect_rtdetr.py +22 -14
utils/bubble_detect_rtdetr.py
CHANGED
|
@@ -7,6 +7,7 @@ import cv2
|
|
| 7 |
from shapely.geometry import Polygon
|
| 8 |
from shapely.ops import unary_union
|
| 9 |
from transformers import AutoImageProcessor, RTDetrForObjectDetection
|
|
|
|
| 10 |
|
| 11 |
MODEL_NAME = "ogkalu/comic-text-and-bubble-detector"
|
| 12 |
|
|
@@ -217,6 +218,7 @@ def refine_bubble_from_bbox(image_pil, bbox):
|
|
| 217 |
def detect_and_refine_bubbles(full_img, conf_threshold=0.30):
|
| 218 |
detections = detect_bubbles_rtdetr(full_img, conf_threshold)
|
| 219 |
|
|
|
|
| 220 |
bubble_boxes = [d["bbox"] for d in detections if d["class"] == 0]
|
| 221 |
|
| 222 |
bubble_polygons = []
|
|
@@ -225,30 +227,36 @@ def detect_and_refine_bubbles(full_img, conf_threshold=0.30):
|
|
| 225 |
for i, bbox in enumerate(bubble_boxes):
|
| 226 |
outer, inner = refine_bubble_from_bbox(full_img, bbox)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
x1, y1, x2, y2 = map(int, bbox)
|
| 231 |
-
outer = [(x1,y1),(x2,y1),(x2,y2),(x1,y2)]
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
inner = outer
|
| 236 |
|
| 237 |
bubble_polygons.append(outer)
|
| 238 |
interior_polygons.append(inner)
|
| 239 |
|
| 240 |
-
print("β¨ RT-DETR refined bubbles:
|
| 241 |
|
| 242 |
-
#
|
| 243 |
-
for i,
|
| 244 |
-
if
|
| 245 |
-
print(f"β interior[{i}] INVALID")
|
| 246 |
else:
|
| 247 |
-
print(f"β interior[{i}] OK ({len(
|
| 248 |
|
| 249 |
return bubble_polygons, interior_polygons, bubble_boxes
|
| 250 |
-
|
| 251 |
-
|
| 252 |
# ------------------------------------------------------------
|
| 253 |
# Polygon β mask
|
| 254 |
# ------------------------------------------------------------
|
|
|
|
| 7 |
from shapely.geometry import Polygon
|
| 8 |
from shapely.ops import unary_union
|
| 9 |
from transformers import AutoImageProcessor, RTDetrForObjectDetection
|
| 10 |
+
from utils.polygon_utils import sanitize_polygon
|
| 11 |
|
| 12 |
MODEL_NAME = "ogkalu/comic-text-and-bubble-detector"
|
| 13 |
|
|
|
|
| 218 |
def detect_and_refine_bubbles(full_img, conf_threshold=0.30):
|
| 219 |
detections = detect_bubbles_rtdetr(full_img, conf_threshold)
|
| 220 |
|
| 221 |
+
# raw boxes from RT-DETR
|
| 222 |
bubble_boxes = [d["bbox"] for d in detections if d["class"] == 0]
|
| 223 |
|
| 224 |
bubble_polygons = []
|
|
|
|
| 227 |
for i, bbox in enumerate(bubble_boxes):
|
| 228 |
outer, inner = refine_bubble_from_bbox(full_img, bbox)
|
| 229 |
|
| 230 |
+
# -----------------------------
|
| 231 |
+
# Sanitize outer polygon
|
| 232 |
+
# -----------------------------
|
| 233 |
+
outer = sanitize_polygon(outer)
|
| 234 |
+
if outer is None:
|
| 235 |
+
print(f"β οΈ Bubble {i}: outer invalid β fallback to rectangle")
|
| 236 |
x1, y1, x2, y2 = map(int, bbox)
|
| 237 |
+
outer = [(x1,y1), (x2,y1), (x2,y2), (x1,y2)]
|
| 238 |
+
|
| 239 |
+
# -----------------------------
|
| 240 |
+
# Sanitize inner polygon
|
| 241 |
+
# -----------------------------
|
| 242 |
+
inner = sanitize_polygon(inner)
|
| 243 |
+
if inner is None:
|
| 244 |
+
print(f"β οΈ Bubble {i}: inner invalid β using outer")
|
| 245 |
inner = outer
|
| 246 |
|
| 247 |
bubble_polygons.append(outer)
|
| 248 |
interior_polygons.append(inner)
|
| 249 |
|
| 250 |
+
print(f"β¨ RT-DETR refined bubbles: {len(bubble_polygons)}")
|
| 251 |
|
| 252 |
+
# Debug summary
|
| 253 |
+
for i, poly in enumerate(interior_polygons):
|
| 254 |
+
if poly is None or len(poly) < 4:
|
| 255 |
+
print(f"β interior[{i}] INVALID ({poly})")
|
| 256 |
else:
|
| 257 |
+
print(f"β interior[{i}] OK ({len(poly)} pts)")
|
| 258 |
|
| 259 |
return bubble_polygons, interior_polygons, bubble_boxes
|
|
|
|
|
|
|
| 260 |
# ------------------------------------------------------------
|
| 261 |
# Polygon β mask
|
| 262 |
# ------------------------------------------------------------
|