Files
rotatyvote/static/js/ringscan.js
T

68 lines
3.6 KiB
JavaScript

// camera side of the ring code: finds the code on a small copy of the frame, then reads it from a crop
// at full sensor resolution. far codes keep every pixel the camera has, and the cost per frame stays flat
import { makeReader, locate, grayOf, CENTER, DISC } from "./ringcode.js";
const WIDE = 960, CROP = 360;
export function makeRingScanner(opts = {}) {
const reader = makeReader(opts);
const wide = document.createElement("canvas"), wctx = wide.getContext("2d", { willReadFrequently: true });
const crop = document.createElement("canvas"), cctx = crop.getContext("2d", { willReadFrequently: true });
let track = null;
// source px of the square around a code of the given unit
function readAt(src, sw, sh, t, now) {
const half = t.unit * (DISC + 0.9);
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;
if (w < 24 || h < 24) return null;
const k = Math.min(1, (opts.crop || CROP) / Math.max(w, h));
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);
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;
// where the code sits in the frame and how big, as fractions: lets the camera zoom in on a small one
const ux = x0 + (d.cand.cx + 0.5) / k, uy = y0 + (d.cand.cy + 0.5) / k, unit = d.cand.r / CENTER / k;
d.where = { x: ux / sw, y: uy / sh, d: (2 * DISC * unit) / Math.min(sw, sh) };
// only a blob with a sync ring keeps the crop alive, anything else goes back to the whole frame
if (!d.token && !d.soft) { track = null; return d; }
track = { cx: ux, cy: uy, unit, A: d.cand.A.map((v) => v / k), at: now };
return d;
}
return {
// src: video or canvas of sw x sh. returns { token, ... } with token null when nothing was read
scan(src, sw, sh, now = performance.now()) {
if (track && now - track.at < 700) {
const d = readAt(src, sw, sh, track, now);
if (d) return d;
}
track = null;
const r = search(src, sw, sh, now, 0, 0, sw, sh);
if (r.token || r.found) return r;
// nothing in the whole view: the back rows see a ring too small to find on the reduced copy. look at the
// middle, where people aim, at twice the detail. same as zooming in, without moving the preview
return search(src, sw, sh, now, sw / 4, sh / 4, sw / 2, sh / 2);
},
};
// finds discs in the region x0 y0 w h of the frame (on a copy of at most WIDE px), reads them at full resolution
function search(src, sw, sh, now, x0, y0, w, h) {
const k = Math.min(1, WIDE / Math.max(w, h));
wide.width = Math.round(w * k); wide.height = Math.round(h * k);
// plain bilinear at 3x reduction skips pixels and keeps the noise
wctx.imageSmoothingQuality = "high";
wctx.drawImage(src, x0, y0, w, h, 0, 0, wide.width, wide.height);
const cands = locate(grayOf(wctx.getImageData(0, 0, wide.width, wide.height)), { max: 5 });
let last = null;
for (const c of cands) {
const d = readAt(src, sw, sh, { cx: x0 + (c.cx + 0.5) / k, cy: y0 + (c.cy + 0.5) / k, unit: c.r / CENTER / k, A: c.A.map((v) => v / k) }, now);
if (d && d.token) return d;
last = d || last;
}
return last || { token: null, found: cands.length };
}
}