a
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* 测试 paper_recognizer WASM 模块
|
||||
* 用法: node test.js
|
||||
*/
|
||||
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
async function test() {
|
||||
console.log('=== 测试 PaperRecognizer WASM 模块 ===\n');
|
||||
|
||||
// 加载模块
|
||||
const Module = require('./web/paper_recognizer.js');
|
||||
const m = await Module();
|
||||
|
||||
console.log('✅ 模块加载成功');
|
||||
console.log(` 导出函数:`);
|
||||
|
||||
const exports = [
|
||||
'_recognize', '_getResultJson', '_getAnnotatedWidth',
|
||||
'_getAnnotatedHeight', '_getAnnotatedPixels', '_freeResult',
|
||||
'_malloc', '_free'
|
||||
];
|
||||
|
||||
for (const name of exports) {
|
||||
const fn = m[name];
|
||||
if (typeof fn === 'function') {
|
||||
console.log(` ✅ ${name}`);
|
||||
} else {
|
||||
console.log(` ❌ ${name} 未导出`);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建一个测试图像(纯灰色图像)
|
||||
const W = 640, H = 480;
|
||||
const pixels = new Uint8Array(W * H * 4);
|
||||
for (let y = 0; y < H; y++) {
|
||||
for (let x = 0; x < W; x++) {
|
||||
const idx = (y * W + x) * 4;
|
||||
pixels[idx + 0] = 128; // R
|
||||
pixels[idx + 1] = 128; // G
|
||||
pixels[idx + 2] = 128; // B
|
||||
pixels[idx + 3] = 255; // A
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n=== 测试识别功能 ===');
|
||||
|
||||
// 分配 WASM 内存并复制图像数据
|
||||
const ptr = m._malloc(pixels.length);
|
||||
m.HEAPU8.set(pixels, ptr);
|
||||
|
||||
// 调用识别
|
||||
const ret = m._recognize(ptr, W, H);
|
||||
console.log(` 识别返回值: ${ret}`);
|
||||
|
||||
// 获取结果 JSON
|
||||
const resultPtr = m._getResultJson();
|
||||
const resultStr = m.UTF8ToString(resultPtr);
|
||||
console.log(` 结果 JSON:\n${JSON.stringify(JSON.parse(resultStr), null, 2)}`);
|
||||
|
||||
// 获取标注图尺寸
|
||||
const aw = m._getAnnotatedWidth();
|
||||
const ah = m._getAnnotatedHeight();
|
||||
console.log(` 标注图尺寸: ${aw} x ${ah}`);
|
||||
|
||||
// 获取标注图像素
|
||||
const ap = m._getAnnotatedPixels();
|
||||
if (ap && aw > 0 && ah > 0) {
|
||||
const size = aw * ah * 4;
|
||||
const annotatedData = new Uint8Array(m.HEAPU8.buffer, ap, size);
|
||||
console.log(` 标注图像素数据大小: ${annotatedData.length} bytes`);
|
||||
|
||||
// 写入文件供查看
|
||||
// 保存为原始 RGBA 数据
|
||||
fs.writeFileSync('/tmp/test_result.rgba', annotatedData);
|
||||
console.log(` 已保存到 /tmp/test_result.rgba (${aw}x${ah} RGBA)`);
|
||||
}
|
||||
|
||||
// 清理
|
||||
m._freeResult();
|
||||
m._free(ptr);
|
||||
|
||||
console.log('\n✅ 测试完成');
|
||||
}
|
||||
|
||||
test().catch(err => {
|
||||
console.error('❌ 测试失败:', err.message);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user