Ring code: far/washed projector bench, multi-scale locator with ring check
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
// far / low contrast bench: stills with per-stage diagnosis, and timed sequences
|
||||
import * as RC from "/static/js/ringcode.js";
|
||||
import { lcg, makeRoom, frame, jpeg } from "/static/ringlab/synth.js";
|
||||
|
||||
import * as V1 from "/static/ringlab/ringcode_v1.js";
|
||||
|
||||
const { decodeFrame, grayOf, locate, decodeAt, RADII, CENTER, _lab } = RC;
|
||||
|
||||
function grab(canvas, maxW) {
|
||||
const k = Math.min(1, maxW / canvas.width);
|
||||
if (k === 1) return { img: grayOf(canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height)), k };
|
||||
const c = document.createElement("canvas"); c.width = Math.round(canvas.width * k); c.height = Math.round(canvas.height * k);
|
||||
const ctx = c.getContext("2d", { willReadFrequently: true });
|
||||
ctx.drawImage(canvas, 0, 0, c.width, c.height);
|
||||
return { img: grayOf(ctx.getImageData(0, 0, c.width, c.height)), k };
|
||||
}
|
||||
|
||||
// pipelines under test: canvas -> { token, ... }
|
||||
const scanners = {};
|
||||
const pipes = {
|
||||
legacy: (canvas) => V1.decodeFrame(grab(canvas, 960).img), // what vote.js shipped with
|
||||
cap960: (canvas, t) => decodeFrame(grab(canvas, 960).img, t.opts || {}),
|
||||
native: (canvas, t) => decodeFrame(grab(canvas, 1e9).img, t.opts || {}),
|
||||
scan: (canvas, t, key) => {
|
||||
const s = (scanners[key] ||= window.makeRingScanner(t.scanOpts || {}));
|
||||
return s.scan(canvas, canvas.width, canvas.height, t.now ?? null);
|
||||
},
|
||||
};
|
||||
|
||||
// where does a failed frame break? substitutes the truth stage by stage
|
||||
function diagnose(img, k, truth, t) {
|
||||
const T = (x, y) => { const [u, v] = truth(x, y); return [(u + 0.5) * k - 0.5, (v + 0.5) * k - 0.5]; };
|
||||
const [cx, cy] = T(0, 0), [ex, ey] = T(CENTER, 0), rpx = Math.hypot(ex - cx, ey - cy);
|
||||
const out = { rpx: +rpx.toFixed(1) };
|
||||
const cands = locate(img, { max: 60 });
|
||||
out.rank = cands.findIndex((c) => Math.hypot(c.cx - cx, c.cy - cy) < 0.3 * rpx);
|
||||
out.ncand = cands.length;
|
||||
if (out.rank >= 0) { const r = decodeAt(img, cands[out.rank], {}); out.candOk = !!(r && r.token === t.token); out.candScore = r ? +(+r.score).toFixed(2) : null; }
|
||||
const S = (x, y) => { const [u, v] = T(x, y); return _lab.bilinear(img, u, v); };
|
||||
const r = _lab.readRings(S, {});
|
||||
out.oracleGeo = !!(r && r.token === t.token);
|
||||
out.oracleScore = r && r.score != null ? +r.score.toFixed(2) : null;
|
||||
// true geometry and rotation: is the tone of each dash still separable?
|
||||
const rings = RC.ringsOf(t.bits);
|
||||
let bitErr = 0, idealErr = 0; const dp = [], byteBits = [];
|
||||
rings.forEach((ring, ri) => {
|
||||
const n = ring.length, step = (2 * Math.PI) / n, vals = [];
|
||||
for (let j = 0; j < n; j++) vals.push(_lab.sector(S, RADII[ri], _lab.sectorAngle(t.theta, j, n), step));
|
||||
const dark = vals.filter((_, j) => ring[j] === "1"), light = vals.filter((_, j) => ring[j] === "0");
|
||||
const m = (a) => a.reduce((x, y) => x + y, 0) / a.length, sd = (a) => Math.sqrt(m(a.map((v) => (v - m(a)) ** 2)));
|
||||
dp.push(+((m(light) - m(dark)) / (Math.hypot(sd(light), sd(dark)) / Math.SQRT2 || 1)).toFixed(1));
|
||||
if (ri === 0) { out.span = +(m(light) - m(dark)).toFixed(0); return; }
|
||||
const sp = _lab.split(vals);
|
||||
let best = n;
|
||||
for (const thr of vals) { let e = 0; vals.forEach((v, j) => { e += (v <= thr ? "1" : "0") !== ring[j]; }); best = Math.min(best, e); }
|
||||
idealErr += best;
|
||||
vals.forEach((v, j) => { const e = (v < sp.thr ? "1" : "0") !== ring[j]; bitErr += e; byteBits.push(e); });
|
||||
});
|
||||
let byteErr = 0;
|
||||
for (let i = 0; i < 96; i += 8) byteErr += byteBits.slice(i, i + 8).some(Boolean);
|
||||
Object.assign(out, { dprime: dp, bitErr, idealErr, byteErr });
|
||||
out.stage = out.rank < 0 || out.rank >= 4 ? "locate" : out.oracleGeo ? "geometry" : byteErr <= 3 ? "sync/thr" : "tone";
|
||||
return out;
|
||||
}
|
||||
|
||||
window.runFar = async (cfg) => {
|
||||
const rnd = lcg(cfg.seed || 4242), results = [];
|
||||
for (const t of cfg.trials) {
|
||||
const room = makeRoom(t.W, t.H, t.room, rnd);
|
||||
let f = frame(room, t, rnd);
|
||||
if (t.jpeg) f = { ...f, canvas: await jpeg(f.canvas, t.jpeg) };
|
||||
const res = { ...t, bits: undefined };
|
||||
for (const name of cfg.pipes) {
|
||||
for (const key of Object.keys(scanners)) delete scanners[key];
|
||||
const t0 = performance.now();
|
||||
const d = pipes[name](f.canvas, t, name);
|
||||
const ms = performance.now() - t0;
|
||||
res[name] = { ok: t.nocode ? !d.token : d.token === t.token, token: d.token || null, ms: +ms.toFixed(1), found: d.found, score: d.score == null ? null : +(+d.score).toFixed(2) };
|
||||
}
|
||||
if (cfg.diag && !t.nocode) {
|
||||
const { img, k } = grab(f.canvas, cfg.diag === "legacy" ? 960 : 1e9);
|
||||
res.diag = diagnose(img, k, f.truth, t);
|
||||
}
|
||||
if (cfg.keep && results.length < cfg.keep) res.png = f.canvas.toDataURL("image/jpeg", 0.9);
|
||||
results.push(res);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
|
||||
// a phone held for a few seconds: rotation, token switches with cross-fade, shake, per-frame noise.
|
||||
// seq: frame params + { fps, secs, interval, tokens: [{ bits, token }], shake }
|
||||
window.runSeq = async (cfg) => {
|
||||
const rnd = lcg(cfg.seed || 777), results = [];
|
||||
for (const s of cfg.seqs) {
|
||||
const room = makeRoom(s.W, s.H, s.room, rnd);
|
||||
const res = { ...s, tokens: undefined };
|
||||
for (const name of cfg.pipes) res[name] = { got: [], false: 0, frames: 0, ms: 0, first5: null };
|
||||
for (const key of Object.keys(scanners)) delete scanners[key];
|
||||
let px = 0, py = 0;
|
||||
const fadeMs = Math.min(220, s.interval * 0.25), t00 = s.t0 || 0;
|
||||
for (let i = 0; i < s.fps * s.secs; i++) {
|
||||
const ms = t00 + (i * 1000) / s.fps + rnd() * 20, slot = Math.floor(ms / s.interval), idx = slot - Math.floor(t00 / s.interval);
|
||||
const cur = s.tokens[idx], prev = idx > 0 ? s.tokens[idx - 1] : null, since = ms - slot * s.interval;
|
||||
// hand shake: drifting offset plus a random smear for this exposure
|
||||
px = px * 0.8 + (rnd() - 0.5) * 2 * s.shake * 3; py = py * 0.8 + (rnd() - 0.5) * 2 * s.shake * 3;
|
||||
const t = { ...s, bits: cur.bits, token: cur.token, theta: (ms / 1000) * (2 * Math.PI / 45), anim: ms / 1000,
|
||||
fade: prev && since < fadeMs ? { from: prev.bits, k: since / fadeMs } : null,
|
||||
dx: (s.dx || 0) + px * (s.W / 1280), dy: (s.dy || 0) + py * (s.W / 1280),
|
||||
motion: [(rnd() - 0.5) * 2 * s.shake, (rnd() - 0.5) * 2 * s.shake], now: ms };
|
||||
const f = frame(room, t, rnd);
|
||||
for (const name of cfg.pipes) {
|
||||
const r = res[name], t0 = performance.now();
|
||||
const d = pipes[name](f.canvas, t, name + results.length);
|
||||
r.ms += performance.now() - t0; r.frames++;
|
||||
if (!d.token) continue;
|
||||
// late reads of an older token are fine (accumulation), anything never shown is a false token
|
||||
if (!s.tokens.slice(0, idx + 1).some((k) => k.token === d.token)) r.false++;
|
||||
else if (!r.got.includes(d.token)) { r.got.push(d.token); if (r.got.length === 5) r.first5 = +((ms - t00) / 1000).toFixed(2); }
|
||||
}
|
||||
}
|
||||
for (const name of cfg.pipes) { const r = res[name]; r.ms = +(r.ms / r.frames).toFixed(1); r.got = r.got.length; }
|
||||
results.push(res);
|
||||
}
|
||||
return results;
|
||||
};
|
||||
Reference in New Issue
Block a user