Tesseract默认情况下基本无法对LED数码管显示屏进行识别。但由于Tesseract具有样本机器学习训练的能力,因此可利用该技术实现Tesseract对LED数码管的OCR识别,这需要对图片进行预处理。
一、图像预处理
预处理步骤:
1)LED屏幕拍照,生成原始图像文件。
2)对图像灰度化处理。
3)对图像二值化处理。
4)对图像黑白反转。
5)按照实际需要的坐标和尺寸,对图像裁切去边,最终保留需要识别的部分。
以下是一个实际案例的处理过程。
拍照:
灰度化处理:
二值化处理:
黑白反转:
裁切去边后,按上下位置生成两张图片:
考虑小数点的影响,增加对0和5的识别,实际投入训练的是以下两张图片:
二、在jTessBoxEditorFX中进行样本训练
1、合并样本图片
执行train.bat启动jTessBoxEditorFX,选择菜单Tools->Merge TIFF执行图片合并,将上面两张图片文件合并为1个tif文件led.led_num.exp0.tif,关闭jTessBoxEditorFX。
合并后的文件命名格式有一定要求:
语法:[lang].[fontname].exp[num].tif
lang为语言名称,fontname为字体名称,num为序号,可自定义。
2、生成BOX文件
tesseract led.led_num.exp0.tif led.led_num.exp0 -l eng --psm 7 batch.nochop makebox
Tesseract Open Source OCR Engine vv4.0.0-beta.1.20180608 with Leptonica
Page 1
Page 2
运行后在目标文件夹生成文件led.led_num.exp0.box。
3、字符纠正
执行train.bat启动jTessBoxEditorFX,选择Box Editor,Open前面生成的合并样本图片led.led_num.exp0.tif,其对应的box文件会自动加载。
检查修改每页中的各个数字字符解析结果,有不正确的修改,完成后保存替换原box文件。数码管字符是倾斜的,小数点紧靠前面的数字,识别修正时注意字符的框选,可通过Insert、Delete等操作增减字符框,并调整字符框的坐标位置和宽度高度。
这是修改之后的状态:
4、创建font_properties文件
创建字体属性文件font_properties,该文件为文本文件,但没有扩展名:
echo led_num 0 0 0 0 0 >font_properties
语法:<fontname> <italic> <bold> <fixed> <serif> <fraktur>
fontname为字体名称,italic为斜体,bold为黑体字,fixed为默认字体,serif为衬线字体,fraktur德文黑字体,1和0代表有和无,精细区分时可使用。
5、生成训练文件
tesseract led.led_num.exp0.tif led.led_num.exp0 -l eng --psm 7 nobatch box.train
运行后在目标文件夹生成文件led.led_num.exp0.tr。
6、生成字符集文件
unicharset_extractor led.led_num.exp0.box
运行后在目标文件夹生成文件unicharset。
7、生成shape文件
shapeclustering -F font_properties -U unicharset -O led.unicharset led.led_num.exp0.tr
运行后在目标文件夹生成文件led.unicharset和shapetable。
8、生成聚集字符特征文件
mftraining -F font_properties -U unicharset -O led.unicharset led.led_num.exp0.tr
运行后在目标文件夹生成文件inttemp和pffmtable。
9、生成字符正常化特征文件
cntraining led.led_num.exp0.tr
运行后在目标文件夹生成文件normproto。
10、对特征文件更名
rename normproto led_num.normproto
rename inttemp led_num.inttemp
rename pffmtable led_num.pffmtable
rename unicharset led_num.unicharset
rename shapetable led_num.shapetable
11、合并训练文件
combine_tessdata led_num.
注意命令中最后有个小数点,运行后在目标文件夹生成文件led_num.traineddata。
12、将最终生成的训练数据文件led_num.traineddata复制到Tesseract-OCR软件安装路径的tessdata目录下,以后Tesseract即可将其作为新的语言识别包使用。
从以上第4步开始,可以合并到一个批处理build.bat中去处理:
@echo.Tesseract-OCR图像文字识别训练语言包生成器
@pause
@echo.
@echo.清理旧文件
@echo.
@del font_properties
@del *.tr
@del *.unicharset
@del *.inttemp
@del *.normproto
@del *.pffmtable
@del *.shapetable
@del *.traineddata
@echo.
@echo.创建font_properties文件
@echo %2 0 0 0 0 0 >font_properties
@echo.
@echo.生成训练文件
@tesseract %1.%2.exp0.tif %1.%2.exp0 -l eng --psm 7 nobatch box.train
@echo.
@echo.生成字符集文件
@unicharset_extractor %1.%2.exp0.box
@echo.
@echo.生成Shape文件
@shapeclustering -F font_properties -U unicharset -O %1.unicharset %1.%2.exp0.tr
@echo.
@echo.生成聚集字符特征文件
@mftraining -F font_properties -U unicharset -O %1.unicharset %1.%2.exp0.tr
@echo.
@echo.生成字符正常化特征文件
@cntraining %1.%2.exp0.tr
@echo.
@echo.对特征文件更名
@rename normproto %2.normproto
@rename inttemp %2.inttemp
@rename pffmtable %2.pffmtable
@rename unicharset %2.unicharset
@rename shapetable %2.shapetable
@echo.
@echo.合并训练文件
@combine_tessdata %2.
@echo.
@echo.Completed!
执行批处理命令
build led led_num
控制台运行过程如下:
Tesseract-OCR图像文字识别训练语言包生成器
请按任意键继续. . .
清理旧文件
找不到 E:\jTessBoxEditorFX\led_num\*.tr
找不到 E:\jTessBoxEditorFX\led_num\*.inttemp
找不到 E:\jTessBoxEditorFX\led_num\*.normproto
找不到 E:\jTessBoxEditorFX\led_num\*.pffmtable
找不到 E:\jTessBoxEditorFX\led_num\*.shapetable
找不到 E:\jTessBoxEditorFX\led_num\*.traineddata
创建font_properties文件
生成训练文件
Tesseract Open Source OCR Engine vv4.0.0-beta.1.20180608 with Leptonica
Page 1
APPLY_BOXES:
Boxes read from boxfile: 5
Found 5 good blobs.
Leaving 2 unlabelled blobs in 0 words.
Generated training data for 1 words
Page 2
APPLY_BOXES:
Boxes read from boxfile: 5
Found 5 good blobs.
Generated training data for 1 words
生成字符集文件
Extracting unicharset from box file led.led_num.exp0.box
Wrote unicharset file unicharset
生成Shape文件
Reading led.led_num.exp0.tr ...
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...
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
Stopped with 0 merged, min dist 0.225806
Master shape_table:Number of shapes = 7 max unichars = 1 number with multiple unichars = 0
生成聚集字符特征文件
Read shape table shapetable of 7 shapes
Reading led.led_num.exp0.tr ...
Warning: no protos/configs for Joined in CreateIntTemplates()
Warning: no protos/configs for |Broken|0|1 in CreateIntTemplates()
Done!
生成字符正常化特征文件
Reading led.led_num.exp0.tr ...
Clustering ...
Writing normproto ...
对特征文件更名
合并训练文件
Combining tessdata files
Output led_num.traineddata created successfully.
Version string:v4.0.0-beta.1.20180608
1:unicharset:size=554, offset=192
3:inttemp:size=127381, offset=746
4:pffmtable:size=90, offset=128127
5:normproto:size=1022, offset=128217
13:shapetable:size=130, offset=129239
23:version:size=22, offset=129369
Completed!
三、训练效果测试
对在图像预处理后生成的两张图片进行识别:
led_gbrt_1.png
tesseract led_gbrt_1.png led_gbrt_1 --psm 7 -l led_num
识别结果正确:
led_gbrt_2.png
tesseract led_gbrt_2.png led_gbrt_2 --psm 7 -l led_num
识别结果正确:
识别没有增加字符的图片:
1.png
tesseract 1.png 1 --psm 7 -l led_num
识别结果正确:
2.png
tesseract 2.png 2 --psm 7 -l led_num
识别结果正确:
总结:
训练过程中,对字符框选的坐标位置极其重要,直接影响训练后识别的准确度,可能需要反复调整对比识别效果才能确定。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28974745/viewspace-2218660/,如需转载,请注明出处,否则将追究法律责任。