以上示例为对文本对识别。
自行训练自定义数据
默认支持的训练数据相对比较弱,对于文字比较清晰的图片来说一般没有问题,但对于存在干扰的验证码这种需求来说,则需要通过自行训练来提升准确率。
tesseract.js 使用的训练数据只是将 tesseract-ocr gzip 压缩了一下而已,训练自定义的数据需要安装 tesseract-ocr 以及其训练数据相关工具。
具体的方法可参考官方相关仓库和文档:
- 安装 tesseract-OCR:https://github.com/tesseract-ocr/tessdoc/blob/master/Installation.md
- https://github.com/tesseract-ocr/tesstrain
- https://github.com/tesseract-ocr/tessdoc/blob/master/Compiling.md
另外也有一些可参考的教程:
- https://zhuanlan.zhihu.com/p/103714876
- https://github.com/kekxv/TesseractTrain
- https://www.bbsmax.com/A/6pdDb7pDJw/
- https://www.bbsmax.com/A/8Bz8KNOkdx/
- https://www.ershicimi.com/p/9bf5ec129082ac6800b1cb6f56c6f71c
- https://blog.csdn.net/sylsjane/article/details/83751297
- https://blog.csdn.net/holmofy/article/details/80867243
- https://blog.csdn.net/makesibushuohua/article/details/52058310
获取用于训练的图片
以验证码为例,下面为获取一定数量的验证码作为训练集。由于训练的图片只能为 png 格式,使用了 sharp 将下载得到的图片转换为 png 格式。sharp 安装过程中需要从 GitHub 仓库下载二进制资源,可能会因为无法下载而安装失败,我们执行如下命令设置从淘宝镜像下载相关资源: