174 lines
8.3 KiB
Python
174 lines
8.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Realistischere synthetische Gesichter (fuer stabile Detektions-Scores).
|
|
|
|
Der erste Entwurf (tests/make_fixtures.py, flache Cartoon-Gesichter) wird von
|
|
SCRFD nur mit ~0.11 Score und nur ueber den Low-Threshold-Fallback erkannt.
|
|
Damit sind Tracking-/Stabilitaetsmessungen nicht aussagekraeftig.
|
|
|
|
Dieses Modul rendert Gesichter per Hoehenfeld + Lambert-Shading:
|
|
1. Kopf/Hals als Distanzfeld aus ueberlagerten Ellipsen
|
|
2. Normalen -> Lambert-Shading fuer plastische Oberflaeche
|
|
3. Gesichtszuege (Brauen, Augen, Nase, Lippen) mit Schatten/Kanten
|
|
4. feine Hauttextur + deterministisches Rauschen
|
|
Ziel: det_score > 0.85, damit die Tests den echten Erkennungs- statt den
|
|
Fallback-Pfad messen.
|
|
"""
|
|
import numpy as np
|
|
import cv2
|
|
|
|
|
|
def _ellipse_heightmap(h, w, cx, cy, ax, ay, ang=0.0, amp=1.0, power=0.65):
|
|
"""Liefert (height, dom) als float32; dom = 1 innerhalb der Ellipse."""
|
|
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
|
|
ca, sa = np.cos(np.deg2rad(ang)), np.sin(np.deg2rad(ang))
|
|
dx, dy = xx - cx, yy - cy
|
|
ex = dx * ca + dy * sa
|
|
ey = -dx * sa + dy * ca
|
|
r = np.sqrt((ex / max(ax, 1e-3)) ** 2 + (ey / max(ay, 1e-3)) ** 2)
|
|
# glattes Hoehenprofil: Maximum in der Mitte, weich auslaufend
|
|
prof = np.clip(1.0 - r, 0.0, 1.0) ** power
|
|
height = prof * amp
|
|
dom = np.clip((1.0 - r) * 6.0, 0.0, 1.0)
|
|
return height, dom
|
|
|
|
|
|
def render_face(size=(400, 400), skin_bgr=(168, 190, 224), bg_bgr=(52, 66, 92), seed=0):
|
|
"""Rendert ein schattenvolles, frontal-symmetrisches Kunst-Gesicht."""
|
|
h, w = size
|
|
rng = np.random.default_rng(seed)
|
|
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
|
|
|
|
cx, cy = w * 0.5, h * 0.545
|
|
ax, ay = w * 0.255, h * 0.325
|
|
|
|
# --- 1) Hoehenfeld: Kopf, Wangen, Stirn, Kiefer, Nase, Hals ---
|
|
height = np.zeros((h, w), np.float32)
|
|
dom = np.zeros((h, w), np.float32)
|
|
|
|
def add(c, y, a, b, amp, ang=0.0, power=0.65):
|
|
nonlocal height, dom
|
|
hh, dd = _ellipse_heightmap(h, w, c, y, a, b, ang, amp, power=power)
|
|
height = np.maximum(height, hh)
|
|
dom = np.maximum(dom, dd)
|
|
|
|
add(cx, cy, ax, ay, 1.00) # Kopf
|
|
add(cx, cy - ay * 0.35, ax * 0.86, ay * 0.72, 0.92) # Stirn
|
|
add(cx, cy + ay * 0.10, ax * 0.90, ay * 0.62, 0.95) # Wangen/Kiefer
|
|
add(cx, cy + ay * 0.62, ax * 0.42, ay * 0.52, 0.80) # Hals
|
|
add(cx, cy - ay * 0.02, ax * 0.17, ay * 0.30, 1.14) # Nasenruecken (kippt nach vorn)
|
|
add(cx, cy + ay * 0.30, ax * 0.10, ay * 0.09, 1.10) # Nasenspitze
|
|
for sx in (-1, 1): # Wangenknochen
|
|
add(cx + sx * ax * 0.62, cy - ay * 0.02, ax * 0.26, ay * 0.24, 1.04)
|
|
add(cx, cy + ay * 0.46, ax * 0.52, ay * 0.20, 1.00) # Unterlippe/Kinn
|
|
|
|
# weicher Rand der Gesichtsform (Haarlinie dunkel)
|
|
edge = np.clip(1.0 - np.abs(np.gradient(dom)[0]) * 40.0, 0, 1)
|
|
edge = np.maximum(edge, np.clip(1.0 - np.abs(np.gradient(dom)[1]) * 40.0, 0, 1))
|
|
|
|
# --- 2) Normalen + Lambert-Shading (Licht von links oben) ---
|
|
gy, gx = np.gradient(height * 26.0)
|
|
nz = np.ones_like(gx)
|
|
nlen = np.sqrt(gx * gx + gy * gy + nz * nz)
|
|
nx, ny, nz = -gx / nlen, -gy / nlen, nz / nlen
|
|
lx, ly, lz = -0.45, -0.55, 0.70
|
|
lam = np.clip(nx * lx + ny * ly + nz * lz, 0, 1)
|
|
shade = 0.42 + 0.72 * lam
|
|
|
|
skin = np.array(skin_bgr, np.float32).reshape(1, 1, 3)
|
|
img = skin * shade[:, :, np.newaxis]
|
|
img = img * dom[:, :, np.newaxis] + np.array(bg_bgr, np.float32).reshape(1, 1, 3) * (1 - dom[:, :, np.newaxis])
|
|
|
|
# --- 3) Gesichtszuege ---
|
|
def px(v): # Prozent der Kopfbreite
|
|
return v * ax
|
|
|
|
eye_y = cy - ay * 0.16
|
|
eye_dx = px(0.42)
|
|
eye_rx, eye_ry = px(0.20), ay * 0.105
|
|
for sx in (-1, 1):
|
|
ex = cx + sx * eye_dx
|
|
# Augenhohle (Schattenmulde)
|
|
cv2.ellipse(img, (int(ex), int(eye_y)), (int(eye_rx * 1.5), int(eye_ry * 1.7)), 0, 0, 360,
|
|
tuple(float(c) * 0.80 for c in skin_bgr), -1)
|
|
# Braue
|
|
cv2.ellipse(img, (int(ex), int(eye_y - ay * 0.19)), (int(eye_rx * 1.5), int(ay * 0.035)), 0,
|
|
185, 355, (52, 46, 42), -1, lineType=cv2.LINE_AA)
|
|
# Weiss sclera + Iris + Pupil + Glanz
|
|
cv2.ellipse(img, (int(ex), int(eye_y)), (int(eye_rx), int(eye_ry)), 0, 0, 360, (238, 238, 240), -1)
|
|
ir = max(2, int(min(eye_rx, eye_ry) * 0.80))
|
|
cv2.circle(img, (int(ex), int(eye_y)), ir, (78, 92, 58), -1, lineType=cv2.LINE_AA)
|
|
cv2.circle(img, (int(ex), int(eye_y)), max(1, int(ir * 0.48)), (18, 18, 22), -1, lineType=cv2.LINE_AA)
|
|
cv2.circle(img, (int(ex - ir * 0.32), int(eye_y - ir * 0.34)), max(1, int(ir * 0.26)),
|
|
(250, 250, 250), -1, lineType=cv2.LINE_AA)
|
|
# Lidkante oben/unten
|
|
cv2.ellipse(img, (int(ex), int(eye_y)), (int(eye_rx), int(eye_ry)), 0, 180, 360, (40, 36, 34), 2, cv2.LINE_AA)
|
|
cv2.ellipse(img, (int(ex), int(eye_y)), (int(eye_rx), int(eye_ry)), 0, 0, 180, (58, 52, 50), 2, cv2.LINE_AA)
|
|
|
|
# Nase: dunkle Nasenfluegel + Nasenlocher
|
|
ny_ = cy + ay * 0.30
|
|
cv2.ellipse(img, (int(cx), int(ny_)), (int(px(0.30)), int(ay * 0.10)), 0, 0, 180,
|
|
tuple(float(c) * 0.74 for c in skin_bgr), -1)
|
|
for sx in (-1, 1):
|
|
cv2.ellipse(img, (int(cx + sx * px(0.17)), int(ny_ + ay * 0.01)), (int(px(0.075)), int(ay * 0.030)),
|
|
0, 0, 360, (46, 40, 44), -1, cv2.LINE_AA)
|
|
|
|
# Mund: Lippen mit Mittelteil + Schatten unter der Unterlippe
|
|
my = cy + ay * 0.56
|
|
cv2.ellipse(img, (int(cx), int(my)), (int(px(0.46)), int(ay * 0.115)), 0, 0, 360,
|
|
(112, 96, 150), -1, lineType=cv2.LINE_AA)
|
|
cv2.ellipse(img, (int(cx), int(my + ay * 0.020)), (int(px(0.44)), int(ay * 0.045)), 0, 0, 180,
|
|
(78, 66, 112), -1, lineType=cv2.LINE_AA)
|
|
cv2.ellipse(img, (int(cx), int(my - ay * 0.028)), (int(px(0.40)), int(ay * 0.038)), 0, 180, 360,
|
|
(134, 112, 168), -1, lineType=cv2.LINE_AA)
|
|
cv2.line(img, (int(cx - px(0.40)), int(my)), (int(cx + px(0.40)), int(my)), (62, 52, 88), 2, cv2.LINE_AA)
|
|
|
|
# Philtrum
|
|
cv2.line(img, (int(cx), int(ny_ + ay * 0.10)), (int(cx), int(my - ay * 0.10)),
|
|
tuple(float(c) * 0.86 for c in skin_bgr), 2, cv2.LINE_AA)
|
|
|
|
# Haar: dunkle Kappe ueber der Stirn + Seiten
|
|
cv2.ellipse(img, (int(cx), int(cy - ay * 0.42)), (int(ax * 1.10), int(ay * 0.72)), 0, 182, 358,
|
|
(38, 33, 42), -1, lineType=cv2.LINE_AA)
|
|
cv2.ellipse(img, (int(cx), int(cy - ay * 0.52)), (int(ax * 1.13), int(ay * 0.62)), 0, 180, 360,
|
|
(30, 26, 34), -1, lineType=cv2.LINE_AA)
|
|
for sx in (-1, 1):
|
|
cv2.ellipse(img, (int(cx + sx * ax * 1.02), int(cy + ay * 0.02)), (int(ax * 0.14), int(ay * 0.52)),
|
|
sx * 6, 0, 360, (34, 29, 38), -1, lineType=cv2.LINE_AA)
|
|
|
|
# Ohren
|
|
for sx in (-1, 1):
|
|
cv2.ellipse(img, (int(cx + sx * ax * 0.99), int(cy + ay * 0.06)), (int(ax * 0.10), int(ay * 0.17)),
|
|
0, 0, 360, tuple(float(c) * 0.90 for c in skin_bgr), -1, lineType=cv2.LINE_AA)
|
|
|
|
# --- 4) Hauttextur + Rauschen (deterministisch) ---
|
|
img = cv2.GaussianBlur(img, (0, 0), 0.7)
|
|
img += rng.normal(0, 3.2, img.shape).astype(np.float32)
|
|
img = np.clip(img, 0, 255).astype(np.uint8)
|
|
|
|
# sanftes vignettieren wie bei einer Kamera
|
|
r = np.sqrt(((xx - w / 2) / (w / 2)) ** 2 + ((yy - h / 2) / (h / 2)) ** 2)
|
|
img = np.clip(img.astype(np.float32) * (1.0 - 0.10 * np.clip(r - 0.55, 0, None) ** 2)[:, :, np.newaxis],
|
|
0, 255).astype(np.uint8)
|
|
return img
|
|
|
|
|
|
def render_scene(size=(480, 360), bg_bgr=(58, 86, 62), seed=1, face_size=(400, 400),
|
|
skin_bgr=(198, 186, 160), jitter=(0, 0)):
|
|
"""Szene mit eingebettetem Kunst-Gesicht."""
|
|
w, h = size
|
|
face = render_face(face_size, skin_bgr=skin_bgr, seed=seed)
|
|
fh, fw = face.shape[:2]
|
|
ox = (w - fw) // 2 + int(jitter[0])
|
|
oy = (h - fh) // 2 + int(jitter[1])
|
|
bg = np.full((h, w, 3), bg_bgr, np.uint8)
|
|
# leichter Verlauf im Hintergrund
|
|
grad = np.linspace(0.82, 1.10, w, dtype=np.float32).reshape(1, w, 1)
|
|
bg = np.clip(bg.astype(np.float32) * grad, 0, 255).astype(np.uint8)
|
|
sx0, sy0 = max(0, ox), max(0, oy)
|
|
ex0, ey0 = min(w, ox + fw), min(h, oy + fh)
|
|
if ex0 > sx0 and ey0 > sy0:
|
|
bg[sy0:ey0, sx0:ex0] = face[sy0 - oy:ey0 - oy, sx0 - ox:ex0 - ox]
|
|
bg = np.clip(bg.astype(np.float32) + np.random.default_rng(seed).normal(0, 2.0, bg.shape), 0, 255).astype(np.uint8)
|
|
return bg
|