Added all
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import textwrap
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import cv2
|
||||
import numpy as np
|
||||
import warnings
|
||||
from typing import List, Tuple, Dict, Any, Optional
|
||||
|
||||
@@ -519,6 +520,36 @@ def build_region_flags(raw_text, corrected_text, region_type, conf):
|
||||
# ============================================================
|
||||
# HELPERS
|
||||
# ============================================================
|
||||
def inpaint_text_only(image_bgr, box_xywh):
|
||||
"""
|
||||
Erases the original text inside the bounding box using inpainting,
|
||||
leaving the background intact without drawing new text.
|
||||
"""
|
||||
x, y = int(box_xywh["x"]), int(box_xywh["y"])
|
||||
w, h = int(box_xywh["w"]), int(box_xywh["h"])
|
||||
|
||||
# 1. Extract the Region of Interest (ROI)
|
||||
roi = image_bgr[y:y+h, x:x+w]
|
||||
if roi.size == 0:
|
||||
return image_bgr
|
||||
|
||||
# 2. Create a mask for the dark text
|
||||
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
|
||||
# Threshold: Pixels darker than 100 become white (the mask), others become black
|
||||
_, mask = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
|
||||
|
||||
# Dilate the mask slightly to ensure the edges of the letters are fully covered
|
||||
kernel = np.ones((3,3), np.uint8)
|
||||
mask = cv2.dilate(mask, kernel, iterations=1)
|
||||
|
||||
# 3. Inpaint the background to erase the text
|
||||
inpainted_roi = cv2.inpaint(roi, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
|
||||
|
||||
# Put the erased background back into the main image
|
||||
image_bgr[y:y+h, x:x+w] = inpainted_roi
|
||||
|
||||
return image_bgr
|
||||
|
||||
def split_boxes_by_horizontal_gap(out_boxes, out_indices, out_quads, out_lines, ocr, gap_multiplier=1.5):
|
||||
"""
|
||||
Splits a single bounding box into multiple boxes if there is a large horizontal
|
||||
@@ -2749,6 +2780,14 @@ def process_manga_page(image_path: str,
|
||||
# Bubble groups (lines as rendered in the bubble)
|
||||
bubble_groups = build_text_from_layout(indices, ocr)
|
||||
|
||||
# ── Step 12.5: Detect Background Complexity ───────────
|
||||
# Analyze the pixels to see if it's a plain white bubble or complex artwork
|
||||
feats = contour_features_for_box(image_bgr, adjusted_box_xyxy)
|
||||
if feats["whiteness_ratio"] > 0.75 and feats["edge_density"] < 0.10:
|
||||
bg_type = "white"
|
||||
else:
|
||||
bg_type = "complex"
|
||||
|
||||
# ── Step 13: Translate ────────────────────────────────
|
||||
translated = ""
|
||||
translation_input = corrected_text
|
||||
@@ -2779,6 +2818,7 @@ def process_manga_page(image_path: str,
|
||||
results[str(bid)] = {
|
||||
"order": order_idx,
|
||||
"region_type": region_type,
|
||||
"background_type": bg_type, # <--- NEW FLAG ADDED HERE
|
||||
"confidence": round(conf, 4),
|
||||
"ocr_source": ocr_source,
|
||||
"raw_ocr": raw_text,
|
||||
@@ -2787,7 +2827,7 @@ def process_manga_page(image_path: str,
|
||||
"translated": translated,
|
||||
"flags": flags,
|
||||
"bubble_groups": bubble_groups,
|
||||
"box": xyxy_to_xywh(adjusted_box_xyxy), # <--- Uses the adjusted box
|
||||
"box": xyxy_to_xywh(adjusted_box_xyxy),
|
||||
"lines": bubble_groups,
|
||||
}
|
||||
|
||||
@@ -2801,6 +2841,7 @@ def process_manga_page(image_path: str,
|
||||
_write_txt_output(results, output_txt)
|
||||
|
||||
return results
|
||||
|
||||
# ============================================================
|
||||
# OUTPUT WRITERS
|
||||
# ============================================================
|
||||
|
||||
Reference in New Issue
Block a user