From ca1a73e0bba0da5d5d91ded604b55d80c62a386d Mon Sep 17 00:00:00 2001 From: Showdown76py Date: Sun, 16 Aug 2026 19:32:44 +0100 Subject: [PATCH] wip --- static/js/ringcode.js | 7 ++++--- static/js/vote.js | 22 ++++++++++++++-------- static/ringlab/far.js | 7 ++++--- static/ringlab/phone.js | 11 ++++++----- tools/ringbench.py | 3 ++- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/static/js/ringcode.js b/static/js/ringcode.js index 8c95163..8f16687 100644 --- a/static/js/ringcode.js +++ b/static/js/ringcode.js @@ -829,10 +829,11 @@ export function makeReader(opts = {}) { let acc = null, frames = 0, anchor = null, pending = null; const similar = (a, b) => { let s = 0, na = 0, nb = 0; for (let i = 0; i < a.length; i++) { s += a[i] * b[i]; na += a[i] * a[i]; nb += b[i] * b[i]; } return s / Math.sqrt(na * nb || 1); }; // slots only move forward, about 2 per second: a token far from the last one is noise or a replay - const gate = (token, now) => { + // a first token that needed the full rs correction waits for a second one to agree + const gate = (token, now, sure) => { const slot = +token.slice(1).split(".")[0]; const near = (ref) => { const d = ((slot - ref.slot + 98304) % 65536) - 32768; return d >= -8 && d <= 8 + (now - ref.at) / 100; }; - if (!anchor || near(anchor)) { anchor = { slot, at: now }; pending = null; return true; } + if (anchor ? near(anchor) : sure) { anchor = { slot, at: now }; pending = null; return true; } if (pending && near(pending)) { anchor = { slot, at: now }; pending = null; return true; } pending = { slot, at: now }; return false; @@ -850,7 +851,7 @@ export function makeReader(opts = {}) { if (t && t.fixes <= 2) { token = t.token; via = "sum"; } } } else if (token) { acc = Float32Array.from(d.soft, (v) => (v > 0 ? 1.5 : -1.5)); frames = 1; } - if (token && opts.gate !== false && !gate(token, now)) token = null; + if (token && opts.gate !== false && !gate(token, now, via === "sum" || d.fixes <= 2)) token = null; return { ...d, token, via }; }, }; diff --git a/static/js/vote.js b/static/js/vote.js index a49f5d5..a14d808 100644 --- a/static/js/vote.js +++ b/static/js/vote.js @@ -1,5 +1,5 @@ import { $, h, api, pollId, liveSocket, resultsView, toast, ring, icon } from "./common.js"; -import { decodeFrame, grayOf } from "./ringcode.js"; +import { makeRingScanner } from "./ringscan.js"; const pid = pollId(); const DONE_KEY = `rv_done_${pid}`; @@ -74,20 +74,22 @@ async function startScanner() { function makeScanner(video, onToken, onOther) { let stream = null, running = false; const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true }); + const rings = makeRingScanner(); async function loop() { if (!running) return; const t0 = performance.now(); if (video.readyState >= 2 && video.videoWidth) { - // 960 px is plenty and keeps low-end phones near 7 fps - 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); try { if (codeStyle === "ring") { - const d = decodeFrame(grayOf(ctx.getImageData(0, 0, canvas.width, canvas.height))); + // reads a crop at sensor resolution: a far code keeps all its pixels + const d = rings.scan(video, video.videoWidth, video.videoHeight); if (d.token) onToken(d.token); } else { + // 960 px is plenty for a qr and keeps low-end phones near 7 fps + 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); qrEngine ||= await window.QrScanner.createQrEngine(); const r = await window.QrScanner.scanImage(canvas, { qrEngine, returnDetailedScanResult: true }).catch(() => null); if (r) { const tok = tokenOf(r.data); if (tok) onToken(tok); else onOther?.(); } @@ -99,9 +101,13 @@ function makeScanner(video, onToken, onOther) { } return { async start() { + // rings from the back of a room are a few dozen pixels wide at 720p, ask for the full sensor + const [w, h] = codeStyle === "ring" ? [1920, 1080] : [1280, 720]; stream = await navigator.mediaDevices.getUserMedia({ - audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: 1280 }, height: { ideal: 720 } }, + audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: w }, height: { ideal: h } }, }); + // not every browser knows focusMode, those ignore it + stream.getVideoTracks()[0]?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {}); video.srcObject = stream; await video.play(); running = true; diff --git a/static/ringlab/far.js b/static/ringlab/far.js index 65ce397..c39c9ce 100644 --- a/static/ringlab/far.js +++ b/static/ringlab/far.js @@ -25,11 +25,12 @@ const pipes = { native: (canvas, t) => decodeFrame(grab(canvas, 1e9).img, t.opts || {}), noacc: (canvas, t, key) => { const s = (scanners[key] ||= window.makeRingScanner({ accumulate: false })); - return s.scan(canvas, canvas.width, canvas.height, t.now ?? null); + return s.scan(canvas, canvas.width, canvas.height, t.now ?? undefined); }, scan: (canvas, t, key) => { - const s = (scanners[key] ||= window.makeRingScanner(t.scanOpts || {})); - return s.scan(canvas, canvas.width, canvas.height, t.now ?? null); + // a lone still has no previous token to agree with + const s = (scanners[key] ||= window.makeRingScanner(t.now == null ? { gate: false } : {})); + return s.scan(canvas, canvas.width, canvas.height, t.now ?? undefined); }, }; diff --git a/static/ringlab/phone.js b/static/ringlab/phone.js index 481a25b..e9483de 100644 --- a/static/ringlab/phone.js +++ b/static/ringlab/phone.js @@ -1,4 +1,6 @@ -import { decodeFrame, grayOf } from "/static/js/ringcode.js"; +import { makeRingScanner } from "/static/js/ringscan.js"; + +const rings = makeRingScanner(); const $ = (id) => document.getElementById(id); const video = $("v"), canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true }); @@ -26,15 +28,14 @@ async function start() { async function loop() { if (!running) return; if (video.readyState >= 2) { - // 960 px is plenty for the locator and keeps capture cheap on low-end phones + // the qr reference reads a 960 px copy, the rings read their own crop of the full frame 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); frames++; fpsN++; - const img = grayOf(ctx.getImageData(0, 0, canvas.width, canvas.height)); - const d = decodeFrame(img); - if (d.found) qr++; + const d = rings.scan(video, video.videoWidth, video.videoHeight); + if (d.cand) qr++; if (d.token) { ring++; mode = "anneaux"; tokens.add(d.token); } else if (mode !== "anneaux") { // reference mode: the classic rotating qr diff --git a/tools/ringbench.py b/tools/ringbench.py index 828560d..7b9bf4a 100644 --- a/tools/ringbench.py +++ b/tools/ringbench.py @@ -72,7 +72,8 @@ def camera(r, d, contrast, blur, sensor): def far_trials(n): r = random.Random(23) out = [] - for d, contrast, blur, tilt, sensor in itertools.product([80, 100, 120, 150, 180], [0.45, 0.3], [0.7, 1.5], [0, 20], [1280, 1920]): + sizes = [int(x) for x in os.environ.get("FAR_D", "80,100,120,150,180").split(",")] + for d, contrast, blur, tilt, sensor in itertools.product(sizes, [0.45, 0.3], [0.7, 1.5], [0, 20], [1280, 1920]): for _ in range(n): t = camera(r, d, contrast, blur, sensor) m = r.uniform(0, 1.5), r.uniform(0, 6.283)