This commit is contained in:
2026-08-16 19:32:44 +01:00
parent 05cfa36e5b
commit ca1a73e0bb
5 changed files with 30 additions and 20 deletions
+4 -3
View File
@@ -829,10 +829,11 @@ export function makeReader(opts = {}) {
let acc = null, frames = 0, anchor = null, pending = null; 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); }; 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 // 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 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; }; 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; } if (pending && near(pending)) { anchor = { slot, at: now }; pending = null; return true; }
pending = { slot, at: now }; pending = { slot, at: now };
return false; return false;
@@ -850,7 +851,7 @@ export function makeReader(opts = {}) {
if (t && t.fixes <= 2) { token = t.token; via = "sum"; } 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; } } 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 }; return { ...d, token, via };
}, },
}; };
+14 -8
View File
@@ -1,5 +1,5 @@
import { $, h, api, pollId, liveSocket, resultsView, toast, ring, icon } from "./common.js"; 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 pid = pollId();
const DONE_KEY = `rv_done_${pid}`; const DONE_KEY = `rv_done_${pid}`;
@@ -74,20 +74,22 @@ async function startScanner() {
function makeScanner(video, onToken, onOther) { function makeScanner(video, onToken, onOther) {
let stream = null, running = false; let stream = null, running = false;
const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true }); const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true });
const rings = makeRingScanner();
async function loop() { async function loop() {
if (!running) return; if (!running) return;
const t0 = performance.now(); const t0 = performance.now();
if (video.readyState >= 2 && video.videoWidth) { 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 { try {
if (codeStyle === "ring") { 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); if (d.token) onToken(d.token);
} else { } 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(); qrEngine ||= await window.QrScanner.createQrEngine();
const r = await window.QrScanner.scanImage(canvas, { qrEngine, returnDetailedScanResult: true }).catch(() => null); 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?.(); } if (r) { const tok = tokenOf(r.data); if (tok) onToken(tok); else onOther?.(); }
@@ -99,9 +101,13 @@ function makeScanner(video, onToken, onOther) {
} }
return { return {
async start() { 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({ 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; video.srcObject = stream;
await video.play(); await video.play();
running = true; running = true;
+4 -3
View File
@@ -25,11 +25,12 @@ const pipes = {
native: (canvas, t) => decodeFrame(grab(canvas, 1e9).img, t.opts || {}), native: (canvas, t) => decodeFrame(grab(canvas, 1e9).img, t.opts || {}),
noacc: (canvas, t, key) => { noacc: (canvas, t, key) => {
const s = (scanners[key] ||= window.makeRingScanner({ accumulate: false })); 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) => { scan: (canvas, t, key) => {
const s = (scanners[key] ||= window.makeRingScanner(t.scanOpts || {})); // a lone still has no previous token to agree with
return s.scan(canvas, canvas.width, canvas.height, t.now ?? null); const s = (scanners[key] ||= window.makeRingScanner(t.now == null ? { gate: false } : {}));
return s.scan(canvas, canvas.width, canvas.height, t.now ?? undefined);
}, },
}; };
+6 -5
View File
@@ -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 $ = (id) => document.getElementById(id);
const video = $("v"), canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true }); const video = $("v"), canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true });
@@ -26,15 +28,14 @@ async function start() {
async function loop() { async function loop() {
if (!running) return; if (!running) return;
if (video.readyState >= 2) { 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); 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);
frames++; fpsN++; frames++; fpsN++;
const img = grayOf(ctx.getImageData(0, 0, canvas.width, canvas.height)); const d = rings.scan(video, video.videoWidth, video.videoHeight);
const d = decodeFrame(img); if (d.cand) qr++;
if (d.found) qr++;
if (d.token) { ring++; mode = "anneaux"; tokens.add(d.token); } if (d.token) { ring++; mode = "anneaux"; tokens.add(d.token); }
else if (mode !== "anneaux") { else if (mode !== "anneaux") {
// reference mode: the classic rotating qr // reference mode: the classic rotating qr
+2 -1
View File
@@ -72,7 +72,8 @@ def camera(r, d, contrast, blur, sensor):
def far_trials(n): def far_trials(n):
r = random.Random(23) r = random.Random(23)
out = [] 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): for _ in range(n):
t = camera(r, d, contrast, blur, sensor) t = camera(r, d, contrast, blur, sensor)
m = r.uniform(0, 1.5), r.uniform(0, 6.283) m = r.uniform(0, 1.5), r.uniform(0, 6.283)