Tesseract-OCR-3.0.5 数字识别训练与合并多次训练数据

最近项目中有个需求,使用手持设备对3C码进行拍照识别,最后决定使用Tesseract-OCR,刚才对这个不了解,网上一大堆帖子,按照步骤操作下来,要么报错,要么就是标题党,实在是很恶心。为了以后可能还是用到,特意记录下来。

我的环境

软件安装

Tesseract-OCR-3.0.5

安装比较简单,双击直接安装即可,也不需要更改目录。安装完毕后需要配置环境变量。

TESSDATA_PREFIX
C:\Program Files (x86)\Tesseract-OCR\tessdata
path
C:\Program Files (x86)\Tesseract-OCR

TESSDATA_PREFIX 配置:
TESSDATA_PREFIX 配置截图
path 配置
path 环境变量配置截图

jTessBoxEditor-2.2.0 安装

这个安装直接解压即可,这个软件需要有JAVA的环境,有关JAVA的安装和环境变量配置比较简单,这里就不介绍了,注意如果没有java环境这个软件是运行不起来的。

具体步骤

下面这张图是我已经完成的截图,其中num.traineddata,这个文件就是最后生成的训练文件。
num-1、num-2、num-3文件夹是存放的需要学习的图片,我为什么分了3个文件夹呢?是这样,为了提高我们日后识别的准确率,所以这个训练的过程是持续的,num-1表示第一次训练学习的数据,num-2表示第二次训练学习的数据,依次类推,这样的好处就是,省去了上次学习训练的重新校对box文件。我们只需要把本次需要训练学习的数据生成tif,在生成box文件进行对本次的内容进行校对即可,然后后续合并结果,生成最终的训练好的文件来使用。
训练完毕的截图

准备训练数据

由于项目的识别的内容都是数字,所以我准备的训练数据都是带有数字的图片

num-1文件夹训练数据内容
num-3文件夹训练数据内容
num-2文件夹训练数据内容
在这里插入图片描述
num-3文件夹训练数据内容
num-3文件夹训练数据内容
准备好数据目录结构
在这里插入图片描述

生成tif文件

使用jTessBoxEditor工具生成tif文件,为了方便后续操作,将生成好的tif文件保持到Scan-OCR目录下,解压jTessBoxEditor压缩包后进入双击train.bat即可运行。

打开jTessBoxEditor工具后,点击Tools,点击Merge TIFF,选中num-1文件夹中所有图片,点击打开。
在这里插入图片描述
调整保存目录,保存名为:num.font.exp1.tif 然后点击保存。
在这里插入图片描述
点击保存后,提示完成了num.font.exp1.tif文件的创建。在Scan-OCR目录下可以看到刚创建的文件
在这里插入图片描述
上面已经完成了num-1文件夹中训练数据tif文件的创建,num-2、num-3文件训练数据创建tif重复上面步骤即可
完成3个文件夹创建trf文件的目录结构:
trf文件创建截图

生成bok文件

通过cmd命令的方式进行生成3个box文件,命令:

tesseract num.font.exp1.tif num.font.exp1 batch.nochop makebox
tesseract num.font.exp2.tif num.font.exp2 batch.nochop makebox
tesseract num.font.exp3.tif num.font.exp3 batch.nochop makebox

执行命令过程:

D:\Scan-OCR>tesseract num.font.exp1.tif num.font.exp1 batch.nochop makebox
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 4
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 5
Warning. Invalid resolution 1 dpi. Using 70 instead.

D:\Scan-OCR>tesseract num.font.exp2.tif num.font.exp2 batch.nochop makebox
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.

D:\Scan-OCR>tesseract num.font.exp3.tif num.font.exp3 batch.nochop makebox
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 4
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 5
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 6
Warning. Invalid resolution 1 dpi. Using 70 instead.
Page 7
Warning. Invalid resolution 1 dpi. Using 70 instead.

D:\Scan-OCR>

执行命令后,会生成3个box文件
bok文件创建截图

字符和位置进行校正

使用jTessBoxEditor工具打开每个tif进行字符和位置校正,然后保存即可
打开trf文件截图
截图
图中:
char 表示识别的字符
x y width height 表示字符的位置信息,我们微调的内容
1)字符是否识别正确
2)字符位置信息是否正确(比如图中字符2char对应的字符是正确的,但是位置信息不正确,经过调整,如下:)
在这里插入图片描述
如果需要调整,我们调整后需要保存一下,注意每张被训练的图片,调整后的信息都是存放在对应的bok文件中的。感兴趣的,可以打开看看。
这个微调的过程很枯燥,都是重复性的工作,慢慢的调整完所有的图片后,保存就可以进行下一步操作。

生成TR文件

tesseract num.font.exp1.tif num.font.exp1 nobatch box.train
tesseract num.font.exp2.tif num.font.exp2 nobatch box.train
tesseract num.font.exp3.tif num.font.exp3 nobatch box.train

执行命令过程:

D:\Scan-OCR>tesseract num.font.exp1.tif num.font.exp1 nobatch box.train
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      11
   Found 11 good blobs.
Generated training data for 2 words
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      13
   Found 13 good blobs.
Generated training data for 3 words
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words
Page 4
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words
Page 5
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 3 words

D:\Scan-OCR>tesseract num.font.exp2.tif num.font.exp2 nobatch box.train
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 2 words
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words

D:\Scan-OCR>tesseract num.font.exp3.tif num.font.exp3 nobatch box.train
Tesseract Open Source OCR Engine v3.05.02 with Leptonica
Page 1
Warning. Invalid resolution 1 dpi. Using 70 instead.
row xheight=78.5, but median xheight = 11.5
APPLY_BOXES:
   Boxes read from boxfile:      13
   Found 13 good blobs.
Generated training data for 2 words
Page 2
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words
Page 3
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 2 words
Page 4
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      12
   Found 12 good blobs.
Generated training data for 1 words
Page 5
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:       6
   Found 6 good blobs.
Generated training data for 1 words
Page 6
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:       6
   Found 6 good blobs.
Generated training data for 1 words
Page 7
Warning. Invalid resolution 1 dpi. Using 70 instead.
APPLY_BOXES:
   Boxes read from boxfile:      14
   Found 14 good blobs.
Generated training data for 3 words

D:\Scan-OCR>

执行命令后,会生成3个tr文件
tr文件截图

新建字体特征文件

创建一个名称为font_properties的字体特征文件。文件内容格式:

其中fontname为字体名称,必须与[lang].[fontname].exp[num].box中的名称保持一致。
、 、 、、 的取值为1或0,表示字体是否具有这些属性。
在Scan-OCR目录下创建一个名称为font_properties的文件,用记事本打开,输入以下下内容:

font 0 0 0 0 0

这里全取值为0,表示字体不是粗体、斜体等等。注意font_properties文件是没有拓展名的
在这里插入图片描述

从所有文件中提取字符

输入命令,生成unicharset文件

D:\Scan-OCR>unicharset_extractor num.font.exp1.box num.font.exp2.box num.font.exp3.box
Extracting unicharset from num.font.exp1.box
Extracting unicharset from num.font.exp2.box
Extracting unicharset from num.font.exp3.box
Wrote unicharset file ./unicharset.

生成shape文件

输入命令生成shapetable文件:

D:\Scan-OCR>shapeclustering -F font_properties -U unicharset num.font.exp1.tr num.font.exp2.tr num.font.exp3.tr
Reading num.font.exp1.tr ...
Reading num.font.exp2.tr ...
Reading num.font.exp3.tr ...
Bad properties for index 3, char 3: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 4, char 0: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 5, char 9: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 6, char 4: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 7, char 1: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 8, char 2: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 9, char 8: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 10, char 6: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 11, char 7: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 12, char 5: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 13, char ?: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 14, char F: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 15, char 垄: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 16, char ~: 0,255 0,255 0,0 0,0 0,0
Building master shape table
Computing shape distances...
Stopped with 0 merged, min dist 999.000000
Computing shape distances...
Stopped with 0 merged, min dist 999.000000
Computing shape distances...
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0
Stopped with 0 merged, min dist 999.000000
Computing shape distances...
Stopped with 0 merged, min dist 999.000000
Computing shape distances...
Stopped with 0 merged, min dist 999.000000
Computing shape distances... 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Stopped with 0 merged, min dist 0.100629
Master shape_table:Number of shapes = 14 max unichars = 1 number with multiple unichars = 0

生成聚集字符特征文件

D:\Scan-OCR>mftraining -F font_properties -U unicharset -O unicharset num.font.exp1.tr num.font.exp2.tr num.font.exp3.tr
Read shape table shapetable of 14 shapes
Reading num.font.exp1.tr ...
Reading num.font.exp2.tr ...
Reading num.font.exp3.tr ...
Bad properties for index 3, char 3: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 4, char 0: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 5, char 9: 0,255 0,255 0,0 0,0 0,0
Bad properties for index 6, char 4: 0,255 0,255 0,0 0,0 0,0
Bad propert
  • 29
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: tesseract-ocr是一种开源的OCR(光学字符识别)引擎,可以用于识别各种语言的文字。对于中文的文字识别,我们需要进行训练来构建一个适合中文的字库。 首先,我们需要准备一个包含足够多的中文字符的训练数据集。这些数据集可以包括手写字、印刷体字、不同字体和大小的字等。然后,我们需要将这些图片转换成tesseract-ocr可以识别的格式(比如tif或png格式)。 接下来,我们需要创建一个训练文件,该文件应该包含每个字符的图像文件名和相应的unicode编码。这可以通过编写一个脚本来实现。然后,我们使用tesseract-ocr训练命令对这些字符进行训练,生成一个字库文件(通常是一个.traineddata文件)。 训练命令的具体使用方法可以参考tesseract的官方文档。一般来说,我们需要指定训练数据集的路径、字库文件的输出路径以及其他一些配置参数,如迭代次数、学习率等。 训练完成后,我们可以将生成的字库文件放置到tesseract-ocr的语言包目录中。然后,使用tesseract-ocr识别命令,指定使用中文字库进行识别。 需要注意的是,tesseract-ocr识别中文时可能存在一些问题,特别是对于手写体和一些特殊字体。因此,我们需要对识别结果进行后处理,如去除一些错误的字符或进行字符校验。 总之,通过训练字库,我们可以使用tesseract-ocr识别中文字符,为中文OCR应用提供支持。虽然训练过程需要一些时间和精力,但它可以提高对中文文字的准确识别率,并且允许我们自定义和扩展字库,以满足特定需求。 ### 回答2: Tesseract-OCR是一种强大的开源光学字符识别OCR)引擎,可用于将印刷体文本转换为可编辑的文本格式。Tesseract能够识别多种语言,包括中文。 要让Tesseract识别中文,需进行一些额外的配置和训练。首先,你需要下载中文训练数据集,如chi_sim.traineddata。这个数据集包含了Tesseract学习如何识别中文字符的训练信息。 然后,将训练数据集放置在Tesseract训练数据目录下。接下来,你需要对Tesseract进行设置,以指定要使用的训练数据集。这可以通过在代码中使用`TessBaseAPI`库和`SetVariable`函数来完成。 创建一个Tesseract实例后,你可以使用`SetVariable`函数设置`lang`变量为"chi_sim",以指定要使用的训练数据集为中文。 之后,你可以加载要识别的图像,并使用`Recognize`函数将其识别为文本。Tesseract将尝试识别图像中的文字,并将结果返回为可编辑的文本。 以下是一个简单的示例代码: ```c #include <tesseract/baseapi.h> #include <leptonica/allheaders.h> int main() { // 创建Tesseract实例 tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI(); // 设置要使用的训练数据集为中文 api->SetVariable("lang", "chi_sim"); // 加载要识别的图像 Pix* image = pixRead("/path/to/your/image.png"); api->SetImage(image); // 进行识别 api->Recognize(0); // 获取识别结果 char* text = api->GetUTF8Text(); printf("识别结果:\n%s", text); // 释放资源 api->End(); delete[] text; pixDestroy(&image); return 0; } ``` 通过以上步骤,你可以使用Tesseract-OCR识别中文,并获取到识别结果。这样,你就可以将印刷体中文文本转换为可编辑的文本格式,便于进一步处理和使用。 ### 回答3: Tesseract-OCR是一个开源的OCR(Optical Character Recognition,光学字符识别)引擎,它最初由惠普实验室开发,现在由谷歌维护。它能够将图像中的文字转换成可编辑的文本,并且支持多种语言的识别,包括中文。 在Tesseract-OCR识别中文的过程中,关键是配置合适的字库并进行训练。字库是一系列用于训练OCR引擎的字符图像和相应的字符编码的集合。 首先,我们需要准备一组中文字符的图像样本,这些样本应该尽量包含不同的字体、大小和风格。可以使用现有的中文字符数据集,或者手动收集一些样本。接下来,将这些字符图像转换为合适的格式,例如,可以将它们转换为tif格式。 然后,我们需要创建一个训练字库的配置文件。在这个配置文件中,我们需要指定字库的名称,训练过程中使用的字符集,以及其他相关的参数。可以通过编辑一个名为“unicharset”的文件来配置这些内容。 接下来,我们需要运行训练命令来训练字库。在命令行中,可以使用tesseract命令来运行训练过程。可以指定训练数据的路径、配置文件的路径和输出模型的路径。运行命令后,训练过程将会开始,并且会生成一个训练好的字库模型文件。 最后,我们可以使用训练好的字库模型来识别中文。在使用Tesseract-OCR进行中文识别时,我们可以通过设置识别语言为中文,以及加载之前训练好的字库模型来进行识别。 总结来说,在使用Tesseract-OCR识别中文的过程中,需要准备中文字符的图像样本,创建训练字库的配置文件,运行训练命令进行字库训练,最后使用训练好的字库模型进行中文识别
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值