Carte de profondeur pour des planches qui suivent les volumes

La profondeur de la scène est estimée à partir de la photo (MiDaS small
ou Depth Anything V2 Small) et mélangée aux gris : les planches suivent
les volumes du sujet plutôt que ses lumières et ses ombres.

- Onglet « Profondeur » : part de la carte de profondeur, modèle, et
  détails fins de la photo gravés dans la profondeur (yeux, bouche).
- Répartition des niveaux : rapproche les planches d'aires égales quand
  les valeurs sont tassées sur quelques gris.
- Images 16 bits lues sur toute leur plage (elles étaient écrêtées à 255).
- Le Python d'Inkscape (MinGW, sans pip) ne peut pas charger onnxruntime :
  le calcul tourne dans un environnement à part, installé par
  deploy.ps1 -Depth, lancé en sous-processus, résultat gardé en cache.
- Depth Anything sur la carte graphique (DirectML) avec repli sur le
  processeur ; MiDaS small toujours sur le processeur, DirectML lui
  faisant rendre une carte fausse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AumyrbcuQobB3GbPrFjG9i
This commit is contained in:
AlexandClaude Opus 5.5 committed 2026-10-04 14:19:26 +02:00
1 parent 16589716fd
commit 3ed0fa0987
13 files changed
+1090 -37

No files matched your search

+290 -7
View File
@@ -11,11 +11,13 @@ import xml.etree.ElementTree as ET
import numpy as np
import pytest
import depth_estimate
import gray_iso_layers_depth
from gray_iso_layers_core import (
box_blur, contour_lines, contour_rings, edge_preserving_blur, gaussian_blur,
gray_to_hex, image_ring, iso_boards, iso_layers, lines_to_d, normalize,
parse_color, ring_area, rings_to_d, scale_lines, scale_rings, simplify_chain,
simplify_ring, smooth, thresholds)
add_detail, blend_fields, box_blur, contour_lines, contour_rings, edge_preserving_blur,
gaussian_blur, gray_to_hex, guided_filter, image_ring, iso_boards, iso_layers,
lines_to_d, normalize, parse_color, ring_area, rings_to_d, scale_lines,
scale_rings, simplify_chain, simplify_ring, smooth, spread_levels, thresholds)
HERE = os.path.dirname(os.path.abspath(__file__))
@@ -171,6 +173,80 @@ def test_iso_layers_preserve_edges_keeps_a_thin_feature():
assert dark[0][:, 1].max() - dark[0][:, 1].min() <= 7
def test_spread_levels_gives_equal_areas():
# Rampe : les valeurs sont deja reparties, rien ne bouge.
assert np.allclose(spread_levels(ramp(200, 20)), ramp(200, 20), atol=0.01)
# Champ tasse vers le noir (carte de profondeur : fond etendu, sujet
# resserre) : a seuils reguliers, le premier niveau prend presque tout.
packed = ramp(200, 20) ** 4
assert np.array_equal(spread_levels(packed, 0), packed)
assert (packed < 0.25).mean() > 0.7
spread = spread_levels(packed)
assert spread.min() >= 0.0 and spread.max() == 1.0
for cut in (0.25, 0.5, 0.75):
assert math.isclose((spread < cut).mean(), cut, abs_tol=0.01)
# Dosage : a mi-chemin entre le champ et sa repartition.
assert np.allclose(spread_levels(packed, 0.5), (packed + spread) / 2.0)
assert not spread_levels(np.full((5, 5), 0.3)).any()
def test_iso_layers_equalize_fills_every_level():
packed = ramp(200, 20) ** 4
width = [evenodd_area(rings) / 19.0
for _level, _gray, rings in iso_layers(packed, levels=4)]
assert width[0] > 140 and width[3] < 15
width = [evenodd_area(rings) / 19.0
for _level, _gray, rings in iso_layers(packed, levels=4, equalize=1.0)]
assert np.allclose(width, 199 / 4.0, atol=2.0)
def test_guided_filter_snaps_a_blurred_edge_to_the_guide():
step = np.where(np.arange(80) < 40, 0.25, 0.75) * np.ones((60, 1))
blurred = gaussian_blur(step, 2.0)
def width(result):
"""Largeur de la transition entre les deux aplats, en echantillons."""
row = result.mean(axis=0)
return int(np.count_nonzero((row > 0.3) & (row < 0.7)))
# Le bord se recale si la fenetre est nettement plus large que le flou.
assert width(blurred) >= 6
assert width(guided_filter(step, blurred, 12)) <= 2
# Source uniforme : rien a recaler. Guide uniforme : simple lissage.
assert np.allclose(guided_filter(step, np.full(step.shape, 0.4), 8), 0.4)
assert np.allclose(guided_filter(np.zeros(step.shape), blurred, 3),
box_blur(box_blur(blurred, 3), 3))
assert np.array_equal(guided_filter(step, blurred, 0), blurred)
def test_add_detail_carves_fine_contrasts_only():
depth = ramp(80, 80)
# Fond uni, clair ou sombre : rien a graver, la profondeur ne bouge pas.
for level in (0.1, 0.9):
assert np.allclose(add_detail(depth, np.full((80, 80), level), 1.0, 4.0), depth)
# Trait sombre fin sur fond clair (une bouche) : il se creuse, et loin de
# lui la profondeur est intacte.
gray = np.full((80, 80), 0.8)
gray[38:42, 20:60] = 0.1
carved = add_detail(depth, gray, 0.5, 4.0)
assert (carved[39:41, 30:50] < depth[39:41, 30:50] - 0.2).all()
assert np.allclose(carved[:15], depth[:15], atol=1e-3)
assert carved.min() >= 0.0 and carved.max() <= 1.0
# Dosage : sans detail, la profondeur seule, etiree sur 0..1.
assert np.allclose(add_detail(depth * 3.0, gray, 0, 4.0), depth)
lighter = add_detail(depth, gray, 0.25, 4.0)
assert (lighter[39:41, 30:50] > carved[39:41, 30:50]).all()
def test_blend_fields():
gray, depth = ramp(), ramp().T[:40, :40].repeat(2, axis=1)[:, :60] * 3.0
assert np.array_equal(blend_fields(gray, depth, 0), normalize(gray))
assert np.array_equal(blend_fields(gray, depth, 1), normalize(depth))
assert np.array_equal(blend_fields(gray, depth, 7), normalize(depth))
assert np.allclose(blend_fields(gray, depth, 0.25),
0.75 * normalize(gray) + 0.25 * normalize(depth))
def test_contour_of_cone_is_a_circle():
# Seuil 0.5 sur un cone de rayon 40 : cercle de rayon 20.
rings = contour_rings(cone(), 0.5)
@@ -501,6 +577,141 @@ def test_rings_to_d_keeps_the_image_border_straight():
assert "L " not in d and d.count("C ") == len(circle)
# --------------------------------------------------------------------------
# Carte de profondeur (sans inkex ni onnxruntime : le calcul est simule)
# --------------------------------------------------------------------------
@pytest.fixture
def depth_home(tmp_path, monkeypatch):
"""Dossier de donnees factice, avec son Python et ses modeles, et un
calcul simule qui note ses appels."""
pytest.importorskip("PIL.Image")
home = tmp_path / "GrayIsoLayers"
(home / "models").mkdir(parents=True)
for name in gray_iso_layers_depth.MODELS.values():
(home / "models" / name).write_bytes(b"onnx")
monkeypatch.setattr(gray_iso_layers_depth, "data_dir", lambda: str(home))
monkeypatch.delenv("GRAY_ISO_LAYERS_PYTHON", raising=False)
python = gray_iso_layers_depth.find_python()
os.makedirs(os.path.dirname(python))
with open(python, "wb"):
pass
calls = []
def run(command, env=None, **_options):
calls.append((command, env))
np.save(command[3], np.full((4, 6), float(len(calls)), dtype=np.float32))
return type("Done", (), {"returncode": 0, "stderr": ""})()
monkeypatch.setattr(gray_iso_layers_depth.subprocess, "run", run)
return home, calls
def photo():
from PIL import Image
return Image.new("RGB", (12, 8), (200, 120, 40))
def test_depth_field_runs_the_helper_once_then_reads_the_cache(depth_home, monkeypatch):
home, calls = depth_home
monkeypatch.setenv("PYTHONPATH", "inkscape")
monkeypatch.setenv("PYTHONHOME", "inkscape")
depth = gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
assert depth.shape == (4, 6) and (depth == 1).all()
command, env = calls[0]
assert command[0] == gray_iso_layers_depth.find_python()
assert command[1].endswith("depth_estimate.py") and os.path.isfile(command[1])
assert command[4].endswith("midas_v21_small_256.onnx")
assert command[5:] == ["--model", "midas_small"]
# Les variables du Python d'Inkscape ne passent pas a l'autre Python.
assert "PYTHONPATH" not in env and "PYTHONHOME" not in env and "PATH" in env
# Meme image, meme modele : le cache repond. Autre modele ou autre
# image : nouveau calcul.
assert (gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small") == 1).all()
assert len(calls) == 1
gray_iso_layers_depth.depth_field(photo(), "abc", "depth_anything_v2_small")
gray_iso_layers_depth.depth_field(photo(), "def", "midas_small")
assert len(calls) == 3
assert len(os.listdir(str(home / "cache"))) == 3
def test_depth_field_prunes_the_cache(depth_home, monkeypatch):
home, _calls = depth_home
monkeypatch.setattr(gray_iso_layers_depth, "CACHE_LIMIT", 2)
for k, digest in enumerate(("a", "b", "c")):
gray_iso_layers_depth.depth_field(photo(), digest, "midas_small")
os.utime(str(home / "cache" / (digest + "-midas_small.npy")), (k, k))
gray_iso_layers_depth._prune(str(home / "cache"))
assert sorted(os.listdir(str(home / "cache"))) == [
"b-midas_small.npy", "c-midas_small.npy"]
def test_depth_field_reports_what_is_missing(depth_home, monkeypatch):
home, calls = depth_home
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="unknown model"):
gray_iso_layers_depth.depth_field(photo(), "abc", "autre")
# Calcul en echec : la derniere ligne de son erreur est rapportee.
monkeypatch.setattr(
gray_iso_layers_depth.subprocess, "run",
lambda *_a, **_k: type("Done", (), {"returncode": 1,
"stderr": "Traceback\nboum\n"})())
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="^boum$"):
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
def too_long(*_args, **_options):
raise gray_iso_layers_depth.subprocess.TimeoutExpired("python", 120)
monkeypatch.setattr(gray_iso_layers_depth.subprocess, "run", too_long)
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="timed out"):
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
# Modele absent, puis environnement Python absent.
os.remove(str(home / "models" / "midas_v21_small_256.onnx"))
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="midas_v21"):
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
os.remove(gray_iso_layers_depth.find_python())
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="python"):
gray_iso_layers_depth.depth_field(photo(), "abc", "depth_anything_v2_small")
assert calls == [] and not (home / "cache").exists()
def test_find_python_honours_the_environment_variable(monkeypatch):
monkeypatch.setenv("GRAY_ISO_LAYERS_PYTHON", "ailleurs/python")
assert gray_iso_layers_depth.find_python() == "ailleurs/python"
monkeypatch.delenv("GRAY_ISO_LAYERS_PYTHON")
assert gray_iso_layers_depth.find_python().startswith(
gray_iso_layers_depth.data_dir())
def test_depth_estimate_preprocess_and_providers():
pytest.importorskip("PIL.Image")
assert sorted(depth_estimate.MODELS) == sorted(gray_iso_layers_depth.MODELS)
for size in depth_estimate.MODELS.values():
tensor = depth_estimate.preprocess(photo(), size)
assert tensor.shape == (1, 3, size, size) and tensor.dtype == np.float32
# Normalisation ImageNet du rouge : (200 / 255 - 0.485) / 0.229.
assert math.isclose(tensor[0, 0, 0, 0], (200 / 255.0 - 0.485) / 0.229, rel_tol=1e-4)
gpu, cpu = "DmlExecutionProvider", "CPUExecutionProvider"
assert depth_estimate.choose_providers([gpu, cpu]) == [gpu, cpu]
assert depth_estimate.choose_providers([gpu, cpu], cpu=True) == [cpu]
assert depth_estimate.choose_providers([cpu]) == [cpu]
def test_depth_estimate_real_model(tmp_path):
"""Calcul reel, quand onnxruntime et le modele sont installes."""
pytest.importorskip("onnxruntime")
pytest.importorskip("PIL.Image")
weights = os.path.join(gray_iso_layers_depth.data_dir(), "models",
gray_iso_layers_depth.MODELS["midas_small"])
if not os.path.isfile(weights):
pytest.skip("modele absent : deploy.ps1 -Depth")
source, output = str(tmp_path / "photo.png"), str(tmp_path / "depth.npy")
photo().save(source)
assert depth_estimate.main([source, output, weights, "--cpu"]) == 0
depth = np.load(output)
assert depth.shape == (256, 256) and np.isfinite(depth).all()
# --------------------------------------------------------------------------
# Bout en bout (necessite inkex et Pillow)
# --------------------------------------------------------------------------
@@ -522,10 +733,13 @@ SVG = """<?xml version="1.0" encoding="UTF-8"?>
"""
def make_document(tmp_path, linked=False, transform=""):
def make_document(tmp_path, linked=False, transform="", bits=8):
"""Document de test : une image en cone, incorporee ou liee."""
image_module = pytest.importorskip("PIL.Image")
picture = image_module.fromarray((cone(64, 28.0) * 255).astype("uint8"), "L")
if bits == 16:
picture = image_module.fromarray((cone(64, 28.0) * 65535).astype("uint16"))
else:
picture = image_module.fromarray((cone(64, 28.0) * 255).astype("uint8"))
if linked:
picture.save(str(tmp_path / "cone.png"))
href = "cone.png"
@@ -704,6 +918,75 @@ def test_end_to_end_plain_blur(tmp_path):
assert paths(kept) != paths(plain)
def test_end_to_end_16_bit_image(tmp_path):
# Carte de profondeur en 16 bits : memes planches que la meme image en
# 8 bits, la ou une conversion directe l'ecreterait en image blanche.
args = ("--id=img1", "--levels=4", "--blur=0", "--shapes=band")
deep = run_extension(tmp_path, *args, document=make_document(tmp_path, bits=16))
plain = run_extension(tmp_path, *args)
assert [path.count("M ") for path in paths(deep)] == [2, 2, 2, 1]
def extent(path):
"""Etendue en x des points du chemin."""
xs = [float(x) for x, _y in re.findall(r"(-?[\d.]+),(-?[\d.]+)", path)]
return min(xs), max(xs)
for a, b in zip(paths(deep), paths(plain)):
assert np.allclose(extent(a), extent(b), atol=0.5)
def test_end_to_end_equalize(tmp_path):
# Cone : les disques a seuils reguliers ont des rayons reguliers ; a
# aires egales, le niveau le plus clair s'etend.
def top_width(*extra):
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
"--shapes=band", *extra)
xs = [float(x) for x, _y in re.findall(r"(-?[\d.]+),(-?[\d.]+)", paths(svg)[3])]
return max(xs) - min(xs)
assert top_width("--equalize=100") > 1.5 * top_width()
def test_end_to_end_depth(tmp_path, monkeypatch, capsys):
pytest.importorskip("inkex")
import gray_iso_layers
calls = []
def fake(picture, digest, model):
calls.append((picture.mode, picture.size, len(digest), model))
return ramp(32, 32) # profondeur : proche a droite
monkeypatch.setattr(gray_iso_layers, "depth_field", fake)
args = ("--id=img1", "--levels=4", "--blur=0", "--shapes=band")
cone_only = run_extension(tmp_path, *args)
assert calls == [] # part de profondeur nulle : aucun calcul
assert all("L " not in path for path in paths(cone_only)[1:])
svg = run_extension(tmp_path, *args, "--depth_mix=100", "--depth_detail=0",
"--depth_model=depth_anything_v2_small")
assert calls == [("RGB", (64, 64), 40, "depth_anything_v2_small")]
# Profondeur seule : quatre bandes verticales, bordees par l'image.
assert [path.count("M ") for path in paths(svg)] == [1, 1, 1, 1]
assert all("L " in path for path in paths(svg))
# Melange : ni le cone seul, ni la rampe seule.
mixed = run_extension(tmp_path, *args, "--depth_mix=50", "--depth_detail=0")
assert paths(mixed) != paths(svg) and paths(mixed) != paths(cone_only)
# Details fins : les contrastes du cone se gravent dans la rampe.
carved = run_extension(tmp_path, *args, "--depth_mix=100")
assert paths(carved) != paths(svg) and paths(carved) != paths(cone_only)
assert capsys.readouterr().err == ""
def missing(*_args):
raise gray_iso_layers.DepthUnavailable("missing file: venv")
# Profondeur indisponible : c'est dit, et les gris servent seuls.
monkeypatch.setattr(gray_iso_layers, "depth_field", missing)
svg = run_extension(tmp_path, *args, "--depth_mix=100")
assert paths(svg) == paths(cone_only)
error = capsys.readouterr().err
assert "missing file: venv" in error and "deploy.ps1 -Depth" in error
def test_end_to_end_without_image(tmp_path, capsys):
svg = run_extension(tmp_path, "--id=rect1")
assert "Board no. 1" not in svg
@@ -772,7 +1055,7 @@ def test_inx_matches_arguments():
options = GrayIsoLayers().arg_parser.parse_args([])
for name, text in params.items():
default = getattr(options, name)
if name in ("tab", "shapes", "fill_mode", "unit"):
if name in ("tab", "shapes", "depth_model", "fill_mode", "unit"):
continue # listes : la valeur par defaut est la premiere option
if isinstance(default, bool):
assert text == str(default).lower(), name