|
| 1 | +import { readdirSync, statSync } from 'node:fs'; |
| 2 | +import { dirname, join as pjoin } from 'node:path'; |
| 3 | +import { performance } from 'node:perf_hooks'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import decodeQR from '../src/decode.ts'; |
| 6 | +// Reuse the curated expectations from decode tests; should.runWhen keeps tests idle on import. |
| 7 | +import { DECODED, DECODE_VECTOR_EXCLUDE } from '../test/decode.test.ts'; |
| 8 | +import { isDecodeImage, readImage } from '../test/utils.ts'; |
| 9 | + |
| 10 | +const _dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | +const DETECTION_PATH = pjoin(_dirname, '..', 'test', 'vectors', 'boofcv-v3', 'detection'); |
| 12 | + |
| 13 | +const listFiles = (path, isDir = false) => |
| 14 | + readdirSync(path) |
| 15 | + .filter((i) => statSync(`${path}/${i}`).isDirectory() === isDir) |
| 16 | + .sort(); |
| 17 | + |
| 18 | +const percent = (value, total) => (total === 0 ? 'n/a' : `${((100 * value) / total).toFixed(1)}%`); |
| 19 | +const millis = (value) => `${Math.round(value)}ms`; |
| 20 | + |
| 21 | +const select = (envName, values) => { |
| 22 | + const raw = process.env[envName]; |
| 23 | + if (!raw) return values; |
| 24 | + const selected = raw |
| 25 | + .split(',') |
| 26 | + .map((s) => s.trim()) |
| 27 | + .filter(Boolean); |
| 28 | + for (const value of selected) { |
| 29 | + if (!values.includes(value)) throw new Error(`unknown ${envName} value=${value}`); |
| 30 | + } |
| 31 | + return selected; |
| 32 | +}; |
| 33 | + |
| 34 | +function vectorFiles(category) { |
| 35 | + const dir = pjoin(DETECTION_PATH, category); |
| 36 | + return listFiles(dir) |
| 37 | + .filter(isDecodeImage) |
| 38 | + .filter((file) => !DECODE_VECTOR_EXCLUDE.includes(`${category}/${file}`)) |
| 39 | + .map((file) => ({ |
| 40 | + category, |
| 41 | + file, |
| 42 | + path: pjoin('detection', category, file), |
| 43 | + expected: DECODED[category]?.[file], |
| 44 | + })); |
| 45 | +} |
| 46 | + |
| 47 | +function runCategory(files) { |
| 48 | + const stats = { |
| 49 | + files: 0, |
| 50 | + expected: 0, |
| 51 | + matched: 0, |
| 52 | + wrong: 0, |
| 53 | + missed: 0, |
| 54 | + unknownDecoded: 0, |
| 55 | + errors: 0, |
| 56 | + ms: 0, |
| 57 | + }; |
| 58 | + const started = performance.now(); |
| 59 | + for (const vector of files) { |
| 60 | + stats.files++; |
| 61 | + if (vector.expected) stats.expected++; |
| 62 | + let decoded; |
| 63 | + try { |
| 64 | + decoded = decodeQR(readImage(vector.path), { moreEffort: true }); |
| 65 | + } catch { |
| 66 | + stats.errors++; |
| 67 | + } |
| 68 | + if (vector.expected) { |
| 69 | + if (vector.expected.includes(decoded)) stats.matched++; |
| 70 | + else if (decoded === undefined) stats.missed++; |
| 71 | + else stats.wrong++; |
| 72 | + } else if (decoded !== undefined) { |
| 73 | + stats.unknownDecoded++; |
| 74 | + } |
| 75 | + } |
| 76 | + stats.ms = performance.now() - started; |
| 77 | + return stats; |
| 78 | +} |
| 79 | + |
| 80 | +function printRow(category, stats) { |
| 81 | + const msPerImage = stats.files === 0 ? 0 : stats.ms / stats.files; |
| 82 | + const quality = stats.matched + stats.unknownDecoded; |
| 83 | + console.log(`${category},${percent(quality, stats.files)},${millis(msPerImage)}`); |
| 84 | +} |
| 85 | + |
| 86 | +function main() { |
| 87 | + const categories = select('QR_QUALITY_CATEGORIES', listFiles(DETECTION_PATH, true)); |
| 88 | + for (const category of categories) { |
| 89 | + const files = vectorFiles(category); |
| 90 | + if (files.length === 0) continue; |
| 91 | + printRow(category, runCategory(files)); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +main(); |
0 commit comments