diff --git a/docs/screenshots/10-code-circulaire.jpg b/docs/screenshots/10-code-circulaire.jpg new file mode 100644 index 0000000..b38396e Binary files /dev/null and b/docs/screenshots/10-code-circulaire.jpg differ diff --git a/static/js/ringcode.js b/static/js/ringcode.js index 6f0ed3d..21055e2 100644 --- a/static/js/ringcode.js +++ b/static/js/ringcode.js @@ -593,18 +593,29 @@ export function decodeAt(img, cand, 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; - for (const scale of opts.scales || [0.95, 0.975, 1.0, 1.025, 1.05]) { - const r0 = RADII[0] * scale, n0 = RING_SECTORS[0], step0 = (2 * Math.PI) / n0; - // full turn: the sync pattern has no rotational symmetry - for (let t = 0; t < steps; t++) { - 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 }; + for (const c of coarse.slice(0, 3)) { + for (const ds of [-0.02, 0, 0.02]) for (let dt = -10; dt <= 10; dt++) { + const r = syncAt(c.theta + (dt * 0.5 * Math.PI) / 180, c.scale + ds); + if (r && (!best || r.score > best.score)) best = r; } } // weak sync means we're not looking at a ring code (or a bad frame): don't let rs "fix" noise diff --git a/static/ringlab/phone.js b/static/ringlab/phone.js index 101a048..481a25b 100644 --- a/static/ringlab/phone.js +++ b/static/ringlab/phone.js @@ -26,7 +26,8 @@ async function start() { async function loop() { if (!running) return; 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.height = Math.round(video.videoHeight * k); ctx.drawImage(video, 0, 0, canvas.width, canvas.height); diff --git a/tools/ringbench.py b/tools/ringbench.py index d0dda1b..a624755 100644 --- a/tools/ringbench.py +++ b/tools/ringbench.py @@ -60,6 +60,10 @@ async def main(): b = Browser(9336) await b.start() 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") for _ in range(50): 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"] 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) - 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"]] 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]])