Added some fixes
This commit is contained in:
209
pipeline.py
209
pipeline.py
@@ -2,60 +2,76 @@
|
||||
"""
|
||||
pipeline.py
|
||||
───────────────────────────────────────────────────────────────
|
||||
Translation-only pipeline for Dandadan_059_2022_Digital
|
||||
Translation + render pipeline
|
||||
|
||||
Flow per page:
|
||||
1. Run translate_manga_text() → output.txt + bubbles.json
|
||||
2. Copy original image to workdir for reference
|
||||
1) translate_manga_text() -> output.txt + bubbles.json (+ debug_clusters.png if DEBUG)
|
||||
2) render_translations() -> page_translated.png
|
||||
3) Pack CBZ with originals + rendered pages + text outputs
|
||||
|
||||
Folder structure produced:
|
||||
Dandadan_059_2022_Digital_1r0n/
|
||||
Folder structure:
|
||||
<CHAPTER_DIR>/
|
||||
├── 000.png
|
||||
├── 001.png
|
||||
└── translated/
|
||||
├── 00/
|
||||
│ ├── output.txt ← translations to review
|
||||
│ ├── bubbles.json ← bubble boxes
|
||||
│ └── debug_clusters.png ← cluster debug (if DEBUG=True)
|
||||
├── 01/
|
||||
├── 000/
|
||||
│ ├── output.txt
|
||||
│ ├── bubbles.json
|
||||
│ ├── page_translated.png
|
||||
│ └── debug_clusters.png (optional)
|
||||
├── 001/
|
||||
│ └── ...
|
||||
└── ...
|
||||
|
||||
Dandadan_059_translated.cbz ← original pages + translations
|
||||
zipped for reference
|
||||
CBZ:
|
||||
- pages/<original pages>
|
||||
- rendered/<page_stem>_translated.png
|
||||
- translations/<page_stem>_output.txt
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import zipfile
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# CONFIG — edit these as needed
|
||||
# CONFIG
|
||||
# ─────────────────────────────────────────────
|
||||
CHAPTER_DIR = "/Users/guillemhernandezsola/Downloads/Spy_x_Family_076_2023_Digital_1r0n"
|
||||
OUTPUT_CBZ = "/Users/guillemhernandezsola/Downloads/Spy_x_Family_076_2023_Digital_1r0n_translated.cbz"
|
||||
SOURCE_LANG = "en"
|
||||
TARGET_LANG = "ca"
|
||||
CHAPTER_DIR = "/Users/guillemhernandezsola/Downloads/Dandadan_059_2022_Digital_1r0n"
|
||||
OUTPUT_CBZ = "/Users/guillemhernandezsola/Downloads/Dandadan_059_2022_Digital_1r0n_translated.cbz"
|
||||
|
||||
# manga-translator.py settings
|
||||
SOURCE_LANG = "en"
|
||||
TARGET_LANG = "ca"
|
||||
|
||||
# translator (NEW signature-compatible)
|
||||
CONFIDENCE_THRESHOLD = 0.10
|
||||
MIN_TEXT_LENGTH = 2
|
||||
CLUSTER_EPS = "auto"
|
||||
PROXIMITY_PX = 80
|
||||
MIN_TEXT_LENGTH = 1
|
||||
GAP_PX = "auto" # was cluster/proximity in old version
|
||||
FILTER_SFX = True
|
||||
QUALITY_THRESHOLD = 0.5
|
||||
UPSCALE_FACTOR = 2.5
|
||||
BBOX_PADDING = 5
|
||||
QUALITY_THRESHOLD = 0.50
|
||||
READING_MODE = "ltr"
|
||||
DEBUG = True
|
||||
|
||||
# renderer
|
||||
RENDER_ENABLED = True
|
||||
RENDER_OUTPUT_NAME = "page_translated.png"
|
||||
|
||||
# optional custom font list for renderer
|
||||
FONT_CANDIDATES = [
|
||||
"fonts/ComicNeue-Regular.ttf",
|
||||
"fonts/ComicRelief-Regular.ttf"
|
||||
]
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# DYNAMIC MODULE LOADER
|
||||
# ─────────────────────────────────────────────
|
||||
def load_module(name, filepath):
|
||||
spec = importlib.util.spec_from_file_location(name, filepath)
|
||||
spec = importlib.util.spec_from_file_location(name, filepath)
|
||||
if spec is None or spec.loader is None:
|
||||
raise FileNotFoundError(f"Cannot load spec for {filepath}")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
@@ -65,10 +81,10 @@ def load_module(name, filepath):
|
||||
# HELPERS
|
||||
# ─────────────────────────────────────────────
|
||||
def sorted_pages(chapter_dir):
|
||||
exts = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
exts = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
pages = [
|
||||
p for p in Path(chapter_dir).iterdir()
|
||||
if p.suffix.lower() in exts
|
||||
if p.is_file() and p.suffix.lower() in exts
|
||||
]
|
||||
return sorted(pages, key=lambda p: p.stem)
|
||||
|
||||
@@ -80,82 +96,97 @@ def make_page_workdir(chapter_dir, page_stem):
|
||||
|
||||
|
||||
def pack_cbz(chapter_dir, translated_dir, output_cbz):
|
||||
"""
|
||||
Packs into CBZ:
|
||||
- All original pages (from chapter_dir root)
|
||||
- All output.txt (one per page subfolder)
|
||||
Sorted by page stem for correct reading order.
|
||||
"""
|
||||
exts = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
pages = sorted(
|
||||
exts = {".jpg", ".jpeg", ".png", ".webp"}
|
||||
|
||||
pages = sorted(
|
||||
[p for p in Path(chapter_dir).iterdir()
|
||||
if p.suffix.lower() in exts],
|
||||
if p.is_file() and p.suffix.lower() in exts],
|
||||
key=lambda p: p.stem
|
||||
)
|
||||
txts = sorted(
|
||||
|
||||
txts = sorted(
|
||||
translated_dir.rglob("output.txt"),
|
||||
key=lambda p: p.parent.name
|
||||
)
|
||||
|
||||
rendered = sorted(
|
||||
translated_dir.rglob(RENDER_OUTPUT_NAME),
|
||||
key=lambda p: p.parent.name
|
||||
)
|
||||
|
||||
if not pages:
|
||||
print("⚠️ No original pages found — CBZ not created.")
|
||||
return
|
||||
|
||||
with zipfile.ZipFile(output_cbz, "w",
|
||||
compression=zipfile.ZIP_STORED) as zf:
|
||||
# Original pages
|
||||
with zipfile.ZipFile(output_cbz, "w", compression=zipfile.ZIP_STORED) as zf:
|
||||
# original pages
|
||||
for img in pages:
|
||||
arcname = f"pages/{img.name}"
|
||||
zf.write(img, arcname)
|
||||
print(f" 🖼 {arcname}")
|
||||
|
||||
# Translation text files
|
||||
# rendered pages
|
||||
for rp in rendered:
|
||||
arcname = f"rendered/{rp.parent.name}_translated.png"
|
||||
zf.write(rp, arcname)
|
||||
print(f" 🎨 {arcname}")
|
||||
|
||||
# text outputs
|
||||
for txt in txts:
|
||||
arcname = f"translations/{txt.parent.name}_output.txt"
|
||||
zf.write(txt, arcname)
|
||||
print(f" 📄 {arcname}")
|
||||
|
||||
print(f"\n✅ CBZ saved → {output_cbz} "
|
||||
f"({len(pages)} page(s), {len(txts)} translation(s))")
|
||||
print(
|
||||
f"\n✅ CBZ saved → {output_cbz} "
|
||||
f"({len(pages)} original, {len(rendered)} rendered, {len(txts)} text)"
|
||||
)
|
||||
|
||||
|
||||
# ─────────────────────────────────────────────
|
||||
# PER-PAGE PIPELINE
|
||||
# ─────────────────────────────────────────────
|
||||
def process_page(page_path, workdir, translator_module):
|
||||
def process_page(page_path, workdir, translator_module, renderer_module):
|
||||
"""
|
||||
Runs translator for a single page.
|
||||
All output files land in workdir.
|
||||
Returns True on success, False on failure.
|
||||
Runs translator + renderer for one page.
|
||||
All generated files are written inside workdir.
|
||||
"""
|
||||
print(f"\n{'─'*60}")
|
||||
print(f" PAGE: {page_path.name}")
|
||||
print(f"{'─'*60}")
|
||||
print(f"\n{'─' * 70}")
|
||||
print(f"PAGE: {page_path.name}")
|
||||
print(f"{'─' * 70}")
|
||||
|
||||
orig_dir = os.getcwd()
|
||||
try:
|
||||
# chdir into workdir so debug_clusters.png,
|
||||
# temp files etc. all land there
|
||||
os.chdir(workdir)
|
||||
|
||||
# 1) translate
|
||||
translator_module.translate_manga_text(
|
||||
image_path = str(page_path.resolve()),
|
||||
source_lang = SOURCE_LANG,
|
||||
target_lang = TARGET_LANG,
|
||||
confidence_threshold = CONFIDENCE_THRESHOLD,
|
||||
export_to_file = "output.txt",
|
||||
export_bubbles_to = "bubbles.json",
|
||||
min_text_length = MIN_TEXT_LENGTH,
|
||||
cluster_eps = CLUSTER_EPS,
|
||||
proximity_px = PROXIMITY_PX,
|
||||
filter_sound_effects = FILTER_SFX,
|
||||
quality_threshold = QUALITY_THRESHOLD,
|
||||
upscale_factor = UPSCALE_FACTOR,
|
||||
bbox_padding = BBOX_PADDING,
|
||||
debug = DEBUG,
|
||||
image_path= str(page_path.resolve()),
|
||||
source_lang=SOURCE_LANG,
|
||||
target_lang=TARGET_LANG,
|
||||
confidence_threshold=CONFIDENCE_THRESHOLD,
|
||||
min_text_length=MIN_TEXT_LENGTH,
|
||||
gap_px=GAP_PX,
|
||||
filter_sound_effects=FILTER_SFX,
|
||||
quality_threshold=QUALITY_THRESHOLD,
|
||||
export_to_file="output.txt",
|
||||
export_bubbles_to="bubbles.json",
|
||||
reading_mode=READING_MODE,
|
||||
debug=DEBUG
|
||||
)
|
||||
print(" ✅ translator done")
|
||||
|
||||
# 2) render
|
||||
if RENDER_ENABLED:
|
||||
renderer_module.render_translations(
|
||||
input_image=str(page_path.resolve()),
|
||||
output_image=RENDER_OUTPUT_NAME,
|
||||
translations_file="output.txt",
|
||||
bubbles_file="bubbles.json",
|
||||
font_candidates=FONT_CANDIDATES
|
||||
)
|
||||
print(" ✅ renderer done")
|
||||
|
||||
print(f" ✅ Translated → {workdir}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
@@ -170,16 +201,20 @@ def process_page(page_path, workdir, translator_module):
|
||||
# MAIN
|
||||
# ─────────────────────────────────────────────
|
||||
def main():
|
||||
# ── Load translator module ────────────────────────────────────
|
||||
print("Loading manga-translator.py...")
|
||||
print("Loading modules...")
|
||||
|
||||
try:
|
||||
translator = load_module(
|
||||
"manga_translator", "manga-translator.py")
|
||||
except FileNotFoundError as e:
|
||||
print(f"❌ Could not load module: {e}")
|
||||
translator = load_module("manga_translator", "manga-translator.py")
|
||||
except Exception as e:
|
||||
print(f"❌ Could not load manga-translator.py: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
renderer = load_module("manga_renderer", "manga-renderer.py")
|
||||
except Exception as e:
|
||||
print(f"❌ Could not load manga-renderer.py: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# ── Discover pages ────────────────────────────────────────────
|
||||
pages = sorted_pages(CHAPTER_DIR)
|
||||
if not pages:
|
||||
print(f"❌ No images found in: {CHAPTER_DIR}")
|
||||
@@ -187,33 +222,31 @@ def main():
|
||||
|
||||
print(f"\n📖 Chapter : {CHAPTER_DIR}")
|
||||
print(f" Pages : {len(pages)}")
|
||||
print(f" Source : {SOURCE_LANG} → Target: {TARGET_LANG}\n")
|
||||
print(f" Source : {SOURCE_LANG} → Target: {TARGET_LANG}")
|
||||
print(f" Render : {'ON' if RENDER_ENABLED else 'OFF'}\n")
|
||||
|
||||
# ── Process each page ─────────────────────────────────────────
|
||||
translated_dir = Path(CHAPTER_DIR) / "translated"
|
||||
succeeded = []
|
||||
failed = []
|
||||
failed = []
|
||||
|
||||
for i, page_path in enumerate(pages, start=1):
|
||||
print(f"\n[{i}/{len(pages)}] {page_path.name}")
|
||||
print(f"[{i}/{len(pages)}] {page_path.name}")
|
||||
workdir = make_page_workdir(CHAPTER_DIR, page_path.stem)
|
||||
ok = process_page(page_path, workdir, translator)
|
||||
ok = process_page(page_path, workdir, translator, renderer)
|
||||
if ok:
|
||||
succeeded.append(page_path.name)
|
||||
else:
|
||||
failed.append(page_path.name)
|
||||
|
||||
# ── Summary ───────────────────────────────────────────────────
|
||||
print(f"\n{'═'*60}")
|
||||
print(f" PIPELINE COMPLETE")
|
||||
print(f" ✅ {len(succeeded)} page(s) succeeded")
|
||||
print(f"\n{'═' * 70}")
|
||||
print("PIPELINE COMPLETE")
|
||||
print(f"✅ {len(succeeded)} page(s) succeeded")
|
||||
if failed:
|
||||
print(f" ❌ {len(failed)} page(s) failed:")
|
||||
print(f"❌ {len(failed)} page(s) failed:")
|
||||
for f in failed:
|
||||
print(f" • {f}")
|
||||
print(f"{'═'*60}\n")
|
||||
print(f" • {f}")
|
||||
print(f"{'═' * 70}\n")
|
||||
|
||||
# ── Pack CBZ ──────────────────────────────────────────────────
|
||||
print("Packing CBZ...")
|
||||
pack_cbz(CHAPTER_DIR, translated_dir, OUTPUT_CBZ)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user