qqwjq1981 commited on
Commit
6a100df
Β·
verified Β·
1 Parent(s): e63ba27

Update utils/bubble_detect_rtdetr.py

Browse files
Files changed (1) hide show
  1. 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
- if outer is None or len(outer) == 0:
229
- print(f"⚠️ Bubble {i}: outer empty β†’ fallback rect")
 
 
 
 
230
  x1, y1, x2, y2 = map(int, bbox)
231
- outer = [(x1,y1),(x2,y1),(x2,y2),(x1,y2)]
232
-
233
- if inner is None or len(inner) == 0:
234
- print(f"⚠️ Bubble {i}: inner empty β†’ using outer")
 
 
 
 
235
  inner = outer
236
 
237
  bubble_polygons.append(outer)
238
  interior_polygons.append(inner)
239
 
240
- print("✨ RT-DETR refined bubbles:", len(bubble_polygons))
241
 
242
- # Extra debugging
243
- for i, p in enumerate(interior_polygons):
244
- if p is None or len(p) == 0:
245
- print(f"❌ interior[{i}] INVALID")
246
  else:
247
- print(f"βœ“ interior[{i}] OK ({len(p)} pts)")
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
  # ------------------------------------------------------------