OCR6:Custom Traineddata

参考:https://groups.google.com/forum/#!msg/tesseract-ocr/MSYezIbckvs/kO1VoNKMDMQJ  

V4版本代码示例 :

import pytesseract
from PIL import Image as img

text = pytesseract.image_to_string(img.open('src2B1.jpg'), lang='teld+chi_sim', config='--psm 3 --oem 1')
print(text.replace('', ''))

 合并识别结果


在实际使用 tesseract-orc 识别库的时候,初次制作的识别库很有可能识别率不太理想,需要后期慢慢补充。将多个修正过的box文件合并成一个识别库。
  • 首先,需要图片样本.tif文件,位置文件.box ,只要有这两个文件在,就可以合并字典

假设已存在如下样品图片和修正过的box文件:

  • image.font.1.tif image.font.1.box
  • image.font.2.tif image.font.2.box
  • image.font.3.fit image.font.3.box

1、先生成相对应的 .tr 文件

  • tesseract image.font.1.tif image.font.1 nobatch box.train
  • tesseract image.font.2.tif image.font.2 nobatch box.train
  • tesseract image.font.3.tif image.font.3 nobatch box.train

2、提取字符

  • unicharset_extractor image.font.1.box image.font.2.box image.font.3.box

3、生成字体特征文件

  • echo image 0 0 0 0 0 >font_propertiesfont

4、执行如下命令

  • mftraining -F font -U unicharset image.font.1.tr image.font.2.tr image.font.3.tr

5、聚集所有.tr 文件

  • cntraining image.font.1.tr image.font.2.tr image.font.3.tr

6、重命名文件

  • unicharset
  • inttemp
  • normproto
  • pfftable
  • shapetable 

7、合并所有文件 生成一个大的字库文件

  • combine_tessdata image.

示例代码:

/*生成box文件*/
/*tesseract teld.shz.exp0.tif teld.shz.exp0 -l chi_sim --psm 3 --oem 1 batch.nochop makebox*/

tesseract teld.shz.exp0.tif teld.shz.exp0 -l chi_sim batch.nochop makebox

/*生成font_properties文件*/
echo shz 0 0 0 0 0 >font_properties

/*生成.tr训练文件*/
tesseract teld.shz.exp0.tif teld.shz.exp0 nobatch box.train

/*生成字符集文件*/
unicharset_extractor teld.shz.exp0.box

/*生成shape文件*/
shapeclustering -F font_properties -U unicharset  teld.shz.exp0.tr

/*生成聚字符特征文件*/
mftraining -F font_properties -U unicharset  teld.shz.exp0.tr

/*生成字符正常化特征文件*/
cntraining teld.shz.exp0.tr

/*文件重命名*/
rename normproto teld.normproto
rename inttemp teld.inttemp
rename pffmtable teld.pffmtable
rename shapetable teld.shapetable
rename unicharset teld.unicharset

/*合并训练文件*/
combine_tessdata teld.

 参考资料


原文地址:https://www.cnblogs.com/tgzhu/p/11512584.html