ocr-config-examples.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * OnnxOCR配置参数使用示例
  3. *
  4. * 这个文件展示了如何在 ocr-dialog-block-reg.js 中配置 OnnxOCR 的所有参数
  5. */
  6. import { startOcrDialogBlockReg, createOcrConfig, getPresetConfig } from './ocr-dialog-block-reg.js';
  7. // ============================================================================
  8. // 使用方式1: 使用预设配置
  9. // ============================================================================
  10. async function usePresetConfig() {
  11. const imagePath = "your_image.png";
  12. const textBlocksJsonPath = "output/text_blocks.json";
  13. const textRegionImagePath = "output/text_region.png";
  14. // 获取高精度预设配置
  15. const config = getPresetConfig('high_precision');
  16. const result = await startOcrDialogBlockReg(
  17. imagePath,
  18. textBlocksJsonPath,
  19. textRegionImagePath,
  20. config
  21. );
  22. }
  23. // ============================================================================
  24. // 使用方式2: 创建自定义配置
  25. // ============================================================================
  26. async function useCustomConfig() {
  27. const imagePath = "your_image.png";
  28. const textBlocksJsonPath = "output/text_blocks.json";
  29. const textRegionImagePath = "output/text_region.png";
  30. // 创建自定义配置
  31. const config = createOcrConfig({
  32. // 检测参数(影响能检测到多少文字区域)
  33. det_db_thresh: 0.15, // 检测阈值:0.1-0.5,越小检测越敏感
  34. det_db_box_thresh: 0.45, // 框置信度:0.3-0.8,越小检测更多框
  35. det_limit_side_len: 1536, # 处理尺寸:640/960/1280/1536,越大精度越高
  36. // 识别参数(影响文字识别准确度)
  37. drop_score: 0.25, // 识别阈值:0.2-0.6,越小保留更多结果
  38. use_angle_cls: true, // 角度分类器:true提高倾斜文字识别
  39. // 高级参数
  40. det_db_unclip_ratio: 1.6, // 框扩展比例:1.2-2.0
  41. det_box_type: "quad", // 检测框类型:quad/poly
  42. use_dilation: false, // 膨胀操作:true可能提高小文字检测
  43. det_db_score_mode: "fast" // 计算模式:fast/slow
  44. });
  45. const result = await startOcrDialogBlockReg(
  46. imagePath,
  47. textBlocksJsonPath,
  48. textRegionImagePath,
  49. config
  50. );
  51. }
  52. // ============================================================================
  53. // 使用方式3: 直接传入配置对象
  54. // ============================================================================
  55. async function useDirectConfig() {
  56. const imagePath = "your_image.png";
  57. const textBlocksJsonPath = "output/text_blocks.json";
  58. const textRegionImagePath = "output/text_region.png";
  59. // 直接传入配置对象
  60. const result = await startOcrDialogBlockReg(
  61. imagePath,
  62. textBlocksJsonPath,
  63. textRegionImagePath,
  64. {
  65. det_db_thresh: 0.2,
  66. det_db_box_thresh: 0.5,
  67. det_limit_side_len: 1280,
  68. drop_score: 0.3,
  69. use_angle_cls: true
  70. }
  71. );
  72. }
  73. // ============================================================================
  74. // 预设配置说明
  75. // ============================================================================
  76. /*
  77. 可用的预设配置:
  78. 1. 'balanced' (平衡配置) - 默认
  79. - 速度和精度平衡
  80. - 适合大多数场景
  81. 2. 'high_precision' (高精度配置)
  82. - 识别更准确,检测更多区域
  83. - 速度较慢,适合要求高精度的场景
  84. 3. 'fast' (快速配置)
  85. - 速度优先,精度稍低
  86. - 适合实时处理或大批量处理
  87. */
  88. // ============================================================================
  89. // 参数详细说明
  90. // ============================================================================
  91. /*
  92. 主要参数说明:
  93. 📍 检测相关参数:
  94. - det_db_thresh: 文字检测阈值 (0.1-0.5)
  95. * 越小 = 检测越敏感,能找到更多文字区域
  96. * 越大 = 检测越严格,只保留明显的文字区域
  97. - det_db_box_thresh: 文字框置信度阈值 (0.3-0.8)
  98. * 越小 = 保留更多候选框
  99. * 越大 = 只保留高置信度的框
  100. - det_limit_side_len: 图片处理尺寸 (640/960/1280/1536)
  101. * 越大 = 精度越高,但速度越慢
  102. * 越小 = 速度越快,但可能漏检小文字
  103. 📝 识别相关参数:
  104. - drop_score: 识别置信度阈值 (0.2-0.6)
  105. * 越小 = 保留更多识别结果(包括不确定的)
  106. * 越大 = 只保留高置信度的识别结果
  107. - use_angle_cls: 角度分类器 (true/false)
  108. * true = 能更好识别倾斜、旋转的文字
  109. * false = 速度更快,但对倾斜文字效果差
  110. 🔧 高级参数:
  111. - det_db_unclip_ratio: 文字框扩展比例 (1.2-2.0)
  112. * 控制检测框相对于实际文字的大小
  113. * 越大 = 框越大,能包含更多边缘文字
  114. - rec_image_shape: 识别图片尺寸 ("3, 48, 320")
  115. * 固定值,不建议修改(模型限制)
  116. - det_box_type: 检测框类型 ("quad"/"poly")
  117. * quad = 四边形框(推荐)
  118. * poly = 多边形框(更精确但复杂)
  119. */
  120. export {
  121. usePresetConfig,
  122. useCustomConfig,
  123. useDirectConfig
  124. };