test-ocr-config.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * OnnxOCR配置参数测试示例
  3. */
  4. import { startOcrDialogBlockReg, createOcrConfig, getPresetConfig } from '../ocr-dialog-block-reg.js';
  5. import path from 'path';
  6. async function testOcrConfigurations() {
  7. const imagePath = "static/漫画/image/鬼-巷第001卷/第一章/test/tmp/0004_鬼-巷第001卷_text_mask.png";
  8. const outputDir = "static/漫画/image/鬼-巷第001卷/第一章/test/tmp";
  9. console.log('🧪 OnnxOCR配置参数测试');
  10. console.log('=' * 60);
  11. // 测试1: 使用预设配置
  12. console.log('\n📋 测试1: 预设配置');
  13. const configs = [
  14. { name: '平衡配置', config: getPresetConfig('balanced') },
  15. { name: '高精度配置', config: getPresetConfig('high_precision') },
  16. { name: '快速配置', config: getPresetConfig('fast') }
  17. ];
  18. for (const { name, config } of configs) {
  19. console.log(`\n🔧 ${name}:`);
  20. console.log(JSON.stringify(config, null, 2));
  21. const textBlocksJsonPath = path.join(outputDir, `test_${name.replace(/\s+/g, '_')}_blocks.json`);
  22. const textRegionImagePath = path.join(outputDir, `test_${name.replace(/\s+/g, '_')}_region.png`);
  23. try {
  24. const result = await startOcrDialogBlockReg(imagePath, textBlocksJsonPath, textRegionImagePath, config);
  25. console.log(`✅ ${name} 完成: 识别到 ${result.totalCount} 个文字区域`);
  26. } catch (error) {
  27. console.log(`❌ ${name} 失败: ${error.message}`);
  28. }
  29. }
  30. // 测试2: 自定义配置
  31. console.log('\n📋 测试2: 自定义配置');
  32. const customConfigs = [
  33. {
  34. name: '超高精度配置',
  35. config: createOcrConfig({
  36. det_db_thresh: 0.1, // 极低检测阈值
  37. det_db_box_thresh: 0.4, // 极低框阈值
  38. det_limit_side_len: 1536, // 最大处理尺寸
  39. drop_score: 0.2, // 极低置信度阈值
  40. use_angle_cls: true,
  41. det_db_unclip_ratio: 1.8 // 更大的框扩展
  42. })
  43. },
  44. {
  45. name: '极速配置',
  46. config: createOcrConfig({
  47. det_db_thresh: 0.5, // 高检测阈值
  48. det_db_box_thresh: 0.8, // 高框阈值
  49. det_limit_side_len: 480, // 小处理尺寸
  50. drop_score: 0.7, // 高置信度阈值
  51. use_angle_cls: false, # 禁用角度分类器
  52. det_db_unclip_ratio: 1.2 // 小框扩展
  53. })
  54. }
  55. ];
  56. for (const { name, config } of customConfigs) {
  57. console.log(`\n🔧 ${name}:`);
  58. console.log(JSON.stringify(config, null, 2));
  59. const textBlocksJsonPath = path.join(outputDir, `test_${name.replace(/\s+/g, '_')}_blocks.json`);
  60. const textRegionImagePath = path.join(outputDir, `test_${name.replace(/\s+/g, '_')}_region.png`);
  61. try {
  62. const result = await startOcrDialogBlockReg(imagePath, textBlocksJsonPath, textRegionImagePath, config);
  63. console.log(`✅ ${name} 完成: 识别到 ${result.totalCount} 个文字区域`);
  64. } catch (error) {
  65. console.log(`❌ ${name} 失败: ${error.message}`);
  66. }
  67. }
  68. console.log('\n🎉 所有配置测试完成!');
  69. }
  70. // 运行测试
  71. testOcrConfigurations().catch(console.error);