Generalized from the render lanes' proven tools: map segmentation, 2.5D massing by wealth zone, a CPU ray marcher with analytic landmarks and true-scale proxy people, prompt composition from blocks, a self-calibrating batch submitter with a renderer adapter point, the plate-vs-frame gates (drift, ECC, landmark region, skyline and invented structures, figure brackets, corner sweep), matrix job builders, a cost line, and an end-to-end smoke test on a synthetic town.
168 lines
9.5 KiB
Python
168 lines
9.5 KiB
Python
#!/usr/bin/env python3
|
|
"""End-to-end smoke test with no API calls: a synthetic map -> segment -> massing -> plates -> jobs -> gates.
|
|
|
|
usage: smoke_test.py [WORKDIR] (default: a temp dir). Takes about a minute on a laptop at 768 px plates.
|
|
It also proves the gate on a known-bad case: the plate shifted 3 px must score worse than the plate itself.
|
|
"""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
import cv2
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
PY = sys.executable
|
|
|
|
|
|
def run(*args):
|
|
cmd = [PY, os.path.join(HERE, args[0])] + list(args[1:])
|
|
print("$", " ".join(os.path.basename(a) if a.startswith(HERE) else a for a in cmd), flush=True)
|
|
subprocess.run(cmd, check=True, cwd=HERE)
|
|
|
|
|
|
def synthetic_map(path, W=1200, H=900):
|
|
"""Parchment with a river, a grid of dark-roofed houses, tree glyphs; 0.5 m/px."""
|
|
rng = np.random.default_rng(1)
|
|
im = np.full((H, W, 3), (226, 214, 184), np.uint8)
|
|
# river: teal band curving across
|
|
for x in range(W):
|
|
yc = int(450 + 120 * np.sin(x / 260.0))
|
|
im[yc - 18:yc + 18, x] = (90, 150, 150)
|
|
# houses: rows of rectangles with 2-3 px gaps, denser in the middle
|
|
for _ in range(420):
|
|
cx, cy = int(rng.normal(620, 220)), int(rng.normal(420, 160))
|
|
w, h = int(rng.uniform(14, 30)), int(rng.uniform(10, 20))
|
|
if not (10 < cx < W - 40 and 10 < cy < H - 30):
|
|
continue
|
|
if abs(cy - (450 + 120 * np.sin(cx / 260.0))) < 40:
|
|
continue
|
|
col = (120 + rng.integers(0, 30), 95 + rng.integers(0, 25), 70 + rng.integers(0, 20))
|
|
im[cy:cy + h, cx:cx + w] = col
|
|
im[cy + h:cy + h + 2, cx + 2:cx + w + 2] = (170, 165, 160) # cast-shadow halo like a map exporter draws
|
|
# trees: cauliflower glyphs = dark stippled blobs
|
|
for _ in range(60):
|
|
cx, cy = int(rng.uniform(40, W - 40)), int(rng.uniform(40, H - 40))
|
|
r = int(rng.uniform(6, 11))
|
|
for _ in range(60):
|
|
a, d = rng.uniform(0, 2 * np.pi), rng.uniform(0, r)
|
|
x, y = int(cx + d * np.cos(a)), int(cy + d * np.sin(a))
|
|
cv2.circle(im, (x, y), 1, (70, 90, 60), -1)
|
|
Image.fromarray(im).save(path)
|
|
|
|
|
|
def main():
|
|
wd = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else tempfile.mkdtemp(prefix="citykit-smoke-")
|
|
os.makedirs(wd, exist_ok=True)
|
|
print("workdir", wd)
|
|
synthetic_map(os.path.join(wd, "map.png"))
|
|
cfg = dict(town="Smoketown", map=dict(path="map.png", meters_per_px=0.5, origin_px=[600, 450]),
|
|
massing=dict(town_npz="town.npz", zones={"1": dict(eaves_min=2.8, eaves_max=4.2, pitch=0.7),
|
|
"2": dict(eaves_min=4.6, eaves_max=7.2, pitch=0.85)},
|
|
cutouts=[[[570, 430], [630, 430], [630, 470], [570, 470]]]),
|
|
terrain=dict(path=None), world_extent_m=[-800, 800, -800, 800],
|
|
views="views.json", scene="scene.json", plates_dir="plates",
|
|
renderer=dict(model="example-model", quality="high", size="2048x1152",
|
|
cost_usd=dict(per_call_1ref=0.06, per_call_4ref=0.09)),
|
|
gates=dict(landmark_groups="hall"))
|
|
json.dump(cfg, open(os.path.join(wd, "citykit.json"), "w"), indent=1)
|
|
C = os.path.join(wd, "citykit.json")
|
|
run("segment_map.py", os.path.join(wd, "map.png"), "--mpp", "0.5", "--out", os.path.join(wd, "seg.npz"),
|
|
"--look", os.path.join(wd, "seg_look.jpg"))
|
|
run("massing.py", os.path.join(wd, "seg.npz"), "--config", C, "--out", os.path.join(wd, "town.npz"))
|
|
# find open ground for an eye-level camera from the heightfield itself (never by eye on the map)
|
|
Z = np.load(os.path.join(wd, "town.npz"))
|
|
SDF = Z["SDF"].astype(np.float32)
|
|
|
|
def open_spot(X, Y, r_m=20):
|
|
px, py = 600 + X / 0.5, 450 - Y / 0.5
|
|
r = int(r_m / 0.5)
|
|
y0, y1, x0, x1 = int(py) - r, int(py) + r, int(px) - r, int(px) + r
|
|
sub = SDF[y0:y1, x0:x1]
|
|
iy, ix = np.unravel_index(np.argmax(sub), sub.shape)
|
|
return float((x0 + ix - 600) * 0.5), float(-(y0 + iy - 450) * 0.5)
|
|
cx, cy = open_spot(-70, -25, 40)
|
|
yaw = np.degrees(np.arctan2(0 - cx, 0 - cy)) # look at the hall
|
|
fx, fy = np.sin(np.radians(yaw)), np.cos(np.radians(yaw))
|
|
|
|
def sdf_at(X, Y):
|
|
return float(SDF[int(450 - Y / 0.5), int(600 + X / 0.5)])
|
|
|
|
def ahead(dmax, dmin):
|
|
# nearest open point along the look direction between dmin and dmax, with clear ground back to the camera
|
|
for d in np.arange(dmax, dmin, -1.0):
|
|
X, Y = cx + d * fx, cy + d * fy
|
|
if all(sdf_at(cx + k * fx, cy + k * fy) > 1.5 for k in np.arange(0, d + 0.5, 1.0)):
|
|
return X, Y
|
|
return cx + dmin * fx, cy + dmin * fy
|
|
p1 = ahead(24, 11)
|
|
p2 = ahead(32, 14)
|
|
scene = dict(solids=[dict(kind="box", group="hall", mat="stone_lt", c=[0, 0, 7], size=[22, 14, 14], heading=20),
|
|
dict(kind="cone", group="hall", mat="slate", c=[0, 0], r0=6, z0=14, z1=24),
|
|
dict(kind="person", xy=list(p1), heading=yaw, height=1.72, label="walker"),
|
|
dict(kind="person", xy=[p1[0] + 1.2 * fy, p1[1] - 1.2 * fx], heading=yaw, height=1.64, robe=True,
|
|
cloth=2, label="walker"),
|
|
dict(kind="cart", xy=list(p2), heading=yaw, load="hay"),
|
|
dict(kind="tree", xy=[-20, 20], h=8, r=3)],
|
|
landmarks=[dict(name="hall_top", xyz=[0, 0, 24])])
|
|
json.dump(scene, open(os.path.join(wd, "scene.json"), "w"), indent=1)
|
|
views = dict(S_street=dict(pos=[cx, cy, 1.65], yaw=float(yaw), pitch=0.0, hfov=62.0, shift=0.08, sun="day"),
|
|
A_aerial=dict(pos=[-160, -200, 90.0], yaw=35.0, pitch=-12.0, hfov=55.0, shift=0.0, sun="day"))
|
|
json.dump(views, open(os.path.join(wd, "views.json"), "w"), indent=1)
|
|
prompts = json.load(open(os.path.join(HERE, "..", "examples", "prompts.example.json")))
|
|
prompts["views"] = {"S_street": dict(eye_level=True, light="day", refs=[], intro="IMAGE 1 is a block model of a street.",
|
|
keep="KEEP EXACTLY everything.", frame=""),
|
|
"A_aerial": dict(eye_level=False, light="day", refs=[], intro="IMAGE 1 is a block model from above.",
|
|
keep="KEEP EXACTLY every footprint.", frame="")}
|
|
json.dump(prompts, open(os.path.join(wd, "prompts.json"), "w"), indent=1)
|
|
run("render_plate.py", "S_street", "A_aerial", "--config", C, "--w", "768")
|
|
# the gate needs <V>_mass.png etc. at the plate name without a size suffix: rename the 768 plates
|
|
pd = os.path.join(wd, "plates")
|
|
for f in os.listdir(pd):
|
|
if "_768" in f:
|
|
os.replace(os.path.join(pd, f), os.path.join(pd, f.replace("_768", "")))
|
|
run("build_jobs.py", "day", "--config", C, "--prompts", os.path.join(wd, "prompts.json"), "--takes", "2",
|
|
"--out", os.path.join(wd, "jobs_day.json"))
|
|
# fake "takes": the plate itself (known good) and the plate shifted 3 px / scaled 1.01 (known bad)
|
|
out = os.path.join(wd, "out_day")
|
|
os.makedirs(out, exist_ok=True)
|
|
for v in views:
|
|
p = cv2.imread(os.path.join(pd, v + "_mass.png"))
|
|
cv2.imwrite(os.path.join(out, v + "_day_t1.png"), p)
|
|
M = np.float32([[1.01, 0, 3 - 0.005 * p.shape[1]], [0, 1.01, 3 - 0.005 * p.shape[0]]])
|
|
cv2.imwrite(os.path.join(out, v + "_day_t2.png"), cv2.warpAffine(p, M, (p.shape[1], p.shape[0]), borderMode=cv2.BORDER_REPLICATE))
|
|
run("gate.py", out, "--config", C)
|
|
G = json.load(open(os.path.join(out, "qc", "gate_out_day.json")))
|
|
for v in views:
|
|
good, bad = G[v + "_day_t1"], G[v + "_day_t2"]
|
|
print(v, "good: grad %.3f scale %s | bad: grad %.3f scale %s shift %s/%s" % (
|
|
good["grad_corr"], good.get("scale"), bad["grad_corr"], bad.get("scale"), bad.get("tx_px"), bad.get("ty_px")))
|
|
assert good["grad_corr"] > 0.99, "plate vs itself must score ~1"
|
|
assert bad["grad_corr"] < good["grad_corr"], "the shifted plate must score lower"
|
|
assert bad.get("scale") is None or abs(bad["scale"] - 1.0) > 0.004, "ECC must see the 1 % zoom"
|
|
assert G["S_street_day_t1"].get("proxies_visible", 0) >= 1, "street view must see proxy people"
|
|
assert G["A_aerial_day_t1"].get("sky_res") is not None, "aerial with sky must get a skyline fit"
|
|
assert G["A_aerial_day_t1"].get("invented_structures_n") == 0, "plate vs itself invents nothing"
|
|
ledger = os.path.join(out, "results.jsonl")
|
|
with open(ledger, "w") as f:
|
|
for v in views:
|
|
f.write(json.dumps(dict(id=v + "_day_t1", status=200, secs=40, refs=1)) + "\n")
|
|
f.write(json.dumps(dict(id=v + "_day_t2", status=200, secs=38, refs=2)) + "\n")
|
|
run("cost.py", ledger, "--config", C)
|
|
picks = {v: dict(day=v + "_day_t1") for v in views}
|
|
json.dump(picks, open(os.path.join(wd, "picks.json"), "w"))
|
|
run("qc_sheet.py", "--config", C, "--picks", os.path.join(wd, "picks.json"), "--gates",
|
|
os.path.join(out, "qc", "gate_out_day.json"), "--frames", out, "--out", os.path.join(wd, "qc_plates.jpg"))
|
|
run("matrix_jobs.py", "weather", "--config", C, "--prompts", os.path.join(wd, "prompts.json"), "--tier", "1",
|
|
"S_street=" + os.path.join(out, "S_street_day_t1.png"), "--out", os.path.join(wd, "jobs_matrix.json"))
|
|
run("matrix_jobs.py", "population", "--config", C, "--prompts", os.path.join(wd, "prompts.json"), "--tier", "2",
|
|
"S_street=" + os.path.join(out, "S_street_day_t1.png"), "--out", os.path.join(wd, "jobs_pop.json"))
|
|
print("SMOKE TEST PASSED in", wd)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|