jebin2 commited on
Commit
e3f9b7d
·
1 Parent(s): 1dde7c9

added thres

Browse files
comic_panel_extractor/llm_panel_extractor.py CHANGED
@@ -235,9 +235,11 @@ def extract_panel_via_llm(input_image_path, config=None, reset=True):
235
  all_path = [file for file in os.listdir(extractor_config.output_folder) if "_panel_" in file]
236
  remain_boxes = utils.get_remaining_areas((original_width, original_height), all_processed_boxes)
237
  if remain_boxes:
238
- panel_extractor.crop_and_save_detected_panels(remain_boxes)
239
- all_processed_boxes.extend(remain_boxes)
240
- accumulated_detected_boxes.extend(remain_boxes)
 
 
241
 
242
  print(f"Processing complete. Final result saved to: {extractor_config.output_folder}")
243
  print(f"Total panels detected: {len(all_path)}")
 
235
  all_path = [file for file in os.listdir(extractor_config.output_folder) if "_panel_" in file]
236
  remain_boxes = utils.get_remaining_areas((original_width, original_height), all_processed_boxes)
237
  if remain_boxes:
238
+ remain_boxes = utils.is_valid_panel((original_width, original_height), remain_boxes, extractor_config.min_width_ratio, extractor_config.min_height_ratio)
239
+ if remain_boxes:
240
+ panel_extractor.crop_and_save_detected_panels(remain_boxes)
241
+ all_processed_boxes.extend(remain_boxes)
242
+ accumulated_detected_boxes.extend(remain_boxes)
243
 
244
  print(f"Processing complete. Final result saved to: {extractor_config.output_folder}")
245
  print(f"Total panels detected: {len(all_path)}")
comic_panel_extractor/utils.py CHANGED
@@ -413,34 +413,66 @@ def find_similar_remaining_regions(boxes, image_shape, debug_image_path, w_t=0.2
413
  return similar_boxes
414
 
415
  def get_remaining_areas(image_size, boxes):
416
- """
417
- Given the image size and a list of bounding boxes, returns the remaining uncovered areas
418
- as rectangles.
419
 
420
- Args:
421
- image_size: (width, height) of the image.
422
- boxes: List of (x1, y1, x2, y2) rectangles.
423
 
424
- Returns:
425
- List of rectangles representing the remaining uncovered areas.
426
- """
427
- width, height = image_size
428
- # Create a binary mask of the image (0 = uncovered, 255 = covered)
429
- mask = np.zeros((height, width), dtype=np.uint8)
430
 
431
- # Mark the covered boxes
432
- for x1, y1, x2, y2 in boxes:
433
- mask[y1:y2, x1:x2] = 255
434
 
435
- # Invert mask to get the remaining area
436
- remaining_mask = cv2.bitwise_not(mask)
437
 
438
- # Find contours in the remaining area
439
- contours, _ = cv2.findContours(remaining_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
 
441
- remaining_boxes = []
442
- for contour in contours:
443
- x, y, w, h = cv2.boundingRect(contour)
444
- remaining_boxes.append((x, y, x + w, y + h))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445
 
446
- return remaining_boxes
 
413
  return similar_boxes
414
 
415
  def get_remaining_areas(image_size, boxes):
416
+ """
417
+ Given the image size and a list of bounding boxes, returns the remaining uncovered areas
418
+ as rectangles.
419
 
420
+ Args:
421
+ image_size: (width, height) of the image.
422
+ boxes: List of (x1, y1, x2, y2) rectangles.
423
 
424
+ Returns:
425
+ List of rectangles representing the remaining uncovered areas.
426
+ """
427
+ width, height = image_size
428
+ # Create a binary mask of the image (0 = uncovered, 255 = covered)
429
+ mask = np.zeros((height, width), dtype=np.uint8)
430
 
431
+ # Mark the covered boxes
432
+ for x1, y1, x2, y2 in boxes:
433
+ mask[y1:y2, x1:x2] = 255
434
 
435
+ # Invert mask to get the remaining area
436
+ remaining_mask = cv2.bitwise_not(mask)
437
 
438
+ # Find contours in the remaining area
439
+ contours, _ = cv2.findContours(remaining_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
440
+
441
+ remaining_boxes = []
442
+ for contour in contours:
443
+ x, y, w, h = cv2.boundingRect(contour)
444
+ remaining_boxes.append((x, y, x + w, y + h))
445
+
446
+ return remaining_boxes
447
+
448
+ def is_valid_panel(
449
+ image_size,
450
+ boxes,
451
+ min_width_ratio: float,
452
+ min_height_ratio: float
453
+ ):
454
+ """
455
+ Check if each panel (box) is valid based on minimum width and height ratio of image size.
456
 
457
+ Args:
458
+ image_size: (width, height) of the image.
459
+ boxes: List of (x1, y1, x2, y2) panel boxes.
460
+ min_width_ratio: Minimum allowed width as a ratio of image width (e.g. 0.05).
461
+ min_height_ratio: Minimum allowed height as a ratio of image height (e.g. 0.05).
462
+
463
+ Returns:
464
+ List of booleans indicating if each panel is valid.
465
+ """
466
+ image_width, image_height = image_size
467
+ min_width = image_width * min_width_ratio
468
+ min_height = image_height * min_height_ratio
469
+
470
+ validity = []
471
+ for x1, y1, x2, y2 in boxes:
472
+ box_width = x2 - x1
473
+ box_height = y2 - y1
474
+ is_valid = box_width >= min_width and box_height >= min_height
475
+ if is_valid:
476
+ validity.append((x1, y1, x2, y2))
477
 
478
+ return validity