Ring decoder: coarse-to-fine sync search (2x faster), lab phone at 960px, bench cpu throttling
This commit is contained in:
+22
-11
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user