Files
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

111 lines
3.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* test.js — PaperRecognizer 纯 JS 版验证测试
*
* 生成符合规范的模拟试纸图像并执行识别,
* 验证模块能正常加载且所有 API 正确运行。
*
* 用法: node test.js
*/
const { PaperRecognizer, PST_NAMES, Img, CONFIGS, PST } = require('./paper-recognizer.js');
function makeTestImage() {
const W = 800, H = 300;
const px = new Uint8Array(W * H * 4);
// Fill background
for (let y = 0; y < H; y++) {
const val = 245 - Math.floor(y / H * 10);
for (let x = 0; x < W; x++) {
const i = (y * W + x) * 4;
px[i] = val; px[i+1] = val; px[i+2] = val; px[i+3] = 255;
}
}
function rect(x1, y1, x2, y2, r, g, b) {
for (let y = y1; y <= y2; y++)
for (let x = x1; x <= x2; x++) {
const i = (y * W + x) * 4;
px[i] = r; px[i+1] = g; px[i+2] = b; px[i+3] = 255;
}
}
// Big locator (left): 50x25 blue (aspect 2:1)
rect(80, 138, 129, 162, 0, 50, 200);
// Small locator (right): 25x15 blue (aspect 1.67:1)
rect(550, 143, 574, 157, 0, 50, 200);
// Color blocks (warm colors)
const colors = [
[200,130,90],[215,160,110],[185,120,80],[225,170,120],
[195,140,100],[210,150,105],[180,125,95],[220,165,115],
[190,135,100],[205,145,85],[230,175,125],[195,130,90],
[215,155,130],[200,145,115]
];
const bigR = 129; // Big right edge
const smallL = 550; // Small left edge
const dist = smallL - bigR;
const step = dist / 50;
const firstOffset = step * 2.25;
const stepOffset = step * 3.5;
for (let i = 0; i < 14 && i < colors.length; i++) {
const x1 = bigR + firstOffset + stepOffset * i;
if (x1 + 18 >= smallL) break;
for (let y = 138; y <= 162; y++)
for (let x = Math.round(x1); x < Math.round(x1) + 18; x++)
if (x < W && y < H) {
const idx = (y * W + x) * 4;
px[idx] = colors[i][0]; px[idx+1] = colors[i][1]; px[idx+2] = colors[i][2];
}
}
return { pixels: px, width: W, height: H };
}
console.log('=== PaperRecognizer 纯 JS 版测试 ===');
console.log('');
// Test 1: Module loads
console.log('✓ Module loaded');
console.log(` PaperRecognizer: ${typeof PaperRecognizer}`);
console.log(` Img functions: ${Object.keys(Img).length}`);
console.log(` PST types: ${Object.keys(PST).length}`);
// Test 2: Basic Img operations
const gray = Img.toGray(new Uint8Array(16).fill(128), 2, 2);
console.log(`\n✓ Img.toGray: ${gray.length} values, mean=${(gray.reduce((a,b)=>a+b,0)/4).toFixed(0)}`);
const { mean, stddev } = Img.meanStdDev(new Float64Array([1,2,3,4,5]), 5);
console.log(`✓ Img.meanStdDev: mean=${mean.toFixed(1)}, stddev=${stddev.toFixed(3)}`);
// Test 3: generate test image and run recognition
const { pixels, width, height } = makeTestImage();
console.log(`\n✓ Test image: ${width}×${height}`);
const recognizer = new PaperRecognizer();
const result = recognizer.recognize(pixels, width, height);
console.log(`\n=== Recognition Result ===`);
console.log(` Success: ${result.success}`);
console.log(` Paper Type: ${PST_NAMES[result.paperType] || 'UNKNOWN'}`);
console.log(` Item Count: ${result.itemCount}`);
console.log(` Locator Color: ${['Unknown','Red','Green','Blue'][result.locatorColor]}`);
console.log(` Small on Left: ${result.smallLocatorOnLeft}`);
console.log(` Blur Level: ${['None','Slight','Severe'][result.blurLevel]} (score=${(result.blurScore||0).toFixed(0)})`);
const jsonStr = recognizer.getResultJson();
console.log(`\n✓ getResultJson(): ${jsonStr.length} chars, valid JSON: ${(()=>{try{JSON.parse(jsonStr);return true}catch(e){return false}})()}`);
const aw = recognizer.getAnnotatedWidth();
const ah = recognizer.getAnnotatedHeight();
const ap = recognizer.getAnnotatedPixels();
console.log(`✓ Annotated: ${aw}×${ah}, pixels=${ap ? ap.length : 0}`);
recognizer.free();
console.log(`\n✓ All tests passed`);
const success = result.success;
process.exit(success ? 0 : 1);