Toolkit: segment, mass, render plates, batch, gate, matrices, cost, smoke test

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.
This commit is contained in:
2026-10-14 11:39:57 +00:00
parent 6c4ddfa523
commit 3d50023cfa
15 changed files with 2309 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
__pycache__/
*.pyc
.venv/
*.npz
*.npy
plates/
out_*/
smoke/
+4
View File
@@ -0,0 +1,4 @@
numpy>=1.24
opencv-python-headless>=4.8
Pillow>=10.0
requests>=2.31
+30
View File
@@ -0,0 +1,30 @@
# Renderer adapters
`render_batch.py` talks to the renderer through one function:
```python
def submit(job, refs, spec, env):
"""job: one entry of jobs.json (id, prompt, size, refs, prompt_alt)
refs: absolute paths of the reference images, IMAGE 1 first (the plate), then style/material refs
spec: the jobs.json header (model, quality, ...)
env: {"key": IMAGE_API_KEY, "base": IMAGE_API_BASE}
returns (status_code, png_bytes_or_None, error_body_text, response_headers_or_{})"""
```
The built-in `openai_images` adapter posts to an OpenAI-compatible `/images/edits` endpoint (multipart, `image[]`
for every reference, `prompt`, `size`, `quality`, `output_format=png`). Jobs with no references go to
`/images/generations`.
What the method needs from a renderer, in order of importance:
1. **Image-conditioned editing with several references** where the first image is treated as the scene to re-surface
and the others as material/style references. The plate must dominate the layout.
2. **A 16:9 output at 2048x1152 or similar** so the plate-to-frame registration is pixel-comparable.
3. **No seed and no strength dial is fine.** The method never relied on either: geometry is held by the plate and the
prompt, variance is handled by rendering 2-4 takes and gating.
4. **A rate-limit signal** (HTTP 429 with Retry-After, or any 5xx) so the batch runner can calibrate concurrency.
Return 429/5xx for "try later" conditions and any other non-200 for a hard failure. A body containing
`safety`, `moderation`, `content_policy` or `rejected` triggers the one allowed rephrase (`prompt_alt`).
Drop your adapter in this directory as `<name>.py` and run `render_batch.py --adapter <name>`.
+84
View File
@@ -0,0 +1,84 @@
#!/usr/bin/env python3
"""Compose re-surfacing prompts per camera from prompt blocks and write a jobs.json for render_batch.py.
Two modes:
day IMAGE 1 = the plate; the prompt = [view.intro] + RESURF + [view.keep] + [setting blocks] + [view.frame]
+ PEOPLE + [EYELINE if the camera is at eye level] + LIGHT + NOMODERN. One job per take.
relight IMAGE 1 = an accepted day take ("keeper"); the prompt repaints ONLY the light (sunset, night, morning).
Geometry carries over, which is why sunset/night cells are relights of the Day pick and never fresh edits.
prompts.json holds the blocks (see examples/prompts.example.json): the generic ones (resurface, people, nomodern,
eyeline, lights) and the per-view ones (intro, keep, frame, refs, eye_level). Everything that names your world
(the setting, the landmarks, the materials by wealth zone) is yours to write; references/prompting.md explains
each block's job and the wording that measurably held.
usage: build_jobs.py day --config citykit.json --prompts prompts.json [--views A,B] [--takes 3] [--tag day] --out jobs_day.json
build_jobs.py relight --config citykit.json --prompts prompts.json --light sunset V=keeper.png ... [--takes 3] --out jobs_sunset.json
Also writes <out>_PROMPTS.md with the full text per view, for the lane report.
"""
import argparse
import json
import os
from citykit import load_config, resolve
def day_jobs(cfg, P, views, takes, tag):
jobs, md = [], []
plates = resolve(cfg, cfg.get("plates_dir", "plates"))
for v in views:
s = P["views"][v]
parts = [s.get("intro", ""), P["resurface"], s.get("keep", "")]
parts += [P[b] for b in s.get("setting_blocks", P.get("default_setting_blocks", []))]
if s.get("frame"):
parts.append(s["frame"])
parts.append(P["people"])
if s.get("eye_level", False):
parts.append(P["eyeline"])
parts.append(P["lights"][s.get("light", "day")])
parts.append(P["nomodern"])
prompt = " ".join(p for p in parts if p)
refs = [os.path.join(plates, v + "_mass.png")] + [resolve(cfg, r) for r in s.get("refs", [])]
md.append("## %s\n\nrefs: %s\n\n%s\n" % (v, refs, prompt))
for t in range(1, takes + 1):
jobs.append(dict(id="%s_%s_t%d" % (v, tag, t), size=cfg["renderer"].get("size", "2048x1152"), refs=refs,
prompt=prompt + " [view %s, %s, take %d]" % (v, tag, t)))
return jobs, md
def relight_jobs(cfg, P, keepers, light, takes):
jobs, md = [], []
for v, path in keepers.items():
s = P["views"].get(v, {})
prompt = P["relights"][s.get("relight_variant", {}).get(light, light)] + " " + P["nomodern"]
md.append("## %s %s\n\nrefs: %s\n\n%s\n" % (v, light, path, prompt))
for t in range(1, takes + 1):
jobs.append(dict(id="%s_%s_t%d" % (v, light, t), size=cfg["renderer"].get("size", "2048x1152"),
refs=[os.path.abspath(path)], prompt=prompt + " [view %s, %s, take %d]" % (v, light, t)))
return jobs, md
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("mode", choices=["day", "relight"])
ap.add_argument("rest", nargs="*", help="relight: V=keeper.png pairs")
ap.add_argument("--config", required=True)
ap.add_argument("--prompts", required=True)
ap.add_argument("--views", default=None, help="comma list; default = every view in prompts.json")
ap.add_argument("--takes", type=int, default=3)
ap.add_argument("--tag", default="day")
ap.add_argument("--light", default="sunset")
ap.add_argument("--out", required=True)
a = ap.parse_intermixed_args()
cfg = load_config(a.config)
P = json.load(open(a.prompts))
if a.mode == "day":
views = a.views.split(",") if a.views else list(P["views"])
jobs, md = day_jobs(cfg, P, views, a.takes, a.tag)
else:
keep = dict(x.split("=", 1) for x in a.rest if "=" in x)
jobs, md = relight_jobs(cfg, P, keep, a.light, a.takes)
R = cfg["renderer"]
json.dump(dict(model=R["model"], quality=R.get("quality", "high"), refs_common=[], jobs=jobs), open(a.out, "w"), indent=1)
open(os.path.splitext(a.out)[0] + "_PROMPTS.md", "w").write("\n".join(md))
print(len(jobs), "jobs ->", a.out)
+76
View File
@@ -0,0 +1,76 @@
"""Shared helpers for the city render toolkit: config loading, world/map transforms, image utilities.
The world frame is meters: X east, Y north, Z up. Z = 0 is the town's ground plane.
A map bitmap maps to the world through `meters_per_px` and `origin_px` (the bitmap pixel that sits at world 0,0).
Row index grows southward, so world Y = -(py - origin_py) * meters_per_px.
"""
import json
import os
import numpy as np
def load_config(path):
with open(path) as f:
cfg = json.load(f)
cfg["_dir"] = os.path.dirname(os.path.abspath(path))
return cfg
def resolve(cfg, p):
"""Resolve a path from the config relative to the config file's directory."""
if p is None:
return None
return p if os.path.isabs(p) else os.path.join(cfg["_dir"], p)
class MapFrame:
"""Pixel <-> world transforms for one bitmap."""
def __init__(self, meters_per_px, origin_px=(0.0, 0.0)):
self.mpp = float(meters_per_px)
self.ox, self.oy = float(origin_px[0]), float(origin_px[1])
def px_to_world(self, px, py):
return (np.asarray(px, float) - self.ox) * self.mpp, -(np.asarray(py, float) - self.oy) * self.mpp
def world_to_px(self, X, Y):
return np.asarray(X, float) / self.mpp + self.ox, -np.asarray(Y, float) / self.mpp + self.oy
def ell(k):
import cv2
k = max(3, int(k)) | 1
return cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (k, k))
def h01(*a):
"""Deterministic hash in [0, 1) from any printable arguments (stable across runs)."""
import hashlib
return int(hashlib.md5(repr(a).encode()).hexdigest()[:8], 16) / 0xFFFFFFFF
def bil(A, r, c):
"""Bilinear sample of a 2D array at fractional (row, col); clamps to the edges."""
hh, ww = A.shape
r = np.clip(r, 0, hh - 1.001)
c = np.clip(c, 0, ww - 1.001)
r0 = r.astype(np.int32)
c0 = c.astype(np.int32)
fr = (r - r0).astype(np.float32)
fc = (c - c0).astype(np.float32)
return ((A[r0, c0] * (1 - fc) + A[r0, c0 + 1] * fc) * (1 - fr)
+ (A[r0 + 1, c0] * (1 - fc) + A[r0 + 1, c0 + 1] * fc) * fr)
def save_json(obj, path):
def conv(v):
if hasattr(v, "tolist"):
return v.tolist()
if isinstance(v, dict):
return {k: conv(x) for k, x in v.items()}
if isinstance(v, (list, tuple)):
return [conv(x) for x in v]
return v
with open(path, "w") as f:
json.dump(conv(obj), f, indent=1)
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
"""Sum the render ledgers into the Cost line every run report must carry.
usage: cost.py --config citykit.json out_day/results.jsonl out_sunset/results.jsonl ... [--gpu-minutes 0]
Prices come from config.renderer.cost_usd: {"per_call_1ref": 0.06, "per_call_4ref": 0.09} (interpolated by the
number of references, measured 2026-10 on one hosted model at 2048x1152 high quality; set your own).
Counts every call, accepted and rejected, because volume is what costs money, not the unit price.
"""
import argparse
import json
from citykit import load_config
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("ledgers", nargs="+")
ap.add_argument("--config", required=True)
ap.add_argument("--gpu-minutes", type=float, default=0.0)
a = ap.parse_args()
cfg = load_config(a.config)
c = cfg["renderer"].get("cost_usd", {"per_call_1ref": 0.06, "per_call_4ref": 0.09})
c1, c4 = float(c["per_call_1ref"]), float(c["per_call_4ref"])
calls = ok = fail = 0
usd = 0.0
secs = 0.0
for p in a.ledgers:
for line in open(p):
if not line.strip():
continue
row = json.loads(line)
calls += 1
secs += row.get("secs", 0)
if row.get("status") == 200:
ok += 1
n = row.get("refs", 1)
usd += c1 + (c4 - c1) * min(max(n - 1, 0), 3) / 3.0
else:
fail += 1
print("Cost: %d renderer calls (%d accepted, %d failed/retried), about $%.2f; %.1f render-minutes of API time; "
"%.0f local GPU-minutes." % (calls, ok, fail, usd, secs / 60, a.gpu_minutes))
+372
View File
@@ -0,0 +1,372 @@
#!/usr/bin/env python3
"""Gates for re-surfaced frames against their template plates. One JSON per batch + check sheets.
Per frame <V>_<pass>_t<k>.png in OUTDIR:
GEOMETRY grad_corr Pearson correlation of gradient magnitude, frame vs plate (896x504, sigma 2). Structure kept.
edge_rec share of plate Canny edges with a frame edge within 4 px.
ecc_cc, scale, rot_deg, tx_px, ty_px affine ECC frame->plate on gradient magnitude. Reframing shows
up as scale != 1 (zoom) or a shift of more than a few px. A take that zoomed 4 % fails here.
landmark_grad_corr the same gradient correlation inside the landmark mask (the solids of the named
groups), which is where the renderer invents palaces: a castle redrawn as a palace scored 0.16 while
faithful takes scored 0.4-0.7 at 3 km.
SKYLINE sky_s/sky_tx/sky_ty/sky_res similarity fit of the frame's sky/ground boundary to the plate's (landscape
frames, where smooth surfaces make grad_corr run low by nature).
invented_structures runs of columns where the frame skyline rises more than --bump px above the
plate's outside any landmark: the "invented large building" gate for wide town views.
FIGURES every proxy person visible in the plate (from <V>_people.json and the oid buffer) drawn as a bracket on
qc/<id>_figs.jpg: foot line, true-height head line, and the +-25 % band. A dressed figure whose head is
outside the band fails by eye; qc/<id>_figzoom.jpg crops the 16 largest for the check.
CORNERS four corners + top/bottom edge bands of every frame, contrast-boosted, on one sheet per position
(qc/CORNERS-<tag>-<pos>.jpg): the text/watermark/logo sweep.
For relit frames (<V>_<light>_t<k>) the reference is the day keeper from --keepers JSON, not the plate.
Thresholds are reported, not enforced: the numbers depend on the view (a textured facade at 20 m scores 0.5-0.7,
a smooth landscape 0.15-0.3), so calibrate per view on a known-good take and a known-bad one (shift the plate 3 px,
scale it 1.01) before trusting a cut-off. references/gates.md gives the measured bands.
usage: gate.py OUTDIR --config citykit.json [--keepers keepers.json] [--landmark-groups castle,library] [ids...]
"""
import argparse
import glob
import json
import os
import re
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
from citykit import load_config, resolve
try:
FB = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 20)
except OSError:
FB = ImageFont.load_default()
def view_of(fid):
return re.sub(r"_[A-Za-z0-9]+_t\d+$", "", fid)
def gmag(img):
g = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float32), (0, 0), 2)
return np.hypot(cv2.Sobel(g, cv2.CV_32F, 1, 0), cv2.Sobel(g, cv2.CV_32F, 0, 1))
def edges(img):
g = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (0, 0), 1.5)
v = np.median(g)
return cv2.Canny(g, int(max(0, 0.66 * v)), int(min(255, 1.33 * v))) > 0
def drift(plate, frame):
s = cv2.resize(plate, (896, 504), interpolation=cv2.INTER_AREA)
o = cv2.resize(frame, (896, 504), interpolation=cv2.INTER_AREA)
gs, go = gmag(s).ravel(), gmag(o).ravel()
gc = float(np.corrcoef(gs, go)[0, 1]) if gs.std() > 1e-6 and go.std() > 1e-6 else 0.0
es, eo = edges(s), edges(o)
k = np.ones((9, 9), np.uint8)
rec = float((cv2.dilate(eo.astype(np.uint8), k)[es] > 0).mean()) if es.any() else 0.0
return dict(grad_corr=round(gc, 3), edge_rec=round(rec, 3))
def gm_norm(img):
g = cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float32), (0, 0), 2.5)
m = np.hypot(cv2.Sobel(g, cv2.CV_32F, 1, 0), cv2.Sobel(g, cv2.CV_32F, 0, 1))
return cv2.GaussianBlur(m / (m.mean() + 1e-6), (0, 0), 2.0)
def ecc(plate, frame):
a = gm_norm(cv2.resize(plate, (1024, 576), interpolation=cv2.INTER_AREA))
b = gm_norm(cv2.resize(frame, (1024, 576), interpolation=cv2.INTER_AREA))
w = np.eye(2, 3, dtype=np.float32)
try:
cc, w = cv2.findTransformECC(a.copy(), b.copy(), w, cv2.MOTION_AFFINE,
(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 200, 1e-6), None, 5)
except cv2.error:
return dict(ecc_cc=None)
sx = float(np.hypot(w[0, 0], w[1, 0]))
sy = float(np.hypot(w[0, 1], w[1, 1]))
W2 = w.copy()
W2[:, 2] *= plate.shape[1] / 1024.0
return dict(_warp=W2.tolist(), ecc_cc=round(float(cc), 3), scale=round((sx + sy) / 2, 4),
rot_deg=round(float(np.degrees(np.arctan2(w[1, 0], w[0, 0]))), 2),
tx_px=round(float(w[0, 2]) * plate.shape[1] / 1024.0, 1),
ty_px=round(float(w[1, 2]) * plate.shape[1] / 1024.0, 1))
def region_corr(plate, frame, mask):
a = gmag(plate)
b = gmag(frame)
m = mask > 0
if m.sum() < 60 or a[m].std() < 1e-6 or b[m].std() < 1e-6:
return None
return round(float(np.corrcoef(a[m], b[m])[0, 1]), 3)
def plate_skyline(cls):
sky = cls == 0
first = np.argmax(~sky, axis=0).astype(float)
first[sky.all(0)] = np.nan
return first
def frame_skyline(bgr):
b, g, r = [bgr[..., i].astype(np.float32) for i in range(3)]
lum = (b + g + r) / 3
sky = (b > r + 8) & (lum > 120)
sky |= (lum > 205) & (np.abs(b - r) < 30)
sky = cv2.morphologyEx(sky.astype(np.uint8), cv2.MORPH_OPEN, np.ones((5, 5), np.uint8)) > 0
H, W = sky.shape
first = np.full(W, np.nan)
for x in range(W):
col = sky[:, x]
k = np.argmin(col) if not col.all() else -1
if k > 3:
first[x] = k
return first
def skyline_fit(ps, fs, cx, cy):
xs = np.arange(len(ps))
best = (1e9, 1.0, 0.0, 0.0)
for s in np.arange(0.92, 1.161, 0.004):
for tx in np.arange(-150, 151, 6):
xp = np.round(s * (xs - cx) + cx + tx).astype(int)
ok = (xp >= 0) & (xp < len(fs)) & ~np.isnan(ps)
if ok.sum() < 300:
continue
fy = fs[xp[ok]]
py = s * (ps[ok] - cy) + cy
m = ~np.isnan(fy)
if m.sum() < 300:
continue
d = fy[m] - py[m]
ty = np.median(d)
res = np.median(np.abs(d - ty))
if res < best[0]:
best = (res, s, tx, ty)
res, s, tx, ty = best
return dict(sky_s=round(float(s), 4), sky_tx=round(float(tx), 1), sky_ty=round(float(ty), 1), sky_res=round(float(res), 2))
def invented_structures(ps, fs, lm_mask, bump_px=18, min_run=12):
"""Columns where the frame's skyline is higher (smaller row) than the plate's by > bump_px, outside landmarks."""
W = len(ps)
lm_cols = (lm_mask > 0).any(0) if lm_mask is not None else np.zeros(W, bool)
lm_cols = cv2.dilate(lm_cols.astype(np.uint8)[None], np.ones((1, 15), np.uint8))[0] > 0
d = ps - fs
up = np.nan_to_num(d, nan=0.0) > bump_px
up &= ~lm_cols
runs = []
x = 0
while x < W:
if up[x]:
x1 = x
while x1 < W and up[x1]:
x1 += 1
if x1 - x >= min_run:
runs.append([int(x), int(x1), int(np.nanmax(d[x:x1]))])
x = x1
else:
x += 1
return runs
def boost(bgr):
lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
lab[..., 0] = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8, 8)).apply(lab[..., 0])
out = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
return cv2.addWeighted(out, 1.6, cv2.GaussianBlur(out, (0, 0), 3), -0.6, 0)
def corners(frames, tag, qc):
if not frames:
return
H, W = frames[0][1].shape[:2]
cw, ch = W // 6, H // 6
regs = {"TL": (0, 0, cw, ch), "TR": (W - cw, 0, cw, ch), "BL": (0, H - ch, cw, ch), "BR": (W - cw, H - ch, cw, ch),
"TOP": (W // 4, 0, W // 2, H // 16), "BOT": (W // 4, H - H // 16, W // 2, H // 16)}
for nm, (x0, y0, CW, CH) in regs.items():
S = 1.4 if CW < W // 3 else 0.62
pw, ph = int(CW * S), int(CH * S)
cols = 4 if CW < W // 3 else 2
rows = (len(frames) + cols - 1) // cols
sh = Image.new("RGB", (cols * (pw + 2) + 4, rows * (ph + 4)), (0, 0, 0))
d = ImageDraw.Draw(sh)
for i, (fid, im) in enumerate(frames):
c = cv2.resize(boost(im[y0:y0 + CH, x0:x0 + CW]), (pw, ph), interpolation=cv2.INTER_CUBIC)
xy = ((i % cols) * (pw + 2), (i // cols) * (ph + 4))
sh.paste(Image.fromarray(cv2.cvtColor(c, cv2.COLOR_BGR2RGB)), xy)
d.text((xy[0] + 5, xy[1] + 3), fid, fill=(255, 255, 0), font=FB, stroke_width=3, stroke_fill=(0, 0, 0))
sh.save(os.path.join(qc, "CORNERS-%s-%s.jpg" % (tag, nm)), quality=90)
def visible_people(people, oid, people_oids, W, H):
out = []
for p in people:
hp = p["px_height"]
if hp < 4:
continue
x0, x1 = int(max(0, p["x"] - 0.3 * hp)), int(min(W, p["x"] + 0.3 * hp + 1))
y0, y1 = int(max(0, p["y_head"] - 2)), int(min(H, p["y_foot"] + 2))
if x1 <= x0 or y1 <= y0:
continue
own = np.isin(oid[y0:y1, x0:x1], list(people_oids)).sum()
p = dict(p, own_px=int(own), vis=round(float(own / max(1.0, 0.22 * hp * hp)), 2))
if own >= 8 and p["vis"] >= 0.2:
out.append(p)
return out
def fig_overlay(fid, frame, people, qc, warp=None):
im = frame.copy()
H, W = im.shape[:2]
if warp is not None:
Wp = np.array(warp)
def tf(x, y):
return Wp[0, 0] * x + Wp[0, 1] * y + Wp[0, 2], Wp[1, 0] * x + Wp[1, 1] * y + Wp[1, 2]
pp = []
for p in people:
x, yf = tf(p["x"], p["y_foot"])
_, yh = tf(p["x"], p["y_head"])
pp.append(dict(p, x=x, y_foot=yf, y_head=yh, px_height=yf - yh))
people = pp
for p in people:
x, yf, yh = p["x"], p["y_foot"], p["y_head"]
hp = yf - yh
if hp < 4:
continue
x, yf, yh = int(round(x)), int(round(yf)), int(round(yh))
lo, hi = int(round(yf - hp * 1.25)), int(round(yf - hp * 0.75))
c = (60, 255, 60)
off = max(4, int(hp * 0.28))
cv2.line(im, (x + off, yf), (x + off, yh), c, 1)
cv2.line(im, (x + off - 3, yf), (x + off + 4, yf), c, 1)
cv2.line(im, (x + off - 3, yh), (x + off + 4, yh), c, 2)
cv2.line(im, (x + off - 1, lo), (x + off + 3, lo), (0, 200, 255), 1)
cv2.line(im, (x + off - 1, hi), (x + off + 3, hi), (0, 200, 255), 1)
cv2.imwrite(os.path.join(qc, fid + "_figs.jpg"), im, [cv2.IMWRITE_JPEG_QUALITY, 90])
tiles = []
for p in sorted(people, key=lambda p: -p["px_height"])[:16]:
hp = p["px_height"]
x = int(p["x"])
yf = int(p["y_foot"])
w2 = int(max(40, hp * 1.2))
x0, x1 = max(0, x - w2), min(W, x + w2)
y0, y1 = max(0, int(yf - hp * 1.7)), min(H, int(yf + hp * 0.3) + 4)
if x1 - x0 < 16 or y1 - y0 < 16:
continue
a = im[y0:y1, x0:x1]
s = min(6.0, 260 / (y1 - y0))
a = cv2.resize(a, None, fx=s, fy=s, interpolation=cv2.INTER_CUBIC)
t = "%s %sm %.0fm" % (p["label"][:14], p["h_m"], p["dist_m"])
cv2.putText(a, t, (4, 16), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 3)
cv2.putText(a, t, (4, 16), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1)
tiles.append(a)
if tiles:
Hh = max(t.shape[0] for t in tiles)
rows, row, wsum = [], [], 0
for t in tiles:
t = np.vstack([t, np.zeros((Hh - t.shape[0], t.shape[1], 3), np.uint8)])
if wsum + t.shape[1] > 2400 and row:
rows.append(np.hstack(row))
row, wsum = [], 0
row.append(t)
wsum += t.shape[1] + 4
row.append(np.zeros((Hh, 4, 3), np.uint8))
if row:
rows.append(np.hstack(row))
Wm = max(r.shape[1] for r in rows)
rows = [np.hstack([r, np.zeros((r.shape[0], Wm - r.shape[1], 3), np.uint8)]) for r in rows]
cv2.imwrite(os.path.join(qc, fid + "_figzoom.jpg"), np.vstack(rows), [cv2.IMWRITE_JPEG_QUALITY, 90])
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("outdir")
ap.add_argument("ids", nargs="*")
ap.add_argument("--config", required=True)
ap.add_argument("--keepers", default=None, help="JSON {view: keeper.png} for relit frames")
ap.add_argument("--landmark-groups", default=None, help="comma list of solid groups; default from config.gates")
ap.add_argument("--bump", type=int, default=18, help="skyline rise (px) that counts as an invented structure")
ap.add_argument("--qc", default=None, help="output dir; default OUTDIR/qc")
a = ap.parse_args()
cfg = load_config(a.config)
plates = resolve(cfg, cfg.get("plates_dir", "plates"))
qc = a.qc or os.path.join(a.outdir, "qc")
os.makedirs(qc, exist_ok=True)
keepers = json.load(open(a.keepers)) if a.keepers else {}
lm_groups = (a.landmark_groups or cfg.get("gates", {}).get("landmark_groups", "")).split(",")
lm_groups = [g for g in lm_groups if g]
ids = a.ids or sorted(os.path.splitext(os.path.basename(p))[0] for p in glob.glob(os.path.join(a.outdir, "*.png")))
import scene_solids as SC
res = {}
frames = []
for fid in ids:
v = view_of(fid)
is_relight = v in keepers
ref_path = keepers[v] if is_relight else os.path.join(plates, v + "_mass.png")
plate = cv2.imread(ref_path)
frame = cv2.imread(os.path.join(a.outdir, fid + ".png"))
if plate is None or frame is None:
print("skip", fid, "(missing plate or frame)")
continue
H, W = plate.shape[:2]
if frame.shape[:2] != (H, W):
frame = cv2.resize(frame, (W, H), interpolation=cv2.INTER_LANCZOS4)
r = drift(plate, frame)
r.update(ecc(plate, frame))
oid_path = os.path.join(plates, v + "_oid.png")
cls_path = os.path.join(plates, v + "_cls.png")
oid = cv2.imread(oid_path, cv2.IMREAD_UNCHANGED) if os.path.exists(oid_path) else None
lmk = None
if oid is not None and lm_groups and not is_relight:
# rebuild the scene to know which oids belong to the landmark groups
VIEWS = json.load(open(resolve(cfg, cfg["views"])))
scene_path = resolve(cfg, VIEWS.get(v, {}).get("scene", cfg.get("scene")))
SC.reset()
if scene_path and os.path.exists(scene_path):
SC.build_from_json(json.load(open(scene_path)))
lm_oids = set()
for g in lm_groups:
lm_oids |= {o + 1 for o in SC.GROUPS.get(g, [])}
if lm_oids:
lmk = np.isin(oid, list(lm_oids)).astype(np.uint8)
lm = cv2.dilate(lmk, np.ones((5, 5), np.uint8))
r["landmark_grad_corr"] = region_corr(plate, frame, lm)
ov = frame.copy()
e = cv2.morphologyEx(lmk, cv2.MORPH_GRADIENT, np.ones((3, 3), np.uint8)) > 0
ov[e] = (255, 255, 0)
cv2.imwrite(os.path.join(qc, fid + "_geo.jpg"), ov, [cv2.IMWRITE_JPEG_QUALITY, 90])
if os.path.exists(cls_path) and not is_relight:
cls = np.asarray(Image.open(cls_path))
if (cls == 0).any():
ps = plate_skyline(cls)
fs = frame_skyline(frame)
if np.isfinite(ps).sum() > 300 and np.isfinite(fs).sum() > 300:
r.update(skyline_fit(ps, fs, W / 2, H / 2))
runs = invented_structures(ps, fs, lmk, bump_px=a.bump)
r["invented_structures"] = runs
r["invented_structures_n"] = len(runs)
ppl_path = os.path.join(plates, v + "_people.json")
if oid is not None and os.path.exists(ppl_path):
people = visible_people(json.load(open(ppl_path)), oid, set(o + 1 for o in SC.GROUPS.get("people", [])) if SC.GROUPS else set(), W, H)
r["proxies_visible"] = len(people)
r["proxies_px_max"] = max([p["px_height"] for p in people] or [0])
warp = r.get("_warp") if (r.get("ecc_cc") or 0) >= 0.55 and abs(r.get("scale", 1) - 1) < 0.08 else None
fig_overlay(fid, frame, people, qc, warp)
r.pop("_warp", None)
res[fid] = r
frames.append((fid, frame))
print(fid, {k: r.get(k) for k in ("grad_corr", "edge_rec", "landmark_grad_corr", "ecc_cc", "scale", "rot_deg",
"tx_px", "ty_px", "sky_res", "invented_structures_n", "proxies_visible")}, flush=True)
tag = os.path.basename(os.path.normpath(a.outdir))
corners(frames, tag, qc)
json.dump(res, open(os.path.join(qc, "gate_%s.json" % tag), "w"), indent=1)
print("wrote", os.path.join(qc, "gate_%s.json" % tag))
if __name__ == "__main__":
main()
+213
View File
@@ -0,0 +1,213 @@
#!/usr/bin/env python3
"""Turn segmentation masks into a 2.5D town block model (a heightfield with roofs, trees, water and zones).
Footprints are straightened, slivers opened away, merged row-house blocks split into house-sized pieces
(separate houses must be separate in pixels, or the re-surfacing model re-slices them into a "caterpillar"),
and every footprint gets a roof: a gable over its fitted rectangle, or a capped hip from the distance transform.
Heights are NOT on the map. They come from wealth zones in the config (`zones`): each zone gives an eaves range
and a roof pitch. Zones are assigned by polygon (in map px) when given, else by roof density (dense core = zone 2,
sparse edge = zone 1). Landmarks you model by hand are cut out (`cutouts`, polygons in map px).
usage: massing.py seg.npz --config citykit.json --out town.npz [--window x0 y0 x1 y1]
Writes HR (roof height m), SDF (m, <0 inside a footprint), EAV (eaves m), CLS, ZONE, TINT, WD (water depth), TREE,
DECK (bridges), plus the window and cell size. render_plate.py reads it.
"""
import argparse
import cv2
import numpy as np
from citykit import ell, h01, load_config
RISE_MAX = 6.5 # m, tallest roof rise above the eaves
def roof_heights(roof, zone_of, lab, n, st, zones, mpp):
H = np.zeros(roof.shape, np.float32)
EAV = np.zeros(roof.shape, np.float32)
for i in range(1, n):
x, y, w, h, area = st[i]
sub = lab[y:y + h, x:x + w] == i
z = zones[str(zone_of[i])]
emin, emax, pitch = z["eaves_min"], z["eaves_max"], z["pitch"]
cx, cy = x + w / 2, y + h / 2
eav = emin + (emax - emin) * h01("eav", i, int(cx), int(cy))
ys, xs = np.nonzero(sub)
rect = cv2.minAreaRect(np.stack([xs, ys], 1).astype(np.float32))
(rcx, rcy), (rw, rh), ang = rect
fill = area / (rw * rh + 1e-6)
blk = H[y:y + h, x:x + w]
eb = EAV[y:y + h, x:x + w]
if fill > 0.70 and min(rw, rh) > 5 and max(rw, rh) * mpp < 75:
box = cv2.boxPoints(rect)
if np.linalg.norm(box[1] - box[0]) < np.linalg.norm(box[2] - box[1]):
box = np.roll(box, -1, axis=0)
p0, p1, p2, p3 = box
d = (p1 - p0) / (np.linalg.norm(p1 - p0) + 1e-6)
nrm = np.array([-d[1], d[0]])
a = (p0 + p3) / 2
span = abs(np.dot(p3 - p0, nrm)) / 2
m = np.zeros((h, w), np.uint8)
cv2.fillPoly(m, [np.round(box).astype(np.int32)], 1)
m &= cv2.dilate(sub.astype(np.uint8), ell(3))
yy, xx = np.nonzero(m)
dist = np.abs((xx - a[0]) * nrm[0] + (yy - a[1]) * nrm[1])
rise = np.clip(span - dist, 0, None) * mpp * pitch
hv = eav + np.minimum(rise, RISE_MAX)
blk[yy, xx] = np.maximum(blk[yy, xx], hv)
eb[yy, xx] = np.maximum(eb[yy, xx], eav)
else:
comp = sub.astype(np.uint8)
dt = cv2.distanceTransform(np.pad(comp, 1), cv2.DIST_L2, 5)[1:-1, 1:-1] * mpp
hv = np.where(comp > 0, eav + np.minimum(dt * pitch, RISE_MAX), 0)
np.maximum(blk, hv, out=blk)
eb[comp > 0] = np.maximum(eb[comp > 0], eav)
return H, EAV
def split_blocks(roof, mpp, min_area_m2=180.0, house_min=8.0, house_max=15.0, depth_split=17.0):
"""Merged row-house blocks -> house-sized pieces with a 1-cell gap between them."""
n, lab, st, _ = cv2.connectedComponentsWithStats(roof, 8)
cut = np.zeros_like(roof)
for i in range(1, n):
x, y, w, h, a = st[i]
if a * mpp ** 2 < min_area_m2:
continue
sub = lab[y:y + h, x:x + w] == i
ys, xs = np.nonzero(sub)
cx, cy = xs.mean(), ys.mean()
cov = np.cov(np.stack([xs - cx, ys - cy]))
ev, evec = np.linalg.eigh(cov)
ux, uy = evec[0, 1], evec[1, 1]
yy, xx = np.mgrid[0:h, 0:w]
u = (xx - cx) * ux + (yy - cy) * uy
v = -(xx - cx) * uy + (yy - cy) * ux
pv = -(xs - cx) * uy + (ys - cy) * ux
edges = [u.min()]
k = 0
while edges[-1] < u.max():
edges.append(edges[-1] + (house_min + (house_max - house_min) * h01("w", i, k)) / mpp)
k += 1
e = np.array(edges)
boundary = np.zeros((h, w), bool)
if len(e) > 2:
dist_edge = np.min(np.abs(u[..., None] - e[None, None, 1:-1]), axis=-1)
boundary |= dist_edge < 0.6
if (pv.max() - pv.min()) * mpp > depth_split:
boundary |= np.abs(v) < 0.6
cut[y:y + h, x:x + w] |= (boundary & sub).astype(np.uint8)
out = roof.copy()
out[cut > 0] = 0
return out
def build(seg, cfg, window=None):
mpp = float(seg["mpp"])
roof, tree, water = seg["roof"].copy(), seg["tree"].copy(), seg["water"].copy()
if window is not None:
x0, y0, x1, y1 = window
else:
x0, y0, x1, y1 = 0, 0, roof.shape[1], roof.shape[0]
roof, tree, water = roof[y0:y1, x0:x1], tree[y0:y1, x0:x1], water[y0:y1, x0:x1]
gh, gw = roof.shape
# hand-modeled landmarks are cut out (polygons in map px)
excl = np.zeros_like(roof)
for poly in cfg.get("cutouts", []):
P = np.array(poly, float) - [x0, y0]
cv2.fillPoly(excl, [P.astype(np.int32)], 1)
if excl.any():
excl = cv2.dilate(excl, ell(6 / mpp))
roof[excl > 0] = 0
tree[excl > 0] = 0
# bridges/decks: roof components lying mostly over (dilated) water
wd = cv2.dilate(water, ell(15))
n, lab, st, _ = cv2.connectedComponentsWithStats(roof, 8)
deck = np.zeros_like(roof)
for i in range(1, n):
x, y, w, h, a = st[i]
comp = lab[y:y + h, x:x + w] == i
if wd[y:y + h, x:x + w][comp].mean() > 0.5:
deck[y:y + h, x:x + w][comp] = 1
roof[deck > 0] = 0
# straighten + de-sliver
cnts, hier = cv2.findContours(roof, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
clean = np.zeros_like(roof)
if hier is not None:
for i, c in enumerate(cnts):
if hier[0][i][3] < 0:
cv2.fillPoly(clean, [cv2.approxPolyDP(c, 1.0, True)], 1)
for i, c in enumerate(cnts):
if hier[0][i][3] >= 0 and cv2.contourArea(c) > 60:
cv2.fillPoly(clean, [cv2.approxPolyDP(c, 1.0, True)], 0)
roof = cv2.morphologyEx(clean, cv2.MORPH_OPEN, ell(2.0 / mpp))
n, lab, st, cen = cv2.connectedComponentsWithStats(roof, 8)
keep = np.zeros(n, bool)
keep[1:] = st[1:, 4] * mpp ** 2 >= cfg.get("min_house_m2", 40)
roof = keep[lab].astype(np.uint8)
roof = split_blocks(roof, mpp)
n, lab, st, cen = cv2.connectedComponentsWithStats(roof, 4)
# zones
zones = cfg["zones"]
zone_of = np.ones(n, np.uint8)
polys = [(int(z.get("id", k)), np.array(z["polygon_px"], float) - [x0, y0])
for k, z in zones.items() if "polygon_px" in z]
dens = cv2.blur(roof.astype(np.float32), (int(80 / mpp) | 1,) * 2)
default_core = int(cfg.get("default_core_zone", 2))
default_edge = int(cfg.get("default_edge_zone", 1))
for i in range(1, n):
cx, cy = cen[i]
zid = None
for pid, P in polys:
if cv2.pointPolygonTest(P.astype(np.float32), (float(cx), float(cy)), False) >= 0:
zid = pid
break
if zid is None:
zid = default_core if dens[int(cy), int(cx)] >= cfg.get("core_density", 0.16) else default_edge
zone_of[i] = zid
HR, EAV = roof_heights(roof, zone_of, lab, n, st, zones, mpp)
ZONE = zone_of[lab] * (roof > 0)
TINT = (np.array([0] + [int(255 * h01("t", i)) for i in range(1, n)], np.uint8))[lab]
# trees -> lumpy crowns
tt = cv2.morphologyEx(tree, cv2.MORPH_OPEN, ell(3)) & (roof == 0)
dtt = cv2.distanceTransform(tt, cv2.DIST_L2, 3) * mpp
rng = np.random.default_rng(3)
lump = cv2.GaussianBlur(rng.normal(0, 1, tt.shape).astype(np.float32), (0, 0), 2.0)
TREE = np.where(tt > 0, 5.5 + 1.6 * np.minimum(dtt, 4.5) + 0.9 * lump, 0).astype(np.float32)
TREE = cv2.GaussianBlur(TREE, (0, 0), 1.0) * (tt > 0)
din = cv2.distanceTransform(roof, cv2.DIST_L2, 5)
dout = cv2.distanceTransform((1 - roof).astype(np.uint8), cv2.DIST_L2, 5)
SDF = np.clip(((dout - din) * mpp).astype(np.float32), -20, 20)
HRx = cv2.dilate(np.where(roof > 0, HR, 0).astype(np.float32), np.ones((3, 3), np.uint8))
HR = np.where(roof > 0, HR, HRx).astype(np.float32)
EAVx = cv2.dilate(EAV, np.ones((3, 3), np.uint8))
EAV = np.where(roof > 0, EAV, EAVx)
wsoft = cv2.GaussianBlur(water.astype(np.float32), (0, 0), 1.2)
WD = float(cfg.get("water_depth_m", 3.0)) * np.clip(wsoft * 1.6 - 0.3, 0, 1)
# classes: 0 street 1 roof 3 tree 4 water 6 deck 8 field
CLS = np.zeros((gh, gw), np.uint8)
open_ = ((roof == 0) & (water == 0) & (tt == 0)).astype(np.uint8)
dens2 = cv2.blur((roof > 0).astype(np.float32), (int(160 / mpp) | 1,) * 2)
CLS[(open_ > 0) & (dens2 < 0.05)] = 8
CLS[roof > 0] = 1
CLS[tt > 0] = 3
CLS[water > 0] = 4
CLS[deck > 0] = 6
print("town grid", gh, gw, "houses", n - 1, "roof frac %.3f" % (roof > 0).mean(),
"zones", dict(zip(*np.unique(zone_of[1:], return_counts=True))))
return dict(HR=HR.astype(np.float32), SDF=SDF.astype(np.float16), EAV=EAV.astype(np.float16), CLS=CLS, TINT=TINT,
ZONE=ZONE.astype(np.uint8), WD=WD.astype(np.float16), TREE=TREE.astype(np.float16),
DECK=deck.astype(np.uint8), win=np.array([x0, y0, x1, y1]), cell=np.float32(mpp))
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("seg")
ap.add_argument("--config", required=True)
ap.add_argument("--out", default="town.npz")
ap.add_argument("--window", type=int, nargs=4, default=None, help="x0 y0 x1 y1 in map px")
a = ap.parse_args()
cfg = load_config(a.config)
out = build(np.load(a.seg), cfg["massing"], a.window)
np.savez(a.out, **out)
print("wrote", a.out)
+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env python3
"""Build the matrix jobs for an accepted set of keepers: time-of-day x weather, and population x time-of-day.
Weather/time matrix (exteriors): every cell is an EDIT of the accepted Day Dry keeper of the same camera (or, for
Sunset/Night cells, a relight of the Day cell of the same weather), never a fresh re-surface, so geometry carries.
Cells and sizes come from the tier (references/effort-tiers.md):
tier 1: Day|Sunset|Night x Dry|Rain|Snow (light) = 9 per camera, plus any storm/deep-snow cells you enable
tier 2: Day + Night Dry + one Snow (light)
tier 3: Day Dry only (no matrix)
Population x time matrix (venues with people): the plate carries proxies at the chosen density; each cell states the
hour and the density relationally. Sizes by tier: T1 4x3 (Morning/Afternoon/Evening/Late Night x Quiet/Moderate/
Packed; 4x4 with Empty for storyline hubs), T2 2x3 (Day/Sunset x Quiet/Moderate/Packed), T3 1x2 (peak hour x
Quiet/Packed), T4 none.
usage: matrix_jobs.py weather --config citykit.json --prompts prompts.json --tier 1 V=DayDryKeeper.png ... --out jobs_matrix.json
matrix_jobs.py population --config citykit.json --prompts prompts.json --tier 1 V=EmptyMaster.png ... --out jobs_pop.json
prompts.json needs "weather" {Rain, Snow (light), Snow (medium), Snow (heavy), Blizzard, ...}, "relights" {Sunset,
Night, ...}, "hours" {Morning, ...} and "densities" {Quiet, ...} blocks. Weather names follow the naming convention
in references/naming.md so files sort and display consistently.
"""
import argparse
import json
import os
from citykit import load_config
WEATHER_BY_TIER = {1: ["Dry", "Rain", "Snow (light)"], 2: ["Dry", "Snow (light)"], 3: ["Dry"], 4: []}
TIMES_BY_TIER = {1: ["Day", "Sunset", "Night"], 2: ["Day", "Night"], 3: ["Day"], 4: []}
POP_BY_TIER = {1: (["Morning", "Afternoon", "Evening", "Late Night"], ["Quiet", "Moderate", "Packed"]),
2: (["Day", "Sunset"], ["Quiet", "Moderate", "Packed"]),
3: (["Peak"], ["Quiet", "Packed"]), 4: ([], [])}
def weather_jobs(cfg, P, keepers, tier, takes, extra_weather):
size = cfg["renderer"].get("size", "2048x1152")
jobs = []
weathers = WEATHER_BY_TIER[tier] + extra_weather
for v, keeper in keepers.items():
for wx in weathers:
# day cell of this weather: an edit of the Day Dry keeper (Dry = the keeper itself, no job)
if wx != "Dry":
prompt = P["weather"][wx] + " " + P.get("weather_lock", "") + " " + P["nomodern"]
for t in range(1, takes + 1):
jobs.append(dict(id="%s_Day-%s_t%d" % (v, wx.replace(" ", ""), t), size=size, refs=[os.path.abspath(keeper)],
prompt=prompt + " [%s, Day %s, take %d]" % (v, wx, t),
derived_from=keeper, cell="Day %s" % wx, view=v))
# Sunset/Night of this weather: relights of the Day cell of the same weather (chained; run after picking)
for tm in TIMES_BY_TIER[tier]:
if tm == "Day":
continue
src = keeper if wx == "Dry" else "<pick of %s Day %s>" % (v, wx)
prompt = P["relights"][tm if tm in P["relights"] else tm.lower()] + " " + P["nomodern"]
for t in range(1, takes + 1):
jobs.append(dict(id="%s_%s-%s_t%d" % (v, tm, wx.replace(" ", ""), t), size=size, refs=[src],
prompt=prompt + " [%s, %s %s, take %d]" % (v, tm, wx, t),
derived_from=src, cell="%s %s" % (tm, wx), view=v, chained=(wx != "Dry")))
return jobs
def population_jobs(cfg, P, masters, tier, takes, with_empty):
size = cfg["renderer"].get("size", "2048x1152")
hours, dens = POP_BY_TIER[tier]
if with_empty and tier == 1:
dens = ["Empty"] + dens
jobs = []
for v, master in masters.items():
for hr in hours:
for dn in dens:
prompt = " ".join([P.get("population_intro", ""), P["hours"][hr], P["densities"][dn],
P.get("population_locks", ""), P["nomodern"]])
for t in range(1, takes + 1):
jobs.append(dict(id="%s_%s-%s_t%d" % (v, hr.replace(" ", ""), dn, t), size=size,
refs=[os.path.abspath(master)], prompt=prompt + " [%s, %s %s, take %d]" % (v, hr, dn, t),
cell="%s %s" % (hr, dn), view=v))
return jobs
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("mode", choices=["weather", "population"])
ap.add_argument("rest", nargs="*", help="V=keeper.png pairs")
ap.add_argument("--config", required=True)
ap.add_argument("--prompts", required=True)
ap.add_argument("--tier", type=int, default=1)
ap.add_argument("--takes", type=int, default=2)
ap.add_argument("--extra-weather", default="", help="comma list, e.g. 'Blizzard,Snow (heavy)'")
ap.add_argument("--with-empty", action="store_true", help="tier 1 population: add the Empty column (4x4)")
ap.add_argument("--out", required=True)
a = ap.parse_intermixed_args()
cfg = load_config(a.config)
P = json.load(open(a.prompts))
keep = dict(x.split("=", 1) for x in a.rest if "=" in x)
if a.mode == "weather":
jobs = weather_jobs(cfg, P, keep, a.tier, a.takes, [w for w in a.extra_weather.split(",") if w])
else:
jobs = population_jobs(cfg, P, keep, a.tier, a.takes, a.with_empty)
R = cfg["renderer"]
chained = [j for j in jobs if j.get("chained")]
json.dump(dict(model=R["model"], quality=R.get("quality", "high"), refs_common=[], jobs=jobs), open(a.out, "w"), indent=1)
print(len(jobs), "jobs ->", a.out, "(%d chained: fill in their refs after picking the Day cell)" % len(chained))
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
"""Contact sheets for the eye check: plate | pick | pick with landmark outline, with the gate numbers as a caption.
usage: qc_sheet.py --config citykit.json --picks picks.json --gates out_day/qc/gate_out_day.json --out qc_plates.jpg [V ...]
picks.json: {"V": {"day": "V_day_t2", "sunset": "V_sunset_t1"}, ...}
Thumbnail every batch of same-tier exteriors at this size before calling it done: "geometry-true" is not the
same as "recognizable" (seven houses on one wealth rung come out as seven identical barns without a character pass).
"""
import argparse
import json
import os
from PIL import Image, ImageDraw, ImageFont
from citykit import load_config, resolve
try:
FB = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 26)
FS = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
except OSError:
FB = FS = ImageFont.load_default()
def lab(d, xy, t, f=FS, fill=(255, 255, 0)):
d.text(xy, t, fill=fill, font=f, stroke_width=3, stroke_fill=(0, 0, 0))
def gate_line(QA, fid):
r = QA.get(fid, {})
return "grad_corr %s · ECC scale %s · shift %s/%s px · landmark corr %s · invented %s" % (
r.get("grad_corr"), r.get("scale"), r.get("tx_px"), r.get("ty_px"), r.get("landmark_grad_corr"),
r.get("invented_structures_n", "-"))
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("views", nargs="*")
ap.add_argument("--config", required=True)
ap.add_argument("--picks", required=True)
ap.add_argument("--gates", required=True)
ap.add_argument("--frames", required=True, help="dir holding the take PNGs")
ap.add_argument("--out", required=True)
ap.add_argument("--title", default="plates vs picks")
a = ap.parse_args()
cfg = load_config(a.config)
plates = resolve(cfg, cfg.get("plates_dir", "plates"))
PICKS = json.load(open(a.picks))
QA = json.load(open(a.gates))
qc = os.path.join(a.frames, "qc")
views = a.views or list(PICKS)
w, h = 640, 360
rows = []
for v in views:
p = PICKS[v]
fid = p["day"] if isinstance(p, dict) else p
ims = [(Image.open(os.path.join(plates, v + "_mass.png")), "block-model plate (IMAGE 1)"),
(Image.open(os.path.join(a.frames, fid + ".png")), "pick " + fid)]
geo = os.path.join(qc, fid + "_geo.jpg")
if os.path.exists(geo):
ims.append((Image.open(geo), "pick + landmark outline from the model (cyan)"))
R = Image.new("RGB", (3 * w + 16, h + 64), (20, 20, 20))
d = ImageDraw.Draw(R)
for i, (im, t) in enumerate(ims):
R.paste(im.convert("RGB").resize((w, h)), (i * (w + 8), 34))
lab(d, (i * (w + 8) + 6, 38), t)
lab(d, (6, 4), "%s: %s" % (v, gate_line(QA, fid)), FS, (255, 255, 255))
rows.append(R)
H = sum(r.height for r in rows)
S = Image.new("RGB", (rows[0].width, H + 50), (20, 20, 20))
d = ImageDraw.Draw(S)
lab(d, (10, 8), a.title, FB, (255, 255, 255))
y = 50
for r in rows:
S.paste(r, (0, y))
y += r.height
S.save(a.out, quality=88)
print("wrote", a.out)
+178
View File
@@ -0,0 +1,178 @@
#!/usr/bin/env python3
"""Self-calibrating parallel batch submitter for an image-edit API ("re-surface IMAGE 1 with these references").
usage: render_batch.py jobs.json --out OUTDIR [--start 4] [--max 8] [--min 2] [--adapter openai_images]
jobs.json: {"model": "...", "quality": "high", "refs_common": [...], "jobs": [{"id", "prompt", "size", "refs": [...],
"prompt_alt": "..."(optional, used once on a moderation refusal)}]}
Concurrency starts at --start in flight; a 429/5xx backs off (limit -2, sleep Retry-After or exponential, requeue);
a full wave of successes at a level ratchets +2 (cap --max). Ledger: OUTDIR/results.jsonl (one line per call,
status, seconds, attempt) and events.log; summary in calibration.md. cost.py reads the ledger.
Credentials come from the environment only: IMAGE_API_KEY (required), IMAGE_API_BASE (default
https://api.openai.com/v1). Never put them in a job file or a config that goes into version control.
Adapters (--adapter): "openai_images" posts multipart to /images/edits (or /images/generations when a job has no
references). To use another renderer, add a function `submit(job, refs, spec, env) -> (status_code, png_bytes|None,
body_text)` in adapters/<name>.py; see adapters/README.md.
"""
import argparse
import base64
import importlib
import json
import os
import re
import sys
import threading
import time
import requests
def submit_openai_images(job, refs, spec, env):
key, base = env["key"], env["base"]
if not refs:
data = {"model": spec["model"], "prompt": job["prompt"], "size": job["size"],
"quality": spec.get("quality", "high"), "output_format": "png", "n": 1}
r = requests.post(base + "/images/generations", headers={"Authorization": "Bearer " + key,
"Content-Type": "application/json"}, json=data, timeout=(15, 480))
else:
files = [("image[]", (os.path.basename(p), open(p, "rb"),
"image/jpeg" if p.lower().endswith((".jpg", ".jpeg")) else "image/png")) for p in refs]
data = {"model": spec["model"], "prompt": job["prompt"], "size": job["size"],
"quality": spec.get("quality", "high"), "output_format": "png", "n": "1"}
r = requests.post(base + "/images/edits", headers={"Authorization": "Bearer " + key}, data=data, files=files,
timeout=(15, 480))
for _, f in files:
f[1].close()
png = None
if r.status_code == 200:
png = base64.b64decode(r.json()["data"][0]["b64_json"])
return r.status_code, png, (r.text[:300].replace("\n", " ") if r.status_code != 200 else ""), r.headers
def load_adapter(name):
if name == "openai_images":
return submit_openai_images
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "adapters"))
return importlib.import_module(name).submit
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("jobs")
ap.add_argument("--out", required=True)
ap.add_argument("--start", type=int, default=4)
ap.add_argument("--max", type=int, default=8)
ap.add_argument("--min", type=int, default=2)
ap.add_argument("--adapter", default="openai_images")
ap.add_argument("--done-file", default=None, help="write a timestamp here when the batch finishes")
ap.add_argument("--skip-existing", action="store_true", help="do not re-submit jobs whose PNG already exists")
a = ap.parse_args()
spec = json.load(open(a.jobs))
OUT = a.out
os.makedirs(OUT, exist_ok=True)
key = os.environ.get("IMAGE_API_KEY")
if not key:
sys.exit("IMAGE_API_KEY is not set in the environment")
env = dict(key=key, base=os.environ.get("IMAGE_API_BASE", "https://api.openai.com/v1").rstrip("/"))
submit = load_adapter(a.adapter)
lock = threading.Condition()
state = dict(limit=a.start, in_flight=0, succ_at_level=0, max_inflight_clean=0, r429=0, errors=0, done=0,
cooldown_until=0.0)
ev = open(os.path.join(OUT, "events.log"), "a")
led = open(os.path.join(OUT, "results.jsonl"), "a")
def log(msg):
line = time.strftime("%H:%M:%S ") + msg
print(line, flush=True)
ev.write(line + "\n")
ev.flush()
def ledger(**row):
led.write(json.dumps(row) + "\n")
led.flush()
def worker(job, attempt):
with lock:
while state["in_flight"] >= state["limit"] or time.time() < state["cooldown_until"]:
lock.wait(1.0)
state["in_flight"] += 1
inflight, level = state["in_flight"], state["limit"]
log("DISPATCH %s attempt=%d in_flight=%d limit=%d" % (job["id"], attempt, inflight, level))
refs = [os.path.abspath(p) for p in spec.get("refs_common", []) + job.get("refs", [])]
t0 = time.time()
try:
code, png, body, headers = submit(job, refs, spec, env)
except Exception as e: # network errors count as retryable
code, png, body, headers = -1, None, str(e)[:200], {}
log("EXC %s %s" % (job["id"], body))
dt = time.time() - t0
with lock:
state["in_flight"] -= 1
nrefs = len(refs)
if code == 200 and png:
open(os.path.join(OUT, job["id"] + ".png"), "wb").write(png)
state["done"] += 1
state["succ_at_level"] += 1
state["max_inflight_clean"] = max(state["max_inflight_clean"], inflight)
ledger(id=job["id"], status=200, secs=round(dt, 1), in_flight=inflight, limit=level, attempt=attempt,
refs=nrefs, size=job.get("size"))
log("OK %s %.0fs" % (job["id"], dt))
if state["succ_at_level"] >= state["limit"] and state["limit"] < a.max:
state["limit"] = min(a.max, state["limit"] + 2)
state["succ_at_level"] = 0
log("RATCHET limit -> %d" % state["limit"])
lock.notify_all()
return True
ledger(id=job["id"], status=code, secs=round(dt, 1), in_flight=inflight, limit=level, attempt=attempt,
refs=nrefs, body=body)
if "insufficient_quota" in body:
log("QUOTA %s -- not retrying" % job["id"])
state["errors"] += 1
lock.notify_all()
return False
if code == 429 or code >= 500 or code == -1:
state["r429"] += 1 if code == 429 else 0
ra = headers.get("Retry-After") if headers else None
wait = float(ra) if ra and ra.replace(".", "").isdigit() else min(60, 5 * (2 ** (attempt - 1)))
state["limit"] = max(a.min, state["limit"] - 2)
state["succ_at_level"] = 0
state["cooldown_until"] = time.time() + wait
log("BACKOFF %s http=%s wait=%.0fs limit -> %d %s" % (job["id"], code, wait, state["limit"], body[:120]))
lock.notify_all()
return worker(job, attempt + 1) if attempt < 6 else False
state["errors"] += 1
log("FAIL %s http=%s %s" % (job["id"], code, body[:200]))
# one rephrase on a moderation refusal, never more; after that the lane reports it and stops
if attempt == 1 and job.get("prompt_alt") and re.search(r"safety|moderat|content_policy|rejected", body, re.I):
log("REPHRASE %s" % job["id"])
lock.notify_all()
return worker(dict(job, prompt=job["prompt_alt"]), 2)
lock.notify_all()
return False
jobs = spec["jobs"]
if a.skip_existing:
jobs = [j for j in jobs if not os.path.exists(os.path.join(OUT, j["id"] + ".png"))]
t_start = time.time()
threads = [threading.Thread(target=worker, args=(j, 1), daemon=True) for j in jobs]
for t in threads:
t.start()
for t in threads:
t.join()
el = max(time.time() - t_start, 1e-6)
summary = ("# batch calibration — %s\n\n- jobs: %d, succeeded: %d, hard failures: %d, 429s: %d\n"
"- start concurrency %d, ceiling reached: %d (cap %d); max in-flight without pushback: %d\n"
"- elapsed %.1f min, %.1f images/min; model %s, quality %s\n"
% (time.strftime("%Y-%m-%d %H:%M UTC", time.gmtime()), len(jobs), state["done"], state["errors"],
state["r429"], a.start, state["limit"], a.max, state["max_inflight_clean"], el / 60,
state["done"] / (el / 60), spec["model"], spec.get("quality", "high")))
open(os.path.join(OUT, "calibration.md"), "w").write(summary)
log("DONE\n" + summary)
if a.done_file:
open(a.done_file, "w").write(time.strftime("%FT%TZ", time.gmtime()))
if __name__ == "__main__":
main()
+620
View File
@@ -0,0 +1,620 @@
#!/usr/bin/env python3
"""Template-plate renderer: a CPU ray marcher over the town heightfield + optional terrain + analytic solids.
For each camera in views.json it writes, under --out:
<V>_mass.png the plate (IMAGE 1 for the re-surfacing model)
<V>_depth.npy ray distance per pixel (m)
<V>_oid.png 16-bit solid id + 1 (0 = heightfield/sky); the figure-scale gate uses it
<V>_cls.png class id per pixel (sky 0, street 1, roof 2, wall 3, tree 4, water 5, deck 6, field 7,
forest 8, rock 9, snow 10, solid 12)
<V>_cam.json camera (so the gates and sheets can project world points)
<V>_people.json every proxy person: screen foot/head rows, distance, px per meter
<V>_landmarks.json projected landmark points from the scene file
views.json: {"<V>": {"pos":[x,y,z], "yaw":deg (0 = north, clockwise), "pitch":deg, "hfov":deg, "shift":0..0.2,
"sun":"day"|"sunset"|"morning", "scene":"scene.json" (optional per-view scene)}}
A camera on the ground: z = ground + 1.65. Vertical lens shift keeps verticals vertical on pitched-up views.
usage: render_plate.py --config citykit.json --views views.json --out plates V1 [V2 ...] [--w 2048]
Runtime: a 2048x1152 plate takes 1-15 minutes on a 32-core CPU depending on how much terrain the rays cross.
Render views in parallel processes.
"""
import argparse
import json
import os
import time
import cv2
import numpy as np
from PIL import Image
import scene_solids as SC
from citykit import MapFrame, bil, load_config, resolve, save_json
SUNC = np.array([1.0, 0.93, 0.82])
AMB = np.array([0.78, 0.85, 0.97])
PAL = {
"street": (150, 142, 124), "field": (112, 128, 82), "meadow": (124, 132, 90), "forest": (62, 80, 58),
"rock": (122, 120, 114), "snow": (232, 236, 240), "water": (58, 82, 92), "deck": (112, 92, 70), "tree": (72, 96, 58),
"wall1": (124, 106, 86), "wall2": (150, 136, 116), "wall3": (178, 174, 166), "wall4": (184, 180, 172),
"roof1": (98, 108, 74), "roof2": (92, 88, 86), "roof3": (80, 84, 92), "roof4": (78, 82, 90),
}
CLS_ID = dict(sky=0, street=1, roof=2, wall=3, tree=4, water=5, deck=6, field=7, forest=8, rock=9, snow=10, solid=12)
SUNS = {"day": dict(az=248.0, el=24.0), "sunset": dict(az=297.0, el=1.5), "morning": dict(az=128.0, el=29.0)}
def sun_vec(kind, suns):
s = suns.get(kind, SUNS[kind])
az, el = np.radians(s["az"]), np.radians(s["el"])
return np.array([np.sin(az) * np.cos(el), np.cos(az) * np.cos(el), np.sin(el)])
class World:
"""Heightfield world: optional terrain grid (m above ground plane) + the town block model on top of it."""
def __init__(self, cfg):
self.cfg = cfg
t = cfg.get("terrain")
if t and t.get("path"):
T = np.load(resolve(cfg, t["path"]))
self.tH = T["H"].astype(np.float32)
self.tc, self.tx0, self.ty1 = float(T["cell"]), float(T["x0"]), float(T["y1"])
self.snowline = float(t.get("snowline_m", 1e9))
else:
self.tH = None
self.snowline = 1e9
m = cfg["map"]
Z = np.load(resolve(cfg, cfg["massing"]["town_npz"]))
self.HR = Z["HR"].astype(np.float32)
self.SDF = Z["SDF"].astype(np.float32)
self.EAV = Z["EAV"].astype(np.float32)
self.CLS, self.TINT, self.ZONE, self.DECK = Z["CLS"], Z["TINT"], Z["ZONE"], Z["DECK"]
self.WD = Z["WD"].astype(np.float32)
self.TREE = Z["TREE"].astype(np.float32)
self.win = Z["win"].astype(float)
self.zc = float(Z["cell"])
self.zh, self.zw = self.HR.shape
self.frame = MapFrame(m["meters_per_px"], m.get("origin_px", [0, 0]))
self.ext = cfg.get("world_extent_m", [-20000, 20000, -20000, 20000])
# 20 m bound tiles over the whole extent: max height per tile, so rays skip open air
self.BT = 20.0
x0, x1, y0, y1 = self.ext
nx, ny = int(np.ceil((x1 - x0) / self.BT)), int(np.ceil((y1 - y0) / self.BT))
mm = np.zeros((ny, nx), np.float32)
if self.tH is not None:
k = max(1, int(round(self.BT / self.tc)))
hh, ww = self.tH.shape
Hp = np.pad(self.tH, ((0, (-hh) % k), (0, (-ww) % k)), constant_values=-50)
tm = Hp.reshape(Hp.shape[0] // k, k, Hp.shape[1] // k, k).max(axis=(1, 3))
r0 = int(round((y1 - self.ty1) / self.BT))
c0 = int(round((self.tx0 - x0) / self.BT))
rs, cs = slice(max(r0, 0), min(r0 + tm.shape[0], ny)), slice(max(c0, 0), min(c0 + tm.shape[1], nx))
mm[rs, cs] = np.maximum(mm[rs, cs], tm[rs.start - r0:rs.stop - r0, cs.start - c0:cs.stop - c0])
town_h = np.maximum(np.where(self.SDF < 0.6, self.HR, 0), self.TREE)
town_h = np.maximum(town_h, self.DECK * 1.5)
th = cv2.dilate(town_h, np.ones((3, 3), np.uint8))
bc = max(1, int(np.ceil(self.BT / self.zc)))
Hz = np.pad(th, ((0, (-self.zh) % bc), (0, (-self.zw) % bc)))
tb = Hz.reshape(Hz.shape[0] // bc, bc, Hz.shape[1] // bc, bc).max(axis=(1, 3))
ty, tx = np.mgrid[0:ny, 0:nx]
Xc = x0 + (tx + 0.5) * self.BT
Yc = y1 - (ty + 0.5) * self.BT
r, c = self.town_rc(Xc, Yc)
bi = np.clip((r / bc).astype(int), 0, tb.shape[0] - 1)
bj = np.clip((c / bc).astype(int), 0, tb.shape[1] - 1)
inside = (r > -bc) & (c > -bc) & (r < self.zh + bc) & (c < self.zw + bc)
add = np.where(inside, tb[bi, bj], 0)
base_t = self.terrain(Xc.ravel(), Yc.ravel()).reshape(ny, nx) if self.tH is not None else 0
mm = np.maximum(mm, base_t + cv2.dilate(add.astype(np.float32), np.ones((3, 3), np.uint8)))
self.mm_base = cv2.dilate(mm.astype(np.float32), np.ones((3, 3), np.uint8)) + 0.6
self.mm = self.mm_base.copy()
self.hmax = float(self.mm.max()) + 1
def add_solids_to_bounds(self):
self.mm = self.mm_base.copy()
x0, x1, y0, y1 = self.ext
for S in SC.SOLIDS:
lo, hi = S["lo"], S["hi"]
c0 = int((lo[0] - x0) / self.BT)
c1 = int((hi[0] - x0) / self.BT)
r0 = int((y1 - hi[1]) / self.BT)
r1 = int((y1 - lo[1]) / self.BT)
r0, c0 = max(r0 - 1, 0), max(c0 - 1, 0)
self.mm[r0:r1 + 2, c0:c1 + 2] = np.maximum(self.mm[r0:r1 + 2, c0:c1 + 2], hi[2] + 0.5)
self.hmax = float(self.mm.max()) + 1
def town_rc(self, X, Y):
px, py = self.frame.world_to_px(X, Y)
return (py - self.win[1]) - 0.5, (px - self.win[0]) - 0.5
def in_town(self, r, c):
return (r >= 0) & (c >= 0) & (r < self.zh - 1) & (c < self.zw - 1)
def terrain(self, X, Y):
if self.tH is None:
return np.zeros(np.shape(X), np.float32)
return bil(self.tH, (self.ty1 - Y) / self.tc - 0.5, (X - self.tx0) / self.tc - 0.5)
def town_off(self, r, c):
sdf = bil(self.SDF, r, c)
hr = bil(self.HR, r, c)
tr = bil(self.TREE, r, c)
wd = bil(self.WD, r, c)
hb = np.where(sdf < 0, hr, 0.0)
h = np.maximum(hb, tr)
rn = np.clip(np.round(r).astype(int), 0, self.zh - 1)
cn = np.clip(np.round(c).astype(int), 0, self.zw - 1)
dk = self.DECK[rn, cn] > 0
return np.where(h > 0.05, h, np.where(dk, 1.2, -wd))
def height(self, X, Y):
h = self.terrain(X, Y).astype(np.float64)
r, c = self.town_rc(X, Y)
it = self.in_town(r, c)
if it.any():
h[it] = h[it] + self.town_off(r[it], c[it])
return h
def bound(self, X, Y):
x0, x1, y0, y1 = self.ext
r = ((y1 - Y) / self.BT).astype(np.int32)
c = ((X - x0) / self.BT).astype(np.int32)
ins = (r >= 0) & (c >= 0) & (r < self.mm.shape[0]) & (c < self.mm.shape[1])
return np.where(ins, self.mm[np.clip(r, 0, self.mm.shape[0] - 1), np.clip(c, 0, self.mm.shape[1] - 1)], -1e3)
def camera(v, W=2048, H=1152):
yaw, pitch = np.radians(v["yaw"]), np.radians(v.get("pitch", 0.0))
f = np.array([np.sin(yaw) * np.cos(pitch), np.cos(yaw) * np.cos(pitch), np.sin(pitch)])
r = np.cross(f, [0, 0, 1.0])
r /= np.linalg.norm(r)
u = np.cross(r, f)
F = (W / 2) / np.tan(np.radians(v["hfov"] / 2))
cy = H / 2 + v.get("shift", 0.0) * H
return dict(C=np.array(v["pos"], float), f=f, r=r, u=u, F=F, W=W, H=H, cx=W / 2, cy=cy)
def rays(cam):
W, H = cam["W"], cam["H"]
px, py = np.meshgrid(np.arange(W) + 0.5, np.arange(H) + 0.5)
d = (cam["f"][None, None] + ((px - cam["cx"]) / cam["F"])[..., None] * cam["r"]
- ((py - cam["cy"]) / cam["F"])[..., None] * cam["u"])
d = d.reshape(-1, 3)
return d / np.linalg.norm(d, axis=1, keepdims=True)
def project(cam, P):
q = np.asarray(P, float) - cam["C"]
zc = q @ cam["f"]
x = cam["cx"] + cam["F"] * (q @ cam["r"]) / zc
y = cam["cy"] - cam["F"] * (q @ cam["u"]) / zc
return np.stack([x, y], -1), zc
def march(world, O, d, pix, tmax=None, max_iter=20000, tcap=40000.0):
"""First hit of rays O + t d against the heightfield world. Returns hit, t, iterations."""
N = len(d)
O = np.broadcast_to(np.asarray(O, float), (N, 3))
t = np.zeros(N)
tprev = np.zeros(N)
hit = np.zeros(N, bool)
tm = np.full(N, tcap) if tmax is None else np.minimum(tmax, tcap)
dxy = np.hypot(d[:, 0], d[:, 1]) + 1e-6
x0, x1, y0, y1 = world.ext
ext = (x0 + 30, x1 - 30, y0 + 30, y1 - 30)
it = 0
idx = np.arange(N)
while idx.size and it < max_iter:
P = O[idx] + t[idx, None] * d[idx]
x, y, z = P[:, 0], P[:, 1], P[:, 2]
gone = (((z > world.hmax) & (d[idx, 2] >= 0)) | (t[idx] > tm[idx]) | (x < ext[0]) | (x > ext[1])
| (y < ext[2]) | (y > ext[3]))
keep = ~gone
idx, x, y, z = idx[keep], x[keep], y[keep], z[keep]
if not idx.size:
break
b = world.bound(x, y)
far = z > b
fi = idx[far]
if fi.size:
dz = d[fi, 2]
down = np.where(dz < -1e-6, (z[far] - b[far]) / np.maximum(-dz, 1e-6), 1e9)
step = np.minimum(down, world.BT * 0.7 / dxy[fi])
tprev[fi] = t[fi]
t[fi] += np.maximum(step, 0.3)
ni = idx[~far]
keep2 = np.ones(idx.size, bool)
if ni.size:
hz = world.height(x[~far], y[~far])
hh = z[~far] <= hz
hit[ni[hh]] = True
mi = ni[~hh]
if mi.size:
fine = np.maximum(0.12, t[mi] * pix * 0.9)
gap = z[~far][~hh] - hz[~hh]
step = np.maximum(fine, np.minimum(gap * 0.4, 4.0 + t[mi] * pix * 2))
tprev[mi] = t[mi]
t[mi] += step
kk = np.ones(idx.size, bool)
kk[np.nonzero(~far)[0][hh]] = False
keep2 = kk
idx = idx[keep2]
it += 1
hi = np.nonzero(hit)[0]
a, bb = tprev[hi].copy(), t[hi].copy()
for _ in range(12):
m = (a + bb) / 2
P = O[hi] + m[:, None] * d[hi]
below = P[:, 2] <= world.height(P[:, 0], P[:, 1])
bb = np.where(below, m, bb)
a = np.where(below, a, m)
t[hi] = bb
t[~hit] = np.inf
return hit, t, it
def convex_interval(planes, O, d):
n = planes[:, :3]
dd = planes[:, 3]
den = d @ n.T
num = dd[None] - (O @ n.T if O.ndim == 2 else (O @ n.T)[None])
with np.errstate(divide="ignore", invalid="ignore"):
t = num / den
ent = np.where(den < -1e-12, t, -np.inf)
ext = np.where(den > 1e-12, t, np.inf)
bad = (np.abs(den) <= 1e-12) & (num < 0)
te = ent.max(1)
ie = ent.argmax(1)
tx = ext.min(1)
te[bad.any(1)] = np.inf
return te, tx, ie
def solid_hit(S, O, d, eps=1e-4):
if S["kind"] == "sphere":
oc = (O - S["c"]) if O.ndim == 2 else (O - S["c"])[None]
b = (oc * d).sum(1)
cc = (oc * oc).sum(1) - S["r"] ** 2
disc = b * b - cc
sq = np.sqrt(np.maximum(disc, 0))
te = np.where(disc >= 0, -b - sq, np.inf)
P = (O[None] if O.ndim == 1 else O) + np.where(np.isfinite(te), te, 0)[:, None] * d
return np.where(te > eps, te, np.inf), (P - S["c"]) / S["r"]
te, tx, ie = convex_interval(S["planes"], O, d)
ok = (te <= tx) & (te > eps)
return np.where(ok, te, np.inf), S["planes"][ie, :3]
def screen_rect(cam, S, pad=2):
lo, hi = S["lo"], S["hi"]
P = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])])
p, zc = project(cam, P)
if (zc <= 0.05).any():
if (zc <= 0.05).all():
return None
return 0, 0, cam["W"], cam["H"]
x0, y0 = np.floor(p.min(0)).astype(int) - pad
x1, y1 = np.ceil(p.max(0)).astype(int) + pad
x0, y0 = max(0, x0), max(0, y0)
x1, y1 = min(cam["W"], x1), min(cam["H"], y1)
if x1 <= x0 or y1 <= y0:
return None
return x0, y0, x1, y1
def trace_solids(cam, d):
W, H = cam["W"], cam["H"]
N = W * H
tb = np.full(N, np.inf)
nb = np.zeros((N, 3))
ob = np.full(N, -1, np.int32)
grid = np.arange(N).reshape(H, W)
for S in SC.SOLIDS:
rc = screen_rect(cam, S)
if rc is None:
continue
x0, y0, x1, y1 = rc
idx = grid[y0:y1, x0:x1].ravel()
t, nrm = solid_hit(S, cam["C"], d[idx])
better = t < tb[idx]
if better.any():
j = idx[better]
tb[j] = t[better]
nb[j] = nrm[better]
ob[j] = S["oid"]
return tb, nb, ob
def shadow_solids(P, Ld, eps=0.03):
N = P.shape[0]
e1 = np.cross(Ld, [0, 0, 1.0])
e1 /= np.linalg.norm(e1)
e2 = np.cross(Ld, e1)
u, v, w = P @ e1, P @ e2, P @ Ld
blocked = np.zeros(N, bool)
for S in SC.SOLIDS:
lo, hi = S["lo"], S["hi"]
C8 = np.array([[x, y, z] for x in (lo[0], hi[0]) for y in (lo[1], hi[1]) for z in (lo[2], hi[2])])
cu, cv, cw = C8 @ e1, C8 @ e2, C8 @ Ld
sel = (~blocked) & (u >= cu.min()) & (u <= cu.max()) & (v >= cv.min()) & (v <= cv.max()) & (w <= cw.max())
if not sel.any():
continue
j = np.nonzero(sel)[0]
t, _ = solid_hit(S, P[j], np.ascontiguousarray(np.broadcast_to(Ld, (j.size, 3))), eps=eps)
blocked[j[np.isfinite(t)]] = True
return blocked
def sky(d, L, kind):
el = np.degrees(np.arcsin(np.clip(d[:, 2], -1, 1)))
t = np.clip(el / 45.0, 0, 1)[:, None]
if kind in ("day", "morning"):
zen, hor = np.array([118, 154, 200], float), np.array([206, 214, 222], float)
else:
zen, hor = np.array([96, 120, 168], float), np.array([232, 200, 170], float)
col = hor * (1 - t ** 0.55) + zen * t ** 0.55
cs = d @ L
col += np.clip(cs, 0, 1)[:, None] ** 12 * (np.array([60, 40, 10]) if kind != "sunset" else np.array([90, 50, 10]))
return col
def hn(a, bb, s):
return np.sin(a * s + 1.9 * np.sin(bb * s * 0.63)) * np.sin(bb * s * 1.21 + 1.4 * np.sin(a * s * 0.49))
def shade(world, cam, d, kind, suns):
W, H = cam["W"], cam["H"]
N = W * H
C = cam["C"]
L = sun_vec(kind, suns)
pix = 1.0 / cam["F"]
t0 = time.time()
ts, ns, os_ = trace_solids(cam, d)
t1 = time.time()
hit_h, th, iters = march(world, C, d, pix, tmax=np.where(np.isfinite(ts), ts + 0.01, 40000.0))
t2 = time.time()
use_h = hit_h & (th < ts)
t = np.where(use_h, th, ts)
hit = np.isfinite(t)
col = sky(d, L, kind)
cls = np.zeros(N, np.uint8)
idx = np.nonzero(hit)[0]
P = C + t[idx, None] * d[idx]
nrm = np.zeros((idx.size, 3))
base = np.zeros((idx.size, 3))
ccls = np.zeros(idx.size, np.uint8)
hh = use_h[idx]
if hh.any():
Ph = P[hh]
x, y, z = Ph[:, 0], Ph[:, 1], Ph[:, 2]
tt = t[idx][hh]
e = np.maximum(0.06, tt * pix * 0.8)
hx = (world.height(x + e, y) - world.height(x - e, y)) / (2 * e)
hy = (world.height(x, y + e) - world.height(x, y - e)) / (2 * e)
n = np.stack([-hx, -hy, np.ones_like(hx)], 1)
n /= np.linalg.norm(n, axis=1, keepdims=True)
zt = world.terrain(x, y)
rel = z - zt
r, c = world.town_rc(x, y)
it = world.in_town(r, c)
b = np.zeros((hh.sum(), 3))
cl = np.zeros(hh.sum(), np.uint8)
# terrain classes by elevation and slope
slope = np.degrees(np.arctan(np.hypot(hx, hy)))
rn = 0.6 * hn(x, y, 0.012) + 0.4 * hn(y, x, 0.031)
fz = 0.5 + 0.5 * hn(x, y, 0.0021)
tcl = np.full(len(x), CLS_ID["field"], np.uint8)
tcl[(zt > 40) | (slope > 8)] = CLS_ID["field"]
tcl[(slope > 10) & (slope < 38) & (zt > 15) & (zt < 780) & (fz > 0.46)] = CLS_ID["forest"]
tcl[(slope > 36 + 8 * rn)] = CLS_ID["rock"]
tcl[(zt > world.snowline + 180 * rn) & (slope < 42)] = CLS_ID["snow"]
for k, v in ((CLS_ID["field"], PAL["meadow"]), (CLS_ID["forest"], PAL["forest"]), (CLS_ID["rock"], PAL["rock"]),
(CLS_ID["snow"], PAL["snow"])):
b[tcl == k] = v
cl[:] = tcl
tex = 0.5 * hn(x, y + z * 0.7, 0.21) + 0.3 * hn(y, x - z, 0.53) + 0.2 * hn(x + z, y, 1.37)
fo = tcl == CLS_ID["forest"]
b[fo] = b[fo] * (0.78 + 0.3 * tex[fo])[:, None]
rk = tcl == CLS_ID["rock"]
b[rk] = b[rk] * (0.8 + 0.25 * tex[rk])[:, None]
if it.any():
ii = np.nonzero(it)[0]
ri, ci = r[ii], c[ii]
sdf = bil(world.SDF, ri, ci)
rn_ = np.clip(np.round(ri).astype(int), 0, world.zh - 1)
cn_ = np.clip(np.round(ci).astype(int), 0, world.zw - 1)
zcls = world.CLS[rn_, cn_]
zone = world.ZONE[rn_, cn_]
tint = world.TINT[rn_, cn_] / 255.0
eav = bil(world.EAV, ri, ci)
trh = bil(world.TREE, ri, ci)
rl = rel[ii]
house = (sdf < 0.35) & (rl > 0.25) & (trh < rl - 0.3)
treep = (trh > 0.3) & (rl > 0.25) & ~house
wall = house & ((rl < eav - 0.05) | (n[ii, 2] < 0.40))
roofp = house & ~wall
ground = ~house & ~treep
bb = b[ii].copy()
cc = cl[ii].copy()
bb[ground] = PAL["street"]
cc[ground] = CLS_ID["street"]
g_open = ground & (zcls == 8)
bb[g_open] = PAL["field"]
cc[g_open] = CLS_ID["field"]
wat = ground & (rl < -0.6)
bb[wat] = PAL["water"]
cc[wat] = CLS_ID["water"]
dk = ground & (world.DECK[rn_, cn_] > 0) & (rl > 0.5)
bb[dk] = PAL["deck"]
cc[dk] = CLS_ID["deck"]
slopey = ground & (zt[ii] > 1.0)
bb[slopey] = b[ii][slopey]
cc[slopey] = cl[ii][slopey]
zk = np.clip(zone, 1, 4)
for zz in (1, 2, 3, 4):
m = wall & (zk == zz)
bb[m] = np.array(PAL["wall%d" % zz]) * (0.92 + 0.16 * tint[m, None])
m = roofp & (zk == zz)
bb[m] = np.array(PAL["roof%d" % zz]) * (0.92 + 0.16 * tint[m, None])
cc[wall] = CLS_ID["wall"]
cc[roofp] = CLS_ID["roof"]
bb[treep] = PAL["tree"]
cc[treep] = CLS_ID["tree"]
if wall.any():
# a window/door rhythm on the walls: the scale cue the re-surfacing model builds real houses from
wi = np.nonzero(wall)[0]
e2 = 0.4
gx_ = bil(world.SDF, ri[wi], ci[wi] + e2) - bil(world.SDF, ri[wi], ci[wi] - e2)
gy_ = bil(world.SDF, ri[wi] + e2, ci[wi]) - bil(world.SDF, ri[wi] - e2, ci[wi])
tv = np.stack([gy_, gx_], 1)
tv /= (np.linalg.norm(tv, axis=1, keepdims=True) + 1e-9)
Pw = Ph[ii][wi]
s_ = Pw[:, 0] * tv[:, 0] - Pw[:, 1] * tv[:, 1]
zz_ = rl[wi]
ev = eav[wi]
st_ = np.floor((zz_ - 0.3) / 2.8)
zin = (zz_ - 0.3) - st_ * 2.8
fw = (s_ / 3.4) % 1.0
win = (fw > 0.38) & (fw < 0.6) & (zin > 1.0) & (zin < 2.0) & (zz_ < ev - 0.6) & (st_ >= 1)
fd = (s_ / 9.3) % 1.0
door = (fd > 0.45) & (fd < 0.56) & (zz_ < 2.2)
gwin = (fw > 0.4) & (fw < 0.58) & (st_ == 0) & (zin > 1.0) & (zin < 1.8) & ~door
cw = bb[wi]
cw[win | gwin] = (54, 54, 58)
cw[door] = (72, 54, 40)
bb[wi] = cw
wn = np.stack([gx_, -gy_, np.zeros_like(gx_)], 1)
wl = np.linalg.norm(wn, axis=1)
good = wl > 1e-4
nn_ = n[ii].copy()
sub = nn_[wi]
sub[good] = wn[good] / wl[good, None]
nn_[wi] = sub
n[ii] = nn_
b[ii] = bb
cl[ii] = cc
nrm[hh] = n
base[hh] = b
ccls[hh] = cl
sh_ = ~hh
if sh_.any():
j = idx[sh_]
nrm[sh_] = ns[j]
oid = os_[j]
bb = np.zeros((j.size, 3))
for o in np.unique(oid):
bb[oid == o] = SC.MAT[SC.SOLIDS[o]["mat"]]
leaf = np.array([SC.SOLIDS[o]["mat"] == "leaf" for o in oid])
if leaf.any():
Pl = P[sh_][leaf]
hx = ((np.floor(Pl[:, 0] * 2.2).astype(np.int64) * 73856093) ^ (np.floor(Pl[:, 1] * 2.2).astype(np.int64) * 19349663)
^ (np.floor(Pl[:, 2] * 2.2).astype(np.int64) * 83492791))
rr = (hx % 1000) / 1000.0
sub = bb[leaf]
sub *= (0.62 + 0.6 * rr)[:, None]
bb[leaf] = sub
base[sh_] = bb
ccls[sh_] = CLS_ID["solid"]
flip = (nrm * d[idx]).sum(1) > 0
nrm[flip] *= -1
t3 = time.time()
lam = np.clip(nrm @ L, 0, 1)
Q = P + nrm * 0.05 + L * 0.02
li = np.nonzero(lam > 0)[0]
shadow = np.ones(idx.size, bool)
if li.size:
bs = shadow_solids(Q[li], L)
rem = li[~bs]
hs, _, _ = march(world, Q[rem], np.ascontiguousarray(np.broadcast_to(L, (rem.size, 3))), pix * 1.5, tcap=12000.0)
sh = np.ones(li.size, bool)
sh[~bs] = hs
shadow[li] = sh
t4 = time.time()
sky_occ = 0.55 + 0.45 * np.clip(nrm[:, 2] * 0.5 + 0.5, 0, 1)
if kind in ("day", "morning"):
amb = AMB[None] * (0.50 * sky_occ)[:, None]
sun = SUNC[None] * (0.80 * lam * ~shadow)[:, None]
else:
amb = np.array([0.66, 0.70, 0.92])[None] * (0.42 * sky_occ)[:, None]
sun = np.array([1.0, 0.70, 0.45])[None] * (0.95 * lam * ~shadow)[:, None]
cc = base * (amb + sun)
wat = ccls == CLS_ID["water"]
if wat.any():
cc[wat] = cc[wat] * 0.7 + sky(np.c_[d[idx][wat][:, :2], -d[idx][wat][:, 2]], L, kind) * 0.3
dist = t[idx]
fog = 1 - np.exp(-dist / (16000.0 if kind != "sunset" else 12000.0))
hz = np.array([200, 208, 218]) if kind != "sunset" else np.array([206, 182, 168])
cc = cc * (1 - fog[:, None]) + hz * fog[:, None]
col[idx] = cc
cls[idx] = ccls
img = col.reshape(H, W, 3).clip(0, 255).astype(np.uint8)
depth = np.where(hit, t, np.inf).reshape(H, W).astype(np.float32)
oid = np.where(hit & ~use_h, os_ + 1, 0).reshape(H, W).astype(np.uint16)
print(" solids %.0fs march %.0fs (%d it) shade %.0fs shadows %.0fs" % (t1 - t0, t2 - t1, iters, t3 - t2, t4 - t3), flush=True)
return img, depth, oid, cls.reshape(H, W)
def people_table(cam):
out = []
for p in SC.PEOPLE:
base = np.array([p["x"], p["y"], p["z0"]])
top = base + [0, 0, p["h"]]
(pb, pt), zc = project(cam, np.stack([base, top]))
if (zc <= 0.3).any():
continue
x = (pb[0] + pt[0]) / 2
if not (0 <= x < cam["W"] and min(pb[1], pt[1]) < cam["H"] and max(pb[1], pt[1]) > 0):
continue
hpx = pb[1] - pt[1]
out.append(dict(label=p["label"], h_m=round(p["h"], 3), dist_m=round(float(zc[0]), 1), x=round(float(x), 1),
y_foot=round(float(pb[1]), 1), y_head=round(float(pt[1]), 1), px_height=round(float(hpx), 1),
px_per_m=round(float(hpx / p["h"]), 2)))
return out
def landmarks(cam, scene):
L = {}
for lm in scene.get("landmarks", []):
p, zc = project(cam, np.array([lm["xyz"]]))
if zc[0] > 0:
L[lm["name"]] = [round(float(p[0, 0]), 1), round(float(p[0, 1]), 1), round(float(zc[0]), 1)]
return L
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("views", nargs="+")
ap.add_argument("--config", required=True)
ap.add_argument("--views-file", default=None, help="defaults to config.views")
ap.add_argument("--out", default=None, help="defaults to config.plates_dir")
ap.add_argument("--w", type=int, default=2048)
ap.add_argument("--sun", default=None, help="override the view's sun: day|sunset|morning")
a = ap.parse_args()
cfg = load_config(a.config)
VIEWS = json.load(open(resolve(cfg, a.views_file or cfg["views"])))
outdir = resolve(cfg, a.out or cfg.get("plates_dir", "plates"))
os.makedirs(outdir, exist_ok=True)
world = World(cfg)
Hpx = a.w * 9 // 16
suns = cfg.get("suns", {})
for k in a.views:
v = VIEWS[k]
t0 = time.time()
SC.reset()
scene_path = resolve(cfg, v.get("scene", cfg.get("scene")))
scene = json.load(open(scene_path)) if scene_path and os.path.exists(scene_path) else {}
SC.build_from_json(scene, ground_fn=world.terrain)
world.add_solids_to_bounds()
cam = camera(v, a.w, Hpx)
d = rays(cam)
kv = a.sun or v.get("sun", "day")
img, depth, oid, cls = shade(world, cam, d, kv, suns)
sfx = "" if a.w == 2048 else "_%d" % a.w
Image.fromarray(img).save(os.path.join(outdir, "%s_mass%s.png" % (k, sfx)))
np.save(os.path.join(outdir, "%s_depth%s.npy" % (k, sfx)), depth)
cv2.imwrite(os.path.join(outdir, "%s_oid%s.png" % (k, sfx)), oid)
Image.fromarray(cls).save(os.path.join(outdir, "%s_cls%s.png" % (k, sfx)))
save_json({kk: vv for kk, vv in cam.items()}, os.path.join(outdir, "%s_cam%s.json" % (k, sfx)))
save_json(people_table(cam), os.path.join(outdir, "%s_people%s.json" % (k, sfx)))
save_json(landmarks(cam, scene), os.path.join(outdir, "%s_landmarks%s.json" % (k, sfx)))
print(k, kv, "%dx%d" % (a.w, Hpx), "done %.0fs" % (time.time() - t0), flush=True)
+238
View File
@@ -0,0 +1,238 @@
"""Analytic convex solids for hand-modeled landmarks and true-scale proxies (people, carts, horses, trees).
Everything is a convex polyhedron (half-space planes) or a sphere, so a ray hit is one interval test.
A scene file (JSON) lists solids in world meters; see examples/scene.example.json. Kinds:
box {c:[x,y,z], size:[sx,sy,sz], heading?:deg, mat, group}
prism {poly:[[x,y],...], z0, z1, mat, group} (vertical extrusion of a convex polygon)
cone {c:[x,y], r0, z0, z1, mat, group} (round tower roof; faceted)
gable {p0:[x,y], p1:[x,y], width, z_eave, rise, mat_wall, mat_roof, group} (house: walls + roof prism)
person {xy:[x,y], z0?, height?:1.72, heading?:deg, robe?:bool, cloth?:int, label?}
cart {xy, z0?, heading, load?:hay|sacks}
horse {xy, z0?, heading, pack?:bool}
tree {xy, z0?, h?:9, r?:3.2, conifer?:bool}
Proxy people are the point of the method: they are the scale anchors the re-surfacing model dresses in place.
"""
import numpy as np
MAT = dict(
stone=(176, 172, 164), stone_dk=(150, 146, 138), stone_lt=(194, 190, 182), slate=(82, 86, 94), lead=(116, 120, 126),
timber=(146, 126, 102), timber_dk=(118, 100, 80), plank=(156, 136, 108), shingle=(96, 84, 72), turf=(98, 110, 72),
oak=(78, 58, 42), skin=(198, 160, 134), legs=(70, 62, 54), leaf=(74, 98, 60), trunk=(84, 68, 54), wheel=(92, 74, 56),
canvas=(170, 160, 136), horse=(104, 78, 58), horse_dk=(70, 54, 42), hay=(176, 156, 98), sack=(160, 146, 118),
iron=(70, 70, 72),
)
CLOTH = [(92, 70, 52), (118, 54, 44), (56, 66, 92), (96, 98, 92), (70, 86, 60), (150, 138, 112), (60, 52, 48),
(128, 98, 64), (84, 60, 78), (110, 112, 118), (140, 120, 90), (74, 80, 96)]
for _i, _c in enumerate(CLOTH):
MAT["cloth%d" % _i] = _c
SOLIDS = []
GROUPS = {}
PEOPLE = []
def reset():
SOLIDS.clear()
GROUPS.clear()
PEOPLE.clear()
def add(planes, pts, mat, group):
oid = len(SOLIDS)
p = np.asarray(pts, float)
SOLIDS.append(dict(kind="convex", planes=np.asarray(planes, float), lo=p.min(0) - 0.05, hi=p.max(0) + 0.05,
mat=mat, oid=oid, group=group))
GROUPS.setdefault(group, []).append(oid)
return oid
def add_sphere(c, r, mat, group):
oid = len(SOLIDS)
c = np.asarray(c, float)
SOLIDS.append(dict(kind="sphere", c=c, r=float(r), lo=c - r, hi=c + r, mat=mat, oid=oid, group=group))
GROUPS.setdefault(group, []).append(oid)
return oid
def _planes_box(c, ax, hx):
P = []
for a, h in zip(ax, hx):
a = np.asarray(a, float)
P.append(np.r_[a, a @ c + h])
P.append(np.r_[-a, -(a @ c) + h])
return np.array(P)
def box_pts(c, ax, hx):
c = np.asarray(c, float)
ax = np.asarray(ax, float)
return np.array([c + sx * hx[0] * ax[0] + sy * hx[1] * ax[1] + sz * hx[2] * ax[2]
for sx in (-1, 1) for sy in (-1, 1) for sz in (-1, 1)])
def add_box(c, ax, hx, mat, group):
c = np.asarray(c, float)
return add(_planes_box(c, ax, hx), box_pts(c, ax, hx), mat, group)
def axes(heading_deg):
b = np.radians(heading_deg)
f = np.array([np.sin(b), np.cos(b), 0.0])
s = np.array([f[1], -f[0], 0.0])
return s, f, np.array([0, 0, 1.0])
def ccw(poly):
P = np.asarray(poly, float)
a = 0.5 * np.sum(P[:, 0] * np.roll(P[:, 1], -1) - np.roll(P[:, 0], -1) * P[:, 1])
return P if a > 0 else P[::-1]
def prism_planes(poly, z0, z1):
P = ccw(poly)
pl = [[0, 0, -1, -z0], [0, 0, 1, z1]]
for i in range(len(P)):
a, b = P[i], P[(i + 1) % len(P)]
e = b - a
n = np.array([e[1], -e[0], 0.0])
n /= np.linalg.norm(n)
pl.append(np.r_[n, n @ np.r_[a, 0]])
pts = [np.r_[p, z0] for p in P] + [np.r_[p, z1] for p in P]
return np.array(pl), pts
def add_prism(poly, z0, z1, mat, group):
pl, pts = prism_planes(poly, z0, z1)
return add(pl, pts, mat, group)
def add_cone(cx, cy, r0, z0, z1, mat, group, n=24, rtop=0.05):
th = np.linspace(0, 2 * np.pi, n, endpoint=False)
pl = [[0, 0, -1, -z0]]
h = z1 - z0
for t in th:
nx, ny = np.cos(t), np.sin(t)
nn = np.array([nx * h, ny * h, r0 - rtop])
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ np.array([cx + nx * r0, cy + ny * r0, z0])])
pts = [[cx + r0 * np.cos(t), cy + r0 * np.sin(t), z0] for t in th] + [[cx, cy, z1]]
return add(np.array(pl), pts, mat, group)
def add_gable(p0, p1, width, z_eave, rise, mat_wall, mat_roof, group, z0=0.0, overhang=0.35):
"""A house: walls as a box under the eaves, a roof prism with its ridge from p0 to p1."""
p0, p1 = np.asarray(p0, float), np.asarray(p1, float)
d = p1 - p0
L = np.linalg.norm(d)
d /= L
nrm = np.array([-d[1], d[0]])
c = (p0 + p1) / 2
ax = [np.r_[d, 0], np.r_[nrm, 0], [0, 0, 1.0]]
add_box(np.r_[c, z0 + (z_eave - z0) / 2], ax, [L / 2, width / 2, (z_eave - z0) / 2], mat_wall, group)
w2 = width / 2 + overhang
pl = [[0, 0, -1, -z_eave], np.r_[ax[0], ax[0] @ np.r_[c, 0] + L / 2 + overhang],
np.r_[-ax[0], -(ax[0] @ np.r_[c, 0]) + L / 2 + overhang]]
for sgn in (1, -1):
nn = np.r_[sgn * nrm * rise, w2]
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ np.r_[c + sgn * nrm * w2, z_eave]])
pts = [np.r_[c + a * d * (L / 2 + overhang) + s * nrm * w2, z_eave] for a in (-1, 1) for s in (-1, 1)]
pts += [np.r_[c + a * d * (L / 2 + overhang), z_eave + rise] for a in (-1, 1)]
add(np.array(pl), pts, mat_roof, group)
def person(xy, z0, height=1.72, heading=0.0, robe=False, cloth=0, group="people", stride=0.0, label=""):
"""A mannequin at true scale: legs, torso, head. Recorded in PEOPLE for the figure-scale gate."""
h = float(height)
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
col = "cloth%d" % (cloth % len(CLOTH))
k = h / 1.72
leg, tor, hr, sh_w = 0.47 * h, 0.30 * h, 0.062 * h, 0.12 * h
if robe:
zt = leg + tor
hw0, hw1, hd0, hd1 = 0.30 * k, sh_w, 0.20 * k, 0.12 * k
pl = [[0, 0, -1, -z0], [0, 0, 1, z0 + zt]]
for v, w0, w1 in ((s, hw0, hw1), (-s, hw0, hw1), (f, hd0, hd1), (-f, hd0, hd1)):
nn = v * zt + z * (w0 - w1)
nn /= np.linalg.norm(nn)
pl.append(np.r_[nn, nn @ (c0 + v * w0)])
pts = [c0 + a * hw0 + bb * hd0 for a in (s, -s) for bb in (f, -f)] + [c0 + z * zt + a * hw1 for a in (s, -s)]
add(np.array(pl), pts, col, group)
else:
for side in (-1, 1):
add_box(c0 + s * side * 0.075 * k + f * stride * side * 0.5 + z * (leg / 2), ax,
[0.065 * k, 0.08 * k, leg / 2], "legs", group)
add_box(c0 + z * (leg + tor / 2 - 0.02), ax, [sh_w, 0.12 * k, tor / 2 + 0.04], col, group)
for side in (-1, 1):
add_box(c0 + s * side * (sh_w + 0.05 * k) + z * (leg + tor * 0.55), ax, [0.045 * k, 0.05 * k, tor * 0.42], col, group)
add_sphere(c0 + z * (h - hr), hr, "skin", group)
PEOPLE.append(dict(x=float(c0[0]), y=float(c0[1]), z0=float(z0), h=h, label=label or group))
def cart(xy, z0, heading, load="hay", group="carts"):
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
add_box(c0 + z * 1.0, ax, [0.75, 1.3, 0.25], "plank", group)
add_box(c0 + z * (1.25 + 0.45), ax, [0.7, 1.2, 0.45], "hay" if load == "hay" else "sack", group)
for side in (-1, 1):
add_box(c0 + s * side * 0.9 + z * 0.65, ax, [0.05, 0.65, 0.65], "wheel", group)
return c0 + f * 1.3
def horse(xy, z0, heading, pack=False, group="animals"):
s, f, z = axes(heading)
ax = [s, f, z]
c0 = np.r_[np.asarray(xy, float)[:2], z0]
add_box(c0 + z * 1.15, ax, [0.3, 0.95, 0.32], "horse", group)
add_box(c0 + f * 1.1 + z * 1.45, ax, [0.15, 0.35, 0.3], "horse_dk", group)
for sx in (-1, 1):
for sy in (-1, 1):
add_box(c0 + s * sx * 0.18 + f * sy * 0.6 + z * 0.42, ax, [0.07, 0.07, 0.42], "horse_dk", group)
if pack:
add_box(c0 + z * 1.65, ax, [0.4, 0.5, 0.2], "sack", group)
def tree(xy, z0, h=9.0, r=3.2, conifer=False, group="trees"):
x, y = float(xy[0]), float(xy[1])
add_box([x, y, z0 + h * 0.2], [[1, 0, 0], [0, 1, 0], [0, 0, 1]], [0.18, 0.18, h * 0.2], "trunk", group)
if conifer:
add_cone(x, y, r, z0 + h * 0.25, z0 + h, "leaf", group, n=12)
else:
add_sphere([x, y, z0 + h * 0.65], r, "leaf", group)
def build_from_json(scene, ground_fn=None):
"""Populate SOLIDS from a scene dict. `ground_fn(x, y) -> z` places proxies on the terrain when z0 is absent."""
def z0_of(e):
if "z0" in e:
return float(e["z0"])
if ground_fn is None:
return 0.0
return float(ground_fn(np.array([e["xy"][0]]), np.array([e["xy"][1]]))[0])
for e in scene.get("solids", []):
k = e["kind"]
g = e.get("group", k)
if k == "box":
s, f, z = axes(e.get("heading", 0.0))
add_box(e["c"], [s, f, z], [v / 2 for v in e["size"]], e.get("mat", "stone"), g)
elif k == "prism":
add_prism(e["poly"], e["z0"], e["z1"], e.get("mat", "stone"), g)
elif k == "cone":
add_cone(e["c"][0], e["c"][1], e["r0"], e["z0"], e["z1"], e.get("mat", "slate"), g)
elif k == "gable":
add_gable(e["p0"], e["p1"], e["width"], e["z_eave"], e["rise"], e.get("mat_wall", "stone"),
e.get("mat_roof", "slate"), g, z0=e.get("z0", 0.0))
elif k == "person":
person(e["xy"], z0_of(e), e.get("height", 1.72), e.get("heading", 0.0), e.get("robe", False),
e.get("cloth", 0), "people", e.get("stride", 0.0), e.get("label", "person"))
elif k == "cart":
cart(e["xy"], z0_of(e), e.get("heading", 0.0), e.get("load", "hay"))
elif k == "horse":
horse(e["xy"], z0_of(e), e.get("heading", 0.0), e.get("pack", False))
elif k == "tree":
tree(e["xy"], z0_of(e), e.get("h", 9.0), e.get("r", 3.2), e.get("conifer", False))
else:
raise ValueError("unknown solid kind: %s" % k)
+102
View File
@@ -0,0 +1,102 @@
#!/usr/bin/env python3
"""Segment a hand-drawn town map bitmap into roof, tree and water masks.
Tuned for the common "parchment with dark roofs, cauliflower tree glyphs, teal water" style of map exporters.
Every threshold is a flag so you can tune it to your own map; `--look` writes a check image you MUST eyeball
before building the massing (false "trees" that are really roof detail are the usual miss).
usage: segment_map.py MAP.png --mpp 0.5 --out seg.npz [--look look.jpg] [--roof-max 184] [--tree-glyph-px 5]
"""
import argparse
import cv2
import numpy as np
from PIL import Image
from citykit import ell
Image.MAX_IMAGE_PIXELS = None
def lstd(g, k):
return np.sqrt(np.maximum(cv2.blur(g * g, (k, k)) - cv2.blur(g, (k, k)) ** 2, 0))
def segment(rgb, mpp, roof_max=184.0, tree_glyph_px=5, tree_min_m2=8.0, tree_max_m2=700.0, roof_min_m2=11.0,
water_gr=22, water_br=12):
"""Return (roof, tree, water) uint8 masks. `mpp` scales the morphology windows so the same flags work across
export resolutions (the reference tuning was at 0.53 m/px)."""
s = 0.53 / mpp # window scale relative to the reference tuning
a = rgb.astype(np.float32)
R, G, B = a[..., 0], a[..., 1], a[..., 2]
g = 0.3 * R + 0.59 * G + 0.11 * B
gb = cv2.blur(g, (3, 3))
# trees: dense short dark strokes (blackhat line density) in compact blobs
bh = cv2.morphologyEx(g, cv2.MORPH_BLACKHAT, ell(tree_glyph_px * s))
ld = cv2.blur((bh > 12).astype(np.float32), (int(9 * s) | 1,) * 2)
tr = ((ld > 0.45) & (cv2.blur(g, (int(9 * s) | 1,) * 2) < 175)).astype(np.uint8)
tr = cv2.morphologyEx(tr, cv2.MORPH_OPEN, ell(5 * s))
tr = cv2.morphologyEx(tr, cv2.MORPH_CLOSE, ell(3 * s))
n, lab, st, _ = cv2.connectedComponentsWithStats(tr, 8)
keep = np.zeros(n, bool)
for i in range(1, n):
x, y, w, h, ar = st[i]
area_m2 = ar * mpp * mpp
if area_m2 < tree_min_m2 or area_m2 > tree_max_m2:
continue
if max(w, h) > 2.6 * min(w, h):
continue
if ar / float(w * h) < 0.4:
continue
keep[i] = True
tree = cv2.dilate(keep[lab].astype(np.uint8), ell(3 * s))
# roofs: darker than the parchment and not a tree; fill small holes; strip the 1 px cast-shadow halo
roof = ((gb < roof_max) & (cv2.dilate(tree, ell(3 * s)) == 0)).astype(np.uint8)
roof = cv2.morphologyEx(roof, cv2.MORPH_OPEN, ell(3 * s))
roof = cv2.morphologyEx(roof, cv2.MORPH_CLOSE, ell(3 * s))
inv = (1 - roof).astype(np.uint8)
n, lab, st, _ = cv2.connectedComponentsWithStats(inv, 4)
hole = np.zeros(n, bool)
hole[1:] = st[1:, 4] * mpp * mpp < 25.0
roof[hole[lab] & (tree == 0)] = 1
roof = cv2.erode(roof, ell(3 * s))
n, lab, st, _ = cv2.connectedComponentsWithStats(roof, 8)
keep = np.zeros(n, bool)
keep[1:] = st[1:, 4] * mpp * mpp >= roof_min_m2
roof = keep[lab].astype(np.uint8)
water = ((G - R > water_gr) & (B - R > water_br)).astype(np.uint8)
water = cv2.morphologyEx(water, cv2.MORPH_OPEN, ell(3 * s))
water = cv2.morphologyEx(water, cv2.MORPH_CLOSE, ell(5 * s))
roof[water > 0] = 0
tree[water > 0] = 0
return roof, tree, water
def look_image(rgb, roof, tree, water):
a = rgb.copy()
a[roof > 0] = (a[roof > 0] * 0.45 + np.array([255, 0, 0]) * 0.55).astype(np.uint8)
a[tree > 0] = (a[tree > 0] * 0.45 + np.array([0, 200, 0]) * 0.55).astype(np.uint8)
a[water > 0] = (a[water > 0] * 0.45 + np.array([0, 80, 255]) * 0.55).astype(np.uint8)
return np.concatenate([rgb, a], 1)
if __name__ == "__main__":
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("map")
ap.add_argument("--mpp", type=float, required=True, help="meters per bitmap pixel")
ap.add_argument("--out", default="seg.npz")
ap.add_argument("--look", default=None, help="write a before/after check image (downscaled to 2400 px wide)")
ap.add_argument("--roof-max", type=float, default=184.0, help="gray level below which a pixel can be roof")
ap.add_argument("--tree-glyph-px", type=int, default=5)
a = ap.parse_args()
im = np.asarray(Image.open(a.map).convert("RGB"))
roof, tree, water = segment(im, a.mpp, a.roof_max, a.tree_glyph_px)
np.savez_compressed(a.out, roof=roof, tree=tree, water=water, mpp=np.float32(a.mpp))
print("roof %.4f tree %.4f water %.4f of the bitmap" % (roof.mean(), tree.mean(), water.mean()))
if a.look:
lk = look_image(im, roof, tree, water)
sc = min(1.0, 2400 / lk.shape[1])
if sc < 1:
lk = cv2.resize(lk, None, fx=sc, fy=sc, interpolation=cv2.INTER_AREA)
Image.fromarray(lk).save(a.look, quality=88)
print("check image:", a.look)
+167
View File
@@ -0,0 +1,167 @@
#!/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()