Ring decoder: coarse-to-fine sync search (2x faster), lab phone at 960px, bench cpu throttling

This commit is contained in:
2026-11-16 18:27:34 +01:00
parent fe1da7ea1c
commit d29ca34b9a
4 changed files with 30 additions and 13 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

+22 -11
View File
@@ -593,18 +593,29 @@ export function decodeAt(img, cand, opts = {}) {
} }
function readRings(S, opts) { function readRings(S, opts) {
const steps = opts.steps || 360, minScore = opts.minScore ?? 0.8; const minScore = opts.minScore ?? 0.8;
const n0 = RING_SECTORS[0], step0 = (2 * Math.PI) / n0;
const syncAt = (theta, scale) => {
const vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, RADII[0] * scale, sectorAngle(theta, j, n0), step0));
return vals.some(Number.isNaN) ? null : { score: corr(vals, SYNC_BITS), theta, scale, vals };
};
// coarse pass over the full turn (no rotational symmetry), then a fine pass around the best hits.
// ~5x fewer samples than a dense search, which matters on low-end phones
const coarse = [];
for (const scale of opts.scales || [0.96, 1.0, 1.04]) {
for (let t = 0; t < 72; t++) {
const r = syncAt((t / 72) * 2 * Math.PI, scale);
if (!r) return null;
coarse.push(r);
}
}
coarse.sort((p, q) => q.score - p.score);
let best = null; let best = null;
for (const scale of opts.scales || [0.95, 0.975, 1.0, 1.025, 1.05]) { for (const c of coarse.slice(0, 3)) {
const r0 = RADII[0] * scale, n0 = RING_SECTORS[0], step0 = (2 * Math.PI) / n0; for (const ds of [-0.02, 0, 0.02]) for (let dt = -10; dt <= 10; dt++) {
// full turn: the sync pattern has no rotational symmetry const r = syncAt(c.theta + (dt * 0.5 * Math.PI) / 180, c.scale + ds);
for (let t = 0; t < steps; t++) { if (r && (!best || r.score > best.score)) best = r;
const theta = (t / steps) * 2 * Math.PI;
const vals = [];
for (let j = 0; j < n0; j++) vals.push(sector(S, r0, sectorAngle(theta, j, n0), step0));
if (vals.some(Number.isNaN)) return null;
const c = corr(vals, SYNC_BITS);
if (!best || c > best.score) best = { score: c, theta, scale, vals };
} }
} }
// weak sync means we're not looking at a ring code (or a bad frame): don't let rs "fix" noise // weak sync means we're not looking at a ring code (or a bad frame): don't let rs "fix" noise
+2 -1
View File
@@ -26,7 +26,8 @@ async function start() {
async function loop() { async function loop() {
if (!running) return; if (!running) return;
if (video.readyState >= 2) { if (video.readyState >= 2) {
const k = Math.min(1, 1280 / video.videoWidth); // 960 px is plenty for the locator and keeps capture cheap on low-end phones
const k = Math.min(1, 960 / video.videoWidth);
canvas.width = Math.round(video.videoWidth * k); canvas.width = Math.round(video.videoWidth * k);
canvas.height = Math.round(video.videoHeight * k); canvas.height = Math.round(video.videoHeight * k);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height); ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
+6 -1
View File
@@ -60,6 +60,10 @@ async def main():
b = Browser(9336) b = Browser(9336)
await b.start() await b.start()
p = await b.page(960, 720) p = await b.page(960, 720)
throttle = float(os.environ.get("CPU", "1"))
if throttle > 1:
# rough low-end phone: chrome devtools cpu slowdown factor
await p.send("Emulation.setCPUThrottlingRate", {"rate": throttle})
await p.goto(f"http://127.0.0.1:{PORT}/static/ringlab/bench.html") await p.goto(f"http://127.0.0.1:{PORT}/static/ringlab/bench.html")
for _ in range(50): for _ in range(50):
if await p.js("!!window.benchReady"): if await p.js("!!window.benchReady"):
@@ -77,7 +81,8 @@ async def main():
a[0] += 1; a[1] += (r["found"] or 0) > 0; a[2] += r["ok"] a[0] += 1; a[1] += (r["found"] or 0) > 0; a[2] += r["ok"]
print(f"{key:8s}", " ".join(f"{k}: ok {v[2]}/{v[0]}" for k, v in sorted(agg.items()))) print(f"{key:8s}", " ".join(f"{k}: ok {v[2]}/{v[0]}" for k, v in sorted(agg.items())))
ok = sum(r["ok"] for r in codes) ok = sum(r["ok"] for r in codes)
print(f"codes read {ok}/{len(codes)} ({100 * ok / len(codes):.0f} %), median {sorted(r['ms'] for r in codes)[len(codes) // 2]} ms/frame") ms = sorted(r['ms'] for r in res)
print(f"codes read {ok}/{len(codes)} ({100 * ok / len(codes):.0f} %), decode ms/frame median {ms[len(ms) // 2]} p90 {ms[int(len(ms) * .9)]}")
fails = [r for r in codes if not r["ok"]] fails = [r for r in codes if not r["ok"]]
print("failures (d, tilt, blur, contrast, found, score, errs):", print("failures (d, tilt, blur, contrast, found, score, errs):",
[(r["d"], r["tilt"], r["blur"], r["contrast"], r["found"], r["score"], r.get("errs")) for r in fails[:16]]) [(r["d"], r["tilt"], r["blur"], r["contrast"], r["found"], r["score"], r.get("errs")) for r in fails[:16]])