Files
2026-09-25 17:14:49 +02:00

116 lines
4.7 KiB
Python

# coding=utf-8
"""
Photo synthetique d'une planche trouee sur la surface de travail.
Sert aux tests (et au schema) : on connait exactement la position des
reperes, du contour et des trous, puis on simule la prise de vue par une
perspective connue, un leger flou et du bruit. Si la chaine retrouve les
cotes a quelques dixiemes de millimetre, l'echelle est respectee.
"""
import os
import sys
import numpy as np
from PIL import Image, ImageDraw, ImageFilter
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import board_photo_core as core # noqa: E402
WIDTH, HEIGHT = 400.0, 400.0 # zone de travail (mm)
ORIGIN = (30.0, 30.0) # centre du repere origine (repere laser)
SPACING = (340.0, 340.0)
MARKER_SIZE = 20.0
BOARD = [(70.0, 90.0), (330.0, 80.0), (340.0, 300.0), (60.0, 310.0)] # repere SVG
HOLES = [((130.0, 150.0), 6.0), ((230.0, 190.0), 12.5), ((290.0, 260.0), 4.0)]
CAMERA = [(300.0, 150.0), (2150.0, 230.0), (2060.0, 1700.0), (370.0, 1610.0)]
PHOTO_SIZE = (2400, 1800)
def markers():
return core.marker_layout(HEIGHT, ORIGIN[0], ORIGIN[1], SPACING[0], SPACING[1])
def render_scene(res=8.0, bed=70, board=205, printed=False):
"""Vue de dessus parfaite, `res` pixels par mm.
`printed` : reperes imprimes colles sur leur gabarit de pose grave (piece
de papier, traits d'alignement, contour et croix du gabarit autour).
"""
size = (int(WIDTH * res), int(HEIGHT * res))
image = Image.new("L", size, bed)
draw = ImageDraw.Draw(image)
def poly(ring, fill):
draw.polygon([(x * res, y * res) for x, y in ring], fill=fill)
# Nid d'abeille schematise : grille claire sur fond sombre.
for k in range(0, int(WIDTH), 8):
draw.line([(k * res, 0), (k * res, size[1])], fill=bed + 35, width=int(res))
draw.line([(0, k * res), (size[0], k * res)], fill=bed + 35, width=int(res))
def lines(polylines, fill, width_mm):
for line in polylines:
draw.line([(x * res, y * res) for x, y in line], fill=fill,
width=max(1, int(round(width_mm * res))))
for index, center in enumerate(markers()):
if printed:
plate = MARKER_SIZE * 1.8
poly([(center[0] - plate, center[1] - plate), (center[0] + plate, center[1] - plate),
(center[0] + plate, center[1] + plate), (center[0] - plate, center[1] + plate)],
215)
lines(core.guide_lines(center, MARKER_SIZE), 90, 0.3)
poly(core.piece_outline(center, MARKER_SIZE)[:-1], 245)
lines(core.piece_ticks(center, MARKER_SIZE), 20, 0.3)
rings = core.marker_rings(center, MARKER_SIZE, origin=index == core.ORIGIN_MARKER)
poly(rings[0], 25)
for dot in rings[1:]:
poly(dot, 245)
continue
half = MARKER_SIZE * 0.85
poly([(center[0] - half, center[1] - half), (center[0] + half, center[1] - half),
(center[0] + half, center[1] + half), (center[0] - half, center[1] + half)], 235)
rings = core.marker_rings(center, MARKER_SIZE, origin=index == core.ORIGIN_MARKER)
poly(rings[0], 25)
for dot in rings[1:]:
poly(dot, 235)
poly(BOARD, board)
for center, radius in HOLES:
poly(core.circle_ring(center, radius, 180), bed)
return image
def camera_matrix():
"""Homographie mm (repere SVG) -> pixels de la photo simulee."""
corners = [(0.0, 0.0), (WIDTH, 0.0), (WIDTH, HEIGHT), (0.0, HEIGHT)]
return core.homography(corners, CAMERA)
def render_photo(seed=0, rotate=0, res=8.0, printed=False):
"""Photo simulee (PIL RGB) ; `rotate` en quarts de tour (appareil tourne)."""
scene = render_scene(res, printed=printed)
to_scene = np.diag([res, res, 1.0]) @ np.linalg.inv(camera_matrix())
to_scene = to_scene / to_scene[2, 2]
photo = scene.transform(PHOTO_SIZE, Image.PERSPECTIVE, tuple(to_scene.ravel()[:8]),
Image.BICUBIC, fillcolor=40)
photo = photo.filter(ImageFilter.GaussianBlur(1.2))
rng = np.random.default_rng(seed)
noisy = np.asarray(photo, dtype=float) + rng.normal(0, 6, (PHOTO_SIZE[1], PHOTO_SIZE[0]))
photo = Image.fromarray(np.clip(noisy, 0, 255).astype(np.uint8)).convert("RGB")
if rotate:
photo = photo.rotate(90 * rotate, expand=True)
return photo
def marker_pixels(rotate=0):
"""Position exacte des reperes dans la photo (ordre HG, HD, BD, BG du repere SVG)."""
points = core.apply_homography(camera_matrix(), markers())
w, h = PHOTO_SIZE
for _ in range(rotate % 4):
# Rotation de 90 deg dans le sens trigonometrique (PIL, expand=True).
points = np.column_stack([points[:, 1], w - points[:, 0]])
w, h = h, w
return [tuple(p) for p in points]