Flux images en 4 etapes explicites + refonte du front façon Aceternity UI

- typeset : flux en deux phases — 3/4 inpainting du texte original sur les
  images BRUTES -> export/images-clean/ (inpaint_image_file, toutes les
  regions valides effacees), puis 4/4 retypographie de la traduction sur
  l'image nettoyee -> export/images-<lang>/ ; replace_texts garde son
  comportement (inpainter=None -> default_inpainter()), sentinel _DRAW_ONLY
  pour la retypographie seule ; _fit_font (merge fc3211b) conservé : taille
  = plus grande police tenant dans la boîte OCR (hauteur de boîte)
- inpaint (LaMa) : dilatation du masque (MaxFilter, mask_dilate=2) pour une
  suppression complète du texte, style pinceau magique (bords des glyphes
  inclus) ; composite toujours limité aux pixels masqués
- pipeline : logs 1/4 OCR -> 2/4 traduction -> 3/4 inpainting -> 4/4
  retypographie ; inpainter résolu une fois et passé à typeset_chapter
- front : INDEX_HTML/SETUP_HTML entierement refondus façon Aceternity UI
  (fond sombre #030303, grille + aurora animée, logo dégradé animé, cartes
  à bord conique animé, toggles, selects ocr_engine/inpaint_engine, barre de
  progression dégradée) en vanilla CSS/JS sans build ni CDN ; ids et
  fonctions JS conservés, case zip export toujours cochee par défaut
- tests : dilatation du masque LaMa, images-clean + images-fr, _DRAW_ONLY,
  _fit_font (merge). Vérifié en reel (page 1 du out/, vrai modèle LaMa) :
  0 pixel modifié hors masques+1px de dilatation, texte effacé dans les
  bulles, traduction réinsérée. Suite : 90 passed, 1 skipped.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
Orionis committed 2026-11-18 23:18:02 +01:00
1 parent 1c843c223e
commit 5c2c102518
7 files changed
+311 -96

No files matched your search

+21
View File
@@ -271,6 +271,27 @@ class TestLamaInpainter(unittest.TestCase):
self.assertEqual(tuple(out_arr[250, 350]), (240, 240, 245))
self.assertEqual(tuple(out_arr[10, 10]), (240, 240, 245))
def test_mask_is_dilated_for_full_text_removal(self):
"""Le masque doit être dilaté (style pinceau magique) : le texte est effacé
entièrement, y compris ses bords anti-crénelés."""
with tempfile.TemporaryDirectory() as tmp:
model = Path(tmp) / "lama_fp32.onnx"
model.write_bytes(b"fake")
img = Image.new("RGB", (512, 512), (240, 240, 245))
fake_mod, fake_session_cls = _fake_onnxruntime_module(fill_value=200.0)
region = [{"index": 0, "box": [100, 100, 50, 30], "text": "x"}]
with mock.patch.dict(sys.modules, {"onnxruntime": fake_mod}):
LamaInpainter(model_path=model).inpaint_image(img, region) # mask_dilate=2 par défaut
mask = fake_session_cls.instances[-1].calls[0]["mask"][0, 0]
# crop 256x256 en (0,0), échelle x2 -> rect 512 : [194,194]..[306,266]
self.assertEqual(mask[230, 250], 1.0) # centre de la boîte
self.assertEqual(mask[192, 192], 1.0) # dilaté de 2 px (hors rectangle strict)
self.assertEqual(mask[190, 190], 0.0) # au-delà de la dilatation
LamaInpainter(model_path=model, mask_dilate=0).inpaint_image(img, region)
mask0 = fake_session_cls.instances[-1].calls[0]["mask"][0, 0]
self.assertEqual(mask0[230, 250], 1.0)
self.assertEqual(mask0[192, 192], 0.0, "sans dilatation, le bord reste à 0")
def test_inpaint_image_uses_poly_when_present(self):
with tempfile.TemporaryDirectory() as tmp:
model = Path(tmp) / "lama_fp32.onnx"
+53 -1
View File
@@ -228,6 +228,57 @@ class TestOCRTypeset(unittest.TestCase):
self.assertEqual(len(outputs), 1)
self.assertTrue((d / "export" / "images-fr" / "p1.png").exists())
def test_typeset_chapter_ecrit_images_clean_puis_traduites(self):
"""Flux en deux étapes : 3/4 inpainting -> export/images-clean, 4/4 retypographie
-> export/images-<lang>."""
from scalper.inpaint import SimpleInpainter
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
(d / "images").mkdir()
self._make_image(d / "images" / "p1.png")
(d / "ocr.json").write_text(
json.dumps({"lang": "eng", "regions": [{"index": 0, "image": "p1.png", "box": [10, 10, 280, 60], "text": "Hello world"}]}),
encoding="utf-8",
)
outputs = typeset_chapter(d, "fr", translations=["Bonjour le monde"], inpainter=SimpleInpainter())
self.assertEqual(len(outputs), 1)
clean = d / "export" / "images-clean" / "p1.png"
trad = d / "export" / "images-fr" / "p1.png"
self.assertTrue(clean.exists(), "étape 3 : image nettoyée (inpainting)")
self.assertTrue(trad.exists(), "étape 4 : image re-traduite")
c = Image.open(clean).convert("RGB")
dark = any(c.getpixel((px, py))[0] < 100 for px in range(10, 290) for py in range(10, 70))
self.assertFalse(dark, "le texte original doit être entièrement effacé dans images-clean")
t = Image.open(trad).convert("RGB")
dark_t = any(t.getpixel((px, py))[0] < 100 for px in range(10, 290) for py in range(10, 70))
self.assertTrue(dark_t, "la traduction doit être redessinée dans images-<lang>")
def test_replace_texts_draw_only_ne_inpaint_pas(self):
"""inpainter=_DRAW_ONLY : retypographie seule, l'image d'entrée n'est pas inpaintée
(utilisé sur les images déjà nettoyées d'export/images-clean)."""
from scalper.typeset import _DRAW_ONLY
class RedInpainter:
def inpaint_image(self, img, regions):
d = ImageDraw.Draw(img)
for r in regions:
x, y, w, h = r["box"]
d.rectangle([x, y, x + w, y + h], fill=(255, 0, 0))
return img
with tempfile.TemporaryDirectory() as tmp:
src = Path(tmp) / "p.png"
out1, out2 = Path(tmp) / "o1.png", Path(tmp) / "o2.png"
self._make_image(src)
regions = [{"index": 0, "box": [10, 10, 280, 60], "text": "Hello world"}]
replace_texts(src, regions, ["x"], out1, inpainter=RedInpainter())
replace_texts(src, regions, ["x"], out2, inpainter=_DRAW_ONLY)
r1 = Image.open(out1).convert("RGB")
r2 = Image.open(out2).convert("RGB")
self.assertEqual(r1.getpixel((12, 12)), (255, 0, 0), "inpainter explicite : boîte remplie")
self.assertEqual(r2.getpixel((12, 12)), (200, 220, 255), "_DRAW_ONLY : boîte non inpaintée")
def test_typeset_chapter_explicit_translations(self):
with tempfile.TemporaryDirectory() as tmp:
d = Path(tmp)
@@ -297,7 +348,8 @@ class TestOCRTypeset(unittest.TestCase):
with mock.patch("scalper.ocr.ocr_chapter", side_effect=fake_ocr):
out = pipeline._run_ocr_typeset(d, {"language": "en"}, "fr", FakeLLM(), {})
self.assertTrue(out.startswith("ok"), out)
self.assertTrue((d / "export" / "images-fr" / "p1.png").exists())
self.assertTrue((d / "export" / "images-clean" / "p1.png").exists(), "étape 3 : inpainting -> images-clean")
self.assertTrue((d / "export" / "images-fr" / "p1.png").exists(), "étape 4 : retypographie -> images-fr")
if __name__ == "__main__":