Print the subject hierarchy as a tree on cards, remove the QR code
Each card now shows, under the call number, the Library of Congress outline levels for its class (from the published outline, down to the narrowest range) followed by the place or topic from the catalogue record's subject heading, one level per line. Existing books get the subject part filled in by a background job at startup that changes nothing else about them. The QR code and everything that read it are removed; the subjects line on the card is replaced by the tree. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
c9b2c2e8c9
commit
7ae1bcef0a
46 files changed
+8591
-545
No files matched your search
+11
-23
@@ -1,12 +1,10 @@
|
||||
import { readBarcodes } from 'zxing-wasm/reader';
|
||||
import type { ReaderOptions } from 'zxing-wasm/reader';
|
||||
import { normalizeIsbn } from '../shared/isbn.ts';
|
||||
import { parseCardCode } from '../shared/cardcode.ts';
|
||||
|
||||
/** RGBA pixels, as from ImageData. Plain data so that this module runs in Node tests. */
|
||||
export interface Pixels { data: Uint8ClampedArray | Uint8Array; width: number; height: number }
|
||||
/** `card` is the text of a Booksorter card's QR code; QR codes that are not cards are dropped without a trace. */
|
||||
export interface DecodeOutcome { isbn: string | null; other: string | null; card: string | null }
|
||||
export interface DecodeOutcome { isbn: string | null; other: string | null }
|
||||
|
||||
// ---- image operations (grey, one byte per pixel) -------------------------------------------------------
|
||||
|
||||
@@ -88,12 +86,6 @@ export const BASE: ReaderOptions = {
|
||||
formats: ['EAN13', 'UPCA'], tryHarder: true, tryDownscale: true, tryRotate: false, tryInvert: false, maxNumberOfSymbols: 2,
|
||||
};
|
||||
|
||||
/**
|
||||
* QR codes are looked for only on the variants that keep 2D detail (plain and enlarged); the row-averaged ones
|
||||
* smear a QR code to nothing and stay EAN-only, so their speed and reliability are as before.
|
||||
*/
|
||||
const WITH_QR: ReaderOptions = { ...BASE, formats: ['EAN13', 'UPCA', 'QRCode'] };
|
||||
|
||||
export interface Variant { name: string; opts: ReaderOptions; prep: (g: Grey) => Grey }
|
||||
const rowSmooth = (g: Grey) => upscale(rowAverage(g, 6), 2);
|
||||
|
||||
@@ -105,19 +97,18 @@ const rowSmooth = (g: Grey) => upscale(rowAverage(g, 6), 2);
|
||||
export const VARIANTS: Variant[] = [
|
||||
{ name: 'rows+sharp2x', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => sharpenAcross(rowSmooth(g), 3, 1.2) },
|
||||
{ name: 'rows2x', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: rowSmooth },
|
||||
{ name: 'crop', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'up2x', opts: { ...WITH_QR, binarizer: 'GlobalHistogram' }, prep: g => upscale(g, 2) },
|
||||
{ name: 'up3x', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => upscale(g, 3) },
|
||||
{ name: 'crop', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'up2x', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: g => upscale(g, 2) },
|
||||
{ name: 'up3x', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => upscale(g, 3) },
|
||||
];
|
||||
/** For the whole frame, which is too big to enlarge: no upscaling. */
|
||||
export const WHOLE_VARIANTS: Variant[] = [
|
||||
{ name: 'whole', opts: { ...WITH_QR, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'whole', opts: { ...BASE, binarizer: 'LocalAverage' }, prep: g => g },
|
||||
{ name: 'whole-rows', opts: { ...BASE, binarizer: 'GlobalHistogram' }, prep: g => sharpenAcross(rowAverage(g, 6), 1, 1) },
|
||||
];
|
||||
|
||||
function judge(codes: { text: string; format?: string }[], into: DecodeOutcome): void {
|
||||
function judge(codes: { text: string }[], into: DecodeOutcome): void {
|
||||
for (const c of codes) {
|
||||
if (c.format === 'QRCode') { if (parseCardCode(c.text)) into.card ??= c.text; continue; }
|
||||
const isbn = /^\d{13}$/.test(c.text) ? normalizeIsbn(c.text) : null;
|
||||
if (isbn) into.isbn ??= isbn; else into.other ??= c.text;
|
||||
}
|
||||
@@ -127,9 +118,6 @@ async function tryVariant(g: Grey, v: Variant, extra: ReaderOptions, into: Decod
|
||||
judge(await readBarcodes(toImage(v.prep(g)) as ImageData, { ...v.opts, ...extra }), into);
|
||||
}
|
||||
|
||||
/** A book ISBN or a card was read: nothing more to try on this frame. */
|
||||
const found = (o: DecodeOutcome): boolean => o.isbn !== null || o.card !== null;
|
||||
|
||||
export interface LiveState { round: number; cursor: number }
|
||||
export const newLiveState = (): LiveState => ({ round: 0, cursor: 0 });
|
||||
/** Every FULL_EVERY-th live round looks at the whole frame instead of the crop. */
|
||||
@@ -146,14 +134,14 @@ export const wantsFull = (st: LiveState): boolean => st.round % FULL_EVERY === F
|
||||
* stopped, so that slow machines still get through every variant across frames.
|
||||
*/
|
||||
export async function decodeLive(p: Pixels, st: LiveState, now: () => number = () => performance.now()): Promise<{ out: DecodeOutcome; state: LiveState }> {
|
||||
const out: DecodeOutcome = { isbn: null, other: null, card: null };
|
||||
const out: DecodeOutcome = { isbn: null, other: null };
|
||||
const began = now(), g = toGrey(p);
|
||||
let cursor = st.cursor;
|
||||
if (wantsFull(st)) {
|
||||
for (const v of WHOLE_VARIANTS) if (!found(out)) await tryVariant(g, v, {}, out);
|
||||
for (const v of WHOLE_VARIANTS) if (!out.isbn) await tryVariant(g, v, {}, out);
|
||||
} else {
|
||||
await tryVariant(g, VARIANTS[0]!, {}, out);
|
||||
for (let i = 0; i < REST.length && !found(out) && now() - began <= LIVE_BUDGET_MS; i++) {
|
||||
for (let i = 0; i < REST.length && !out.isbn && now() - began <= LIVE_BUDGET_MS; i++) {
|
||||
await tryVariant(g, REST[cursor]!, {}, out);
|
||||
cursor = (cursor + 1) % REST.length;
|
||||
}
|
||||
@@ -163,12 +151,12 @@ export async function decodeLive(p: Pixels, st: LiveState, now: () => number = (
|
||||
|
||||
/** Everything we have on one frame, for the Capture burst: every variant, rotated too, crop then whole frame. */
|
||||
export async function decodeThorough(crop: Pixels, whole: Pixels | null): Promise<DecodeOutcome> {
|
||||
const out: DecodeOutcome = { isbn: null, other: null, card: null };
|
||||
const out: DecodeOutcome = { isbn: null, other: null };
|
||||
for (const p of whole ? [crop, whole] : [crop]) {
|
||||
const g = toGrey(p);
|
||||
for (const v of p === crop ? VARIANTS : WHOLE_VARIANTS) {
|
||||
await tryVariant(g, v, { tryRotate: true, tryInvert: true }, out);
|
||||
if (found(out)) return out;
|
||||
if (out.isbn) return out;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
|
||||
Reference in new issue
Block a user