Utilisation de depth map

This commit is contained in:
Alex committed 2026-10-04 23:13:07 +02:00
1 parent 3ed0fa0987
commit 34984e96f7
14 files changed
+766 -54

No files matched your search

+117 -4
View File
@@ -14,9 +14,10 @@ import pytest
import depth_estimate
import gray_iso_layers_depth
from gray_iso_layers_core import (
add_detail, blend_fields, box_blur, contour_lines, contour_rings, edge_preserving_blur,
add_detail, blend_fields, board_labels, 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,
lines_to_d, mask_components, mask_depth, normalize, number_lines, number_size,
parse_color, region_mask, 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__))
@@ -577,6 +578,63 @@ def test_rings_to_d_keeps_the_image_border_straight():
assert "L " not in d and d.count("C ") == len(circle)
def test_number_lines_draw_digits_as_segments():
closed, opened = number_lines(8, 50, 20, 10)
assert closed == [] and len(opened) == 7
points = np.vstack(opened)
# Chiffre de 5 x 10, centre en (50, 20).
assert points.min(axis=0).tolist() == [47.5, 15]
assert points.max(axis=0).tolist() == [52.5, 25]
assert [len(number_lines(n, 0, 0, 1)[1]) for n in (1, 7, 4, 0)] == [2, 3, 4, 6]
# Deux chiffres, encadres : un cadre ferme autour du tout.
closed, opened = number_lines(12, 0, 0, 10, framed=True)
assert len(opened) == 2 + 5 and len(closed) == 1
width, height = number_size(12, 10, True)
assert (width, height) == (13 + 7, 10 + 7)
assert np.allclose(closed[0].max(axis=0), (width / 2, height / 2))
assert np.abs(np.vstack(opened)).max(axis=0).tolist() == [6.5, 5]
def test_region_mask_depth_and_components():
square = np.array([(10, 10), (30, 10), (30, 30), (10, 30)], dtype=float)
hole = np.array([(18, 18), (22, 18), (22, 22), (18, 22)], dtype=float)
far = np.array([(40, 5), (48, 5), (48, 9), (40, 9)], dtype=float)
mask, xs, ys = region_mask([square, hole, far], 61, 41, 1.0)
assert mask.shape == (len(ys), len(xs)) == (40, 60)
assert mask.sum() == 20 * 20 - 4 * 4 + 8 * 4
assert mask[15, 15] and not mask[19, 19] and mask[6, 43] and not mask[0, 0]
labels, count = mask_components(mask)
assert count == 2 and labels[15, 15] != labels[6, 43] and labels[0, 0] == 0
depth = mask_depth(mask)
assert depth[0, 0] == 0 and depth[10, 10] == 1 and depth[6, 43] == 2
# Troisieme case depuis le bord gauche du carre.
assert depth[15, 12] == 3
assert not region_mask([], 61, 41, 1.0)[0].any()
def test_board_labels_number_each_piece_and_each_marked_area():
boards = iso_boards(cone(), levels=4, shapes="light")
# Planche n° 1 (toute l'image) : le numero 2 au creux du disque ou se pose
# la planche n° 2, et son propre numero cache sous elle, a l'ecart.
own, after = board_labels(boards[0][2], boards[1][2], 1, 101, 101, 6.0, 2.0)
assert len(own) == 1 and len(after) == 1
(x, y, size), (nx, ny, nsize) = own[0], after[0]
assert math.hypot(nx - 50, ny - 50) < 2 and nsize == 6.0
assert contains(boards[1][2], x, y) and size >= 2.0
assert math.hypot(x - nx, y - ny) > 6.0
# Derniere planche : rien ne la recouvre, son numero reste visible.
own, after = board_labels(boards[3][2], None, 4, 101, 101, 6.0, 2.0)
assert after == [] and len(own) == 1
assert contains(boards[3][2], own[0][0], own[0][1])
# Piece trop etroite pour un numero : pas de numero.
assert board_labels(boards[3][2], None, 4, 101, 101, 30.0, 25.0) == ([], [])
# Deux pieces separees : un numero chacune.
left = np.array([(5, 5), (40, 5), (40, 40), (5, 40)], dtype=float)
right = left + (50, 50)
own, _after = board_labels([left, right], None, 3, 101, 101, 6.0, 2.0)
assert sorted(round(x) // 50 for x, _y, _size in own) == [0, 1]
# --------------------------------------------------------------------------
# Carte de profondeur (sans inkex ni onnxruntime : le calcul est simule)
# --------------------------------------------------------------------------
@@ -757,7 +815,9 @@ def run_extension(tmp_path, *args, linked=False, document=None, transform=""):
from gray_iso_layers import GrayIsoLayers
document = document or make_document(tmp_path, linked, transform)
out = tmp_path / "out.svg"
GrayIsoLayers().run([*args, "--output={}".format(out), document])
# Sans les numeros, sauf demande : les tests comptent les chemins.
GrayIsoLayers().run(["--numbers=false", *args, "--output={}".format(out),
document])
# inkex n'ecrit rien quand le document n'a pas change.
return out.read_text(encoding="utf-8") if out.exists() else ""
@@ -818,6 +878,59 @@ def test_end_to_end_default(tmp_path):
assert svg.count("transform=") == 1 # celle du calque d'origine
def test_end_to_end_boards_side_by_side(tmp_path):
# Image de 100 x 100 en (15, 25) : cases de 105 (ecart de 5 %), 4 planches
# sur 2 colonnes, la premiere case juste a droite de l'image.
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
"--layout=side")
assert layers_of(svg) == [("Photo", 0), ("Board no. 1", 2), ("Board no. 2", 2),
("Board no. 3", 2), ("Board no. 4", 1), ("Dessus", 0)]
found = paths(svg)
cuts = [path for path in found if "stroke:#ff0000" in path]
marks = [path for path in found if "stroke:#000000" in path]
assert ' d="M 120.000,25.000 L 220.000,25.000 L 220.000,125.000 L 120.000,125.000 Z"' in cuts[0]
def cell(path):
"""Case de la grille (colonne, ligne) ou tombent tous les points."""
points = re.findall(r"(-?[\d.]+),(-?[\d.]+)", path)
cells = {(int((float(x) - 15) // 105), int((float(y) - 25) // 105))
for x, y in points}
assert len(cells) == 1, cells
return cells.pop()
assert [cell(path) for path in cuts] == [(1, 0), (2, 0), (1, 1), (2, 1)]
# Le repere suit sa planche : c'est le trace de la suivante, ramene
# dans la case de celle-ci.
assert [cell(path) for path in marks] == [(1, 0), (2, 0), (1, 1)]
stacked = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0")
assert paths(stacked)[0] != found[0]
assert svg.count("transform=") == 1 # celle du calque d'origine
# Image tournee : la grille part de son encombrement dans le document.
svg = run_extension(tmp_path, "--id=img1", "--levels=2", "--blur=0",
"--layout=side", "--mark=false", transform="rotate(90)")
assert ' d="M 110.000,25.000 L 110.000,125.000 L 10.000,125.000 L 10.000,25.000 Z"' in paths(svg)[0]
def test_end_to_end_numbers(tmp_path):
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
"--numbers=true")
# Un chemin de numeros par planche, en plus de la decoupe et du repere.
assert layers_of(svg) == [("Photo", 0), ("Board no. 1", 3), ("Board no. 2", 3),
("Board no. 3", 3), ("Board no. 4", 2), ("Dessus", 0)]
numbered = [path for path in paths(svg) if 'label="Numbers"' in path]
assert len(numbered) == 4
for path in numbered:
assert "fill:none" in path and "stroke:#000000" in path and "C " not in path
# Planche n° 1 : son numero encadre (un cadre ferme, 2 segments) et le
# numero 2 (5 segments) ; la derniere n'a que le sien (4 segments).
assert numbered[0].count("Z") == 1 and numbered[0].count("M ") == 1 + 2 + 5
assert numbered[3].count("Z") == 1 and numbered[3].count("M ") == 1 + 4
# Sans trait, rien a graver.
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
"--numbers=true", "--stroke_width=0")
assert 'label="Numbers"' not in svg
def test_end_to_end_marking_options(tmp_path):
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
"--mark=false")
@@ -1055,7 +1168,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", "depth_model", "fill_mode", "unit"):
if name in ("tab", "shapes", "layout", "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