Ring scanner: seed the crop read, try three candidates
This commit is contained in:
@@ -814,7 +814,13 @@ function readRings(S, opts) {
|
|||||||
|
|
||||||
// whole frame: find center discs, read the first one that yields a valid token
|
// whole frame: find center discs, read the first one that yields a valid token
|
||||||
export function decodeFrame(img, opts = {}) {
|
export function decodeFrame(img, opts = {}) {
|
||||||
const cands = locate(img, opts);
|
// seed: where the disc was a moment ago, or on a smaller copy of the frame. refit there before searching again
|
||||||
|
let cands = [];
|
||||||
|
if (opts.seed) {
|
||||||
|
const s = opts.seed, k = s.r / CENTER, fit = discFit(img, { cx: s.cx, cy: s.cy, A: s.A || [k, 0, 0, k], r: s.r }), rn = fit && ringness(img, fit);
|
||||||
|
if (rn) cands = [{ ...fit, ...rn }];
|
||||||
|
}
|
||||||
|
if (!cands.length) cands = locate(img, opts);
|
||||||
let last = null;
|
let last = null;
|
||||||
for (const c of cands) {
|
for (const c of cands) {
|
||||||
const r = decodeAt(img, c, opts);
|
const r = decodeAt(img, c, opts);
|
||||||
@@ -840,8 +846,8 @@ export function makeReader(opts = {}) {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
read(img, now = Date.now()) {
|
read(img, now = Date.now(), seed = null) {
|
||||||
const d = decodeFrame(img, opts);
|
const d = decodeFrame(img, { ...opts, seed });
|
||||||
let token = d.token, via = "frame";
|
let token = d.token, via = "frame";
|
||||||
if (!token && d.soft && opts.accumulate !== false) {
|
if (!token && d.soft && opts.accumulate !== false) {
|
||||||
if (acc && similar(acc, d.soft) > 0.3) { for (let i = 0; i < acc.length; i++) acc[i] += d.soft[i]; frames++; }
|
if (acc && similar(acc, d.soft) > 0.3) { for (let i = 0; i < acc.length; i++) acc[i] += d.soft[i]; frames++; }
|
||||||
|
|||||||
@@ -16,14 +16,16 @@ export function makeRingScanner(opts = {}) {
|
|||||||
const x0 = Math.max(0, Math.round(t.cx - half)), y0 = Math.max(0, Math.round(t.cy - half));
|
const x0 = Math.max(0, Math.round(t.cx - half)), y0 = Math.max(0, Math.round(t.cy - half));
|
||||||
const w = Math.min(sw, Math.round(t.cx + half)) - x0, h = Math.min(sh, Math.round(t.cy + half)) - y0;
|
const w = Math.min(sw, Math.round(t.cx + half)) - x0, h = Math.min(sh, Math.round(t.cy + half)) - y0;
|
||||||
if (w < 24 || h < 24) return null;
|
if (w < 24 || h < 24) return null;
|
||||||
const k = Math.min(1, CROP / Math.max(w, h));
|
const k = Math.min(1, (opts.crop || CROP) / Math.max(w, h));
|
||||||
crop.width = Math.round(w * k); crop.height = Math.round(h * k);
|
crop.width = Math.round(w * k); crop.height = Math.round(h * k);
|
||||||
|
cctx.imageSmoothingQuality = "high";
|
||||||
cctx.drawImage(src, x0, y0, w, h, 0, 0, crop.width, crop.height);
|
cctx.drawImage(src, x0, y0, w, h, 0, 0, crop.width, crop.height);
|
||||||
const d = reader.read(grayOf(cctx.getImageData(0, 0, crop.width, crop.height)), now);
|
const seed = { cx: (t.cx - x0) * k - 0.5, cy: (t.cy - y0) * k - 0.5, r: t.unit * CENTER * k, A: t.A && t.A.map((v) => v * k) };
|
||||||
|
const d = reader.read(grayOf(cctx.getImageData(0, 0, crop.width, crop.height)), now, seed);
|
||||||
if (!d.cand) return null;
|
if (!d.cand) return null;
|
||||||
// only a blob with a sync ring keeps the crop alive, anything else falls back to the whole frame
|
// only a blob with a sync ring keeps the crop alive, anything else goes back to the whole frame
|
||||||
const at = d.token || d.soft ? now : t.at ?? now - 500;
|
if (!d.token && !d.soft) { track = null; return d; }
|
||||||
track = { cx: x0 + (d.cand.cx + 0.5) / k, cy: y0 + (d.cand.cy + 0.5) / k, unit: d.cand.r / CENTER / k, at };
|
track = { cx: x0 + (d.cand.cx + 0.5) / k, cy: y0 + (d.cand.cy + 0.5) / k, unit: d.cand.r / CENTER / k, A: d.cand.A.map((v) => v / k), at: now };
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,10 +42,10 @@ export function makeRingScanner(opts = {}) {
|
|||||||
// plain bilinear at 3x reduction skips pixels and keeps the noise
|
// plain bilinear at 3x reduction skips pixels and keeps the noise
|
||||||
wctx.imageSmoothingQuality = "high";
|
wctx.imageSmoothingQuality = "high";
|
||||||
wctx.drawImage(src, 0, 0, wide.width, wide.height);
|
wctx.drawImage(src, 0, 0, wide.width, wide.height);
|
||||||
const cands = locate(grayOf(wctx.getImageData(0, 0, wide.width, wide.height)), { max: 2 });
|
const cands = locate(grayOf(wctx.getImageData(0, 0, wide.width, wide.height)), { max: 3 });
|
||||||
let last = null;
|
let last = null;
|
||||||
for (const c of cands) {
|
for (const c of cands) {
|
||||||
const d = readAt(src, sw, sh, { cx: (c.cx + 0.5) / k, cy: (c.cy + 0.5) / k, unit: c.r / CENTER / k }, now);
|
const d = readAt(src, sw, sh, { cx: (c.cx + 0.5) / k, cy: (c.cy + 0.5) / k, unit: c.r / CENTER / k, A: c.A.map((v) => v / k) }, now);
|
||||||
if (d && d.token) return d;
|
if (d && d.token) return d;
|
||||||
last = d || last;
|
last = d || last;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ const pipes = {
|
|||||||
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 ?? undefined);
|
return s.scan(canvas, canvas.width, canvas.height, t.now ?? undefined);
|
||||||
},
|
},
|
||||||
|
crop: (canvas, t, key) => {
|
||||||
|
const s = (scanners[key] ||= window.makeRingScanner({ gate: t.now != null, crop: +key.slice(4) }));
|
||||||
|
return s.scan(canvas, canvas.width, canvas.height, t.now ?? undefined);
|
||||||
|
},
|
||||||
scan: (canvas, t, key) => {
|
scan: (canvas, t, key) => {
|
||||||
// a lone still has no previous token to agree with
|
// a lone still has no previous token to agree with
|
||||||
const s = (scanners[key] ||= window.makeRingScanner(t.now == null ? { gate: false } : {}));
|
const s = (scanners[key] ||= window.makeRingScanner(t.now == null ? { gate: false } : {}));
|
||||||
@@ -135,7 +139,7 @@ window.runFar = async (cfg) => {
|
|||||||
for (const name of cfg.pipes) {
|
for (const name of cfg.pipes) {
|
||||||
for (const key of Object.keys(scanners)) delete scanners[key];
|
for (const key of Object.keys(scanners)) delete scanners[key];
|
||||||
const t0 = performance.now();
|
const t0 = performance.now();
|
||||||
const d = pipes[name](f.canvas, t, name);
|
const d = (pipes[name] || pipes[name.slice(0, 4)])(f.canvas, t, name);
|
||||||
const ms = performance.now() - t0;
|
const ms = performance.now() - t0;
|
||||||
res[name] = { ok: t.nocode ? !d.token : d.token === t.token, token: d.token || null, ms: +ms.toFixed(1), found: d.found, score: d.score == null ? null : +(+d.score).toFixed(2) };
|
res[name] = { ok: t.nocode ? !d.token : d.token === t.token, token: d.token || null, ms: +ms.toFixed(1), found: d.found, score: d.score == null ? null : +(+d.score).toFixed(2) };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user