jebin2 commited on
Commit
e3f1283
·
1 Parent(s): 79125e3
comic_panel_extractor/panel_extractor.py CHANGED
@@ -6,6 +6,7 @@ import cv2
6
  from dataclasses import dataclass
7
  import os
8
  import re
 
9
 
10
  @dataclass
11
  class PanelData:
@@ -247,22 +248,14 @@ class PanelExtractor:
247
  coords.append(tuple(map(int, match.groups())))
248
  return coords
249
 
250
- def is_fully_contained(self, x1: int, y1: int, x2: int, y2: int,
251
- boxes: List[Tuple[int, int, int, int]],
252
- threshold: int = 200) -> bool:
253
- for ex1, ey1, ex2, ey2 in boxes:
254
- # Case 1: Fully contained
255
- if x1 >= ex1 and y1 >= ey1 and x2 <= ex2 and y2 <= ey2:
256
- return True
257
 
258
- # Case 2: Near containment (within threshold)
259
- if (
260
- x1 >= ex1 - threshold and y1 >= ey1 - threshold and
261
- x2 <= ex2 + threshold and y2 <= ey2 + threshold
262
- ):
263
- return True
264
-
265
- return False
266
 
267
  def _save_panels(self, panels: List[Tuple[int, int, int, int]], original: np.ndarray, width: int, height: int) -> Tuple[List[np.ndarray], List[PanelData], List[str]]:
268
  """Save panel images and return panel data."""
@@ -310,10 +303,18 @@ class PanelExtractor:
310
  # Check for full containment in existing and current session
311
  already_saved_coords = existing_coords + [ (pd.x_start, pd.y_start, pd.x_end, pd.y_end) for pd in panel_data ]
312
 
313
- if self.is_fully_contained(x1, y1, x2, y2, already_saved_coords):
 
 
314
  print(f"⚠️ Skipping panel #{idx} — fully contained in existing panel")
315
  continue
316
 
 
 
 
 
 
 
317
  # Save panel
318
  panel_img = visual_output[y1:y2, x1:x2]
319
  panel_images.append(panel_img)
 
6
  from dataclasses import dataclass
7
  import os
8
  import re
9
+ from .utils import remove_duplicate_boxes
10
 
11
  @dataclass
12
  class PanelData:
 
248
  coords.append(tuple(map(int, match.groups())))
249
  return coords
250
 
251
+ def count_panels_inside(self, target_box, other_boxes):
252
+ x1a, y1a, x2a, y2a = target_box
253
+ count = 0
254
+ for x1b, y1b, x2b, y2b in other_boxes:
255
+ if x1a <= x1b and y1a <= y1b and x2a >= x2b and y2a >= y2b:
256
+ count += 1
257
+ return count
258
 
 
 
 
 
 
 
 
 
259
 
260
  def _save_panels(self, panels: List[Tuple[int, int, int, int]], original: np.ndarray, width: int, height: int) -> Tuple[List[np.ndarray], List[PanelData], List[str]]:
261
  """Save panel images and return panel data."""
 
303
  # Check for full containment in existing and current session
304
  already_saved_coords = existing_coords + [ (pd.x_start, pd.y_start, pd.x_end, pd.y_end) for pd in panel_data ]
305
 
306
+ # 1. Skip if duplicate
307
+ is_duplicate, _ = remove_duplicate_boxes(already_saved_coords, (x1, y1, x2, y2))
308
+ if is_duplicate:
309
  print(f"⚠️ Skipping panel #{idx} — fully contained in existing panel")
310
  continue
311
 
312
+ # 2. Skip if this panel contains ≥1 other panels
313
+ contained_count = self.count_panels_inside((x1, y1, x2, y2), already_saved_coords)
314
+ if contained_count >= 1:
315
+ print(f"⚠️ Skipping panel #{idx} — contains {contained_count} other panels inside")
316
+ continue
317
+
318
  # Save panel
319
  panel_img = visual_output[y1:y2, x1:x2]
320
  panel_images.append(panel_img)