使用Tesseract-OCR训练文字识别记录
使用Tesseract-OCR訓練文字識別記錄
作者 毛毛卷彎彎from: http://www.jianshu.com/p/5c8c6b170f6f
Tesseract官方文檔頁面
https://github.com/tesseract-ocr/tesseract
jTessBoxEditor官方文檔頁面
http://vietocr.sourceforge.net/training.html
jTessBoxEditor is a box editor and trainer for Tesseract OCR, providing editing of box data of both Tesseract 2.0x and 3.0x formats and full automation of Tesseract training. It can read images of common image formats, including multi-page TIFF. The program requires Java Runtime Environment 7 or later.
工具和環境準備
- Tesseract-OCR引擎
- jTessBoxEditor用來訓練字庫
- Tesseract-OCR在centos 7中安裝,jTessBoxEditor安裝在win中
安裝Tesseract
之所以選擇在centos 7下安裝Tesseract,因為在此之前安裝過win版本,和在centos 6編譯和yum安裝過,但是在使用過程中都會提示缺少某一部分內容。
在centos 7下選擇了yum安裝。
在yum安裝前,需要epel源。
[root@docker01 yum.repos.d]# yum install epel-release
/etc/yum.repos.d目錄下就多了一個epel.repo文件
開始yum安裝Tesseract
[root@docker01 yum.repos.d]# yum install tesseract
這樣就會自動解決一些依賴關系,省的到后面用的時候出現各種缺少文件提示。
測試是否安裝成功
[root@docker01 tesseract]# tesseractUsage:tesseract imagename|stdin outputbase|stdout [options...] [configfile...]OCR options:--tessdata-dir /path specify the location of tessdata path--user-words /path/to/file specify the location of user words file--user-patterns /path/to/file specify the location of user patterns file-l lang[+lang] specify language(s) used for OCR-c configvar=value set value for control parameter.Multiple -c arguments are allowed.-psm pagesegmode specify page segmentation mode. These options must occur before any configfile.pagesegmode values are:0 = Orientation and script detection (OSD) only.1 = Automatic page segmentation with OSD.2 = Automatic page segmentation, but no OSD, or OCR3 = Fully automatic page segmentation, but no OSD. (Default)4 = Assume a single column of text of variable sizes.5 = Assume a single uniform block of vertically aligned text.6 = Assume a single uniform block of text.7 = Treat the image as a single text line.8 = Treat the image as a single word.9 = Treat the image as a single word in a circle.10 = Treat the image as a single character.Single options:-v --version: version info--list-langs: list available languages for tesseract engine. Can be used with --tessdata-dir.--print-parameters: print tesseract parameters to the stdout.查看當前有哪些語言環境
[root@docker01 tesseract]# tesseract --list-langsList of available languages (2): eng就一個英語環境。
語言包所在的目錄
后期若要添加語言包,則可下載語言包后放到這里面。
pkgs.org中對tesseract的安裝說明,已經安裝后的一些文件信息
https://pkgs.org/centos-7/epel-x86_64/tesseract-3.04.00-3.el7.x86_64.rpm.html
安裝jTessBoxEditor
jTessBoxEditor需要jre7(Java Runtime Environment)以上的版本支持。
安裝完jre后,下載jTessBoxEditor,解壓,運行train.bat文件即可運行
運行后界面圖
至此兩個所需要的軟件安裝結束。
初步識別工作
準備幾張圖片
把這幾張圖片傳到安裝tesseract的機器上
開始識別0.gif圖片
[root@docker01 test01]# tesseract 0.gif out.0 -l eng Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory這是在該目錄下多了一個out.0.txt文件
[root@docker01 test01]# ll 總用量 28 -rw-r--r-- 1 root root 1829 10月 24 16:05 0.gif -rw-r--r-- 1 root root 1930 10月 24 16:05 1.gif -rw-r--r-- 1 root root 1890 10月 24 16:05 2.gif -rw-r--r-- 1 root root 1986 10月 24 16:05 3.gif -rw-r--r-- 1 root root 1828 10月 24 16:05 4.gif -rw-r--r-- 1 root root 1866 10月 24 16:06 5.gif -rw-r--r-- 1 root root 6 10月 26 00:52 out.0.txt查看所識別到的內容
[root@docker01 test01]# cat out.0.txt [54v和圖片上的I54v有點差別。
批量識別所有內容
[root@docker01 test01]# for i in {1..5};do tesseract $i.gif out.$i -l eng;done Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory查看識別出的內容
[root@docker01 test01]# ll 總用量 48 -rw-r--r-- 1 root root 1829 10月 24 16:05 0.gif -rw-r--r-- 1 root root 1930 10月 24 16:05 1.gif -rw-r--r-- 1 root root 1890 10月 24 16:05 2.gif -rw-r--r-- 1 root root 1986 10月 24 16:05 3.gif -rw-r--r-- 1 root root 1828 10月 24 16:05 4.gif -rw-r--r-- 1 root root 1866 10月 24 16:06 5.gif -rw-r--r-- 1 root root 6 10月 26 00:52 out.0.txt -rw-r--r-- 1 root root 9 10月 26 01:00 out.1.txt -rw-r--r-- 1 root root 5 10月 26 01:00 out.2.txt -rw-r--r-- 1 root root 6 10月 26 01:00 out.3.txt -rw-r--r-- 1 root root 7 10月 26 01:00 out.4.txt -rw-r--r-- 1 root root 5 10月 26 01:00 out.5.txt [root@docker01 test01]# cat *.txt [54v ikhb‘ ymm 7y28 nl 9c mzb和上面的圖片對應,其實就一個3.gif圖片識別對了
訓練工作
合成圖片工作
返回到win系統上,運行jTessBoxEditor工具,把所有圖片合成一張.tif格式的圖片
打開所有要合成的圖片
命名要合成圖片的名字
注:有關這個命名有個說法,必須要按以下格式命名
tif命名規范:
[lang].[fontname].exp[num].tif
其中lang為語言名稱,fontname為字體名稱,num為序號,可以隨便定義。
但我試了其他的明白,直接命名也是正常的。
提示創建成功,在圖片目錄下生成一個mytest.tif的文件
生成box文件工作
把mytest.tif文件上傳到centos 7 系統上
[root@docker01 04test]# ll總用量 100 -rw-r--r-- 1 root root 99212 10月 26 15:42 mytest.tif在mytest.tif所在的目錄下打開一個命令行,產生相應的Box文件(*.box)
來生成一個box文件,該文件記錄了tesseract識別出來的每一個字和其位置坐標。
這時目錄多出了一個mytest.box和mytest.txt文件
[root@docker01 04test]# ll總用量 108 -rw-r--r-- 1 root root 1005 10月 26 23:52 mytest.box -rw-r--r-- 1 root root 99212 10月 26 15:42 mytest.tif -rw-r--r-- 1 root root 101 10月 26 23:52 mytest.txt修正文字內容
把mytest.box下載下來,放到win系統下,放到之前mytest.tif目錄下。
使用jTessBoxEditor開始修正文字
修正文字會遇到的幾種情況
-
普通情況
可以看到,識別到的第一個值是6,但圖片中的值為e,所以開始手動修改
修改后,回車,然后點擊save保存
然后進行一張圖片修正
若識別到的圖片的文字與圖片上一樣,即可繼續下一張圖片識別 -
表中無內容
部分圖片可能由于背景顏色關系,導致此張圖片無法識別,可跳過繼續下一張識別。 -
識別一半
例如以下圖片,四個字符,只被分割成兩個
此時,可以用到分割識別框以及調整識別框位置的功能
調整后的圖形
Run Tesseract for Training
產生字符特征文件(*.tr)
把修正后的box文件傳回centos7系統中,刪除原來在centos 7系統中的box文件
[root@docker01 03test]# rm 200test.boxrm:是否刪除普通文件 "200test.box"?y [root@docker01 03test]# rz -byrz waiting to receive. Starting zmodem transfer. Press Ctrl+C to cancel. Transferring 200test.box...100% 9 KB 9 KB/sec 00:00:01 0 Errors [root@docker01 03test]# tesseract 200test.tif 200test nobatch box.train目錄下都了一個tr文件
[root@docker01 03test]# ll總用量 1756 -rw-r--r-- 1 root root 10210 10月 26 16:53 200test.box -rw-r--r-- 1 root root 949532 10月 26 15:13 200test.tif -rw-r--r-- 1 root root 830214 10月 27 00:58 200test.tr -rw-r--r-- 1 root root 325 10月 27 00:58 200test.txtCompute the Character Set
產生計算字符集(unicharset)
[root@docker01 03test]# unicharset_extractor 200test.boxExtracting unicharset from 200test.box Wrote unicharset file ./unicharset.定義字體特征文件并聚集字符特征
新建文件“font_properties”。那么需要在目錄下新建一個名字為“font_properties”的文件,并且輸入文本 :
注意:這里 200test 必須與訓練名中的名稱保持一致,填入下面內容 ,這里全取值為0,表示字體不是粗體、斜體等等。
執行命令:
[root@docker01 03test]# mftraining -F font_properties -U unicharset 200test.trWarning: No shape table file present: shapetable Reading 200test.tr ... Flat shape table summary: Number of shapes = 43 max unichars = 1 number with multiple unichars = 0 Warning: no protos/configs for Joined in CreateIntTemplates() Warning: no protos/configs for |Broken|0|1 in CreateIntTemplates() Done!輸入命令:
[root@docker01 03test]# cntraining 200test.trReading 200test.tr ... Clustering ...Writing normproto ...此時,在目錄下應該生成若干個文件了,把unicharset, inttemp, normproto, pffmtable這四個文件加上前綴“200test.”。然后 合并訓練文件
[root@docker01 03test]# ll總用量 2100 -rw-r--r-- 1 root root 10210 10月 26 16:53 200test.box -rw-r--r-- 1 root root 949532 10月 26 15:13 200test.tif -rw-r--r-- 1 root root 830214 10月 27 00:58 200test.tr -rw-r--r-- 1 root root 325 10月 27 00:58 200test.txt -rw-r--r-- 1 root root 18 10月 27 01:02 font_properties -rw-r--r-- 1 root root 323869 10月 27 01:03 inttemp -rw-r--r-- 1 root root 5342 10月 27 01:04 normproto -rw-r--r-- 1 root root 341 10月 27 01:03 pffmtable -rw-r--r-- 1 root root 778 10月 27 01:03 shapetable -rw-r--r-- 1 root root 2301 10月 27 01:00 unicharset修改文件,并合并訓練文件
[root@docker01 03test]# ll總用量 2100 -rw-r--r-- 1 root root 10210 10月 26 16:53 200test.box -rw-r--r-- 1 root root 949532 10月 26 15:13 200test.tif -rw-r--r-- 1 root root 830214 10月 27 00:58 200test.tr -rw-r--r-- 1 root root 325 10月 27 00:58 200test.txt -rw-r--r-- 1 root root 18 10月 27 01:02 font_properties -rw-r--r-- 1 root root 323869 10月 27 01:03 test200.inttemp -rw-r--r-- 1 root root 5342 10月 27 01:04 test200.normproto -rw-r--r-- 1 root root 341 10月 27 01:03 test200.pffmtable -rw-r--r-- 1 root root 778 10月 27 01:03 test200.shapetable -rw-r--r-- 1 root root 2301 10月 27 01:00 test200.unicharse合并文件
[root@docker01 03test]# combine_tessdata test200.Combining tessdata files TessdataManager combined tesseract data files. Offset for type 0 (test200.config ) is -1 Offset for type 1 (test200.unicharset ) is 140 Offset for type 2 (test200.unicharambigs ) is -1 Offset for type 3 (test200.inttemp ) is 2441 Offset for type 4 (test200.pffmtable ) is 326310 Offset for type 5 (test200.normproto ) is 326651 Offset for type 6 (test200.punc-dawg ) is -1 Offset for type 7 (test200.word-dawg ) is -1 Offset for type 8 (test200.number-dawg ) is -1 Offset for type 9 (test200.freq-dawg ) is -1 Offset for type 10 (test200.fixed-length-dawgs ) is -1 Offset for type 11 (test200.cube-unicharset ) is -1 Offset for type 12 (test200.cube-word-dawg ) is -1 Offset for type 13 (test200.shapetable ) is 331993 Offset for type 14 (test200.bigram-dawg ) is -1 Offset for type 15 (test200.unambig-dawg ) is -1 Offset for type 16 (test200.params-model ) is -1 Output test200.traineddata created sucessfully.此時目錄下“test200.traineddata”的文件拷貝到tesseract程序目錄下的“tessdata”目錄。
[root@docker01 03test]# cp test200.traineddata /usr/share/tesseract/tessdata查看當前語言包有哪些
[root@docker01 tesseract_test]# tesseract --list-langs List of available languages (4): eng normal myfont test200至此,新的語言包已訓練完成,下一步就是要用此語言包來識別圖形文字
再次識別
還是最開始的5漲圖片
[root@docker01 test01]# ll總用量 44 -rw-r--r-- 1 root root 1829 10月 24 16:05 0.gif -rw-r--r-- 1 root root 1930 10月 24 16:05 1.gif -rw-r--r-- 1 root root 1890 10月 24 16:05 2.gif -rw-r--r-- 1 root root 1986 10月 24 16:05 3.gif -rw-r--r-- 1 root root 1828 10月 24 16:05 4.gif -rw-r--r-- 1 root root 1866 10月 24 16:06 5.gif用一個循環批量識別
[root@docker01 test01]# for i in {1..5};do tesseract $i.gif out.$i -l test200;done Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory Tesseract Open Source OCR Engine v3.04.00 with Leptonica Warning in pixReadMemGif: writing to a temp file, not directly to memory識別后輸出的文件
[root@docker01 test01]# ll 總用量 48 -rw-r--r-- 1 root root 1829 10月 24 16:05 0.gif -rw-r--r-- 1 root root 1930 10月 24 16:05 1.gif -rw-r--r-- 1 root root 1890 10月 24 16:05 2.gif -rw-r--r-- 1 root root 1986 10月 24 16:05 3.gif -rw-r--r-- 1 root root 1828 10月 24 16:05 4.gif -rw-r--r-- 1 root root 1866 10月 24 16:06 5.gif -rw-r--r-- 1 root root 6 10月 27 01:18 out.0.txt -rw-r--r-- 1 root root 6 10月 27 01:18 out.1.txt -rw-r--r-- 1 root root 6 10月 27 01:18 out.2.txt -rw-r--r-- 1 root root 6 10月 27 01:18 out.3.txt -rw-r--r-- 1 root root 7 10月 27 01:18 out.4.txt -rw-r--r-- 1 root root 6 10月 27 01:18 out.5.txt查看文件內容,以及對比圖片
[root@docker01 test01]# cat out.* l54vikh6ynxn7y28nl 9cw4zb圖片內容
可以對比下最開始的識別情況,識別率大大提高了。
總結
以上是生活随笔為你收集整理的使用Tesseract-OCR训练文字识别记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ 解析从浏览器端传过来的图像bas
- 下一篇: Go程序性能分析pprof