Tesseract官方文檔頁(yè)面
jTessBoxEditor官方文檔頁(yè)面
工具和環(huán)境準(zhǔn)備
- Tesseract-OCR引擎
- jTessBoxEditor用來訓(xùn)練字庫(kù)
- Tesseract-OCR在centos 7中安裝肌割,jTessBoxEditor安裝在win中
安裝Tesseract
之所以選擇在centos 7下安裝Tesseract柄粹,因?yàn)樵诖酥鞍惭b過win版本弦讽,和在centos 6編譯和yum安裝過,但是在使用過程中都會(huì)提示缺少某一部分內(nèi)容。
在centos 7下選擇了yum安裝柏靶。
在yum安裝前诈火,需要epel源。
[root@docker01 yum.repos.d]# yum install epel-release
/etc/yum.repos.d目錄下就多了一個(gè)epel.repo文件
開始yum安裝Tesseract
[root@docker01 yum.repos.d]# yum install tesseract
這樣就會(huì)自動(dòng)解決一些依賴關(guān)系掸屡,省的到后面用的時(shí)候出現(xiàn)各種缺少文件提示。
依賴關(guān)系解決
=======================================================================================================================================================================================
Package 架構(gòu) 版本 源 大小
=======================================================================================================================================================================================
正在安裝:
tesseract x86_64 3.04.00-3.el7 epel 11 M
為依賴而安裝:
cairo x86_64 1.14.2-1.el7 base 711 k
fontconfig x86_64 2.10.95-7.el7 base 228 k
fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k
giflib x86_64 4.1.6-9.el7 base 40 k
graphite2 x86_64 1.3.6-1.el7_2 updates 112 k
harfbuzz x86_64 0.9.36-1.el7 base 156 k
jbigkit-libs x86_64 2.0-11.el7 base 46 k
leptonica x86_64 1.72-2.el7 epel 928 k
libICE x86_64 1.0.9-2.el7 base 65 k
libSM x86_64 1.2.2-2.el7 base 39 k
libX11 x86_64 1.6.3-2.el7 base 605 k
libX11-common noarch 1.6.3-2.el7 base 162 k
libXau x86_64 1.0.8-2.1.el7 base 29 k
libXdamage x86_64 1.1.4-4.1.el7 base 20 k
libXext x86_64 1.3.3-3.el7 base 39 k
libXfixes x86_64 5.0.1-2.1.el7 base 18 k
libXft x86_64 2.3.2-2.el7 base 58 k
libXrender x86_64 0.9.8-2.1.el7 base 25 k
libXxf86vm x86_64 1.1.3-2.1.el7 base 17 k
libicu x86_64 50.1.2-15.el7 base 6.9 M
libjpeg-turbo x86_64 1.2.90-5.el7 base 134 k
libpng x86_64 2:1.5.13-7.el7_2 updates 213 k
libthai x86_64 0.1.14-9.el7 base 187 k
libtiff x86_64 4.0.3-25.el7_2 updates 169 k
libwebp x86_64 0.3.0-3.el7 base 170 k
libxcb x86_64 1.11-4.el7 base 189 k
libxshmfence x86_64 1.2-1.el7 base 7.2 k
mesa-libEGL x86_64 10.6.5-3.20150824.el7 base 74 k
mesa-libGL x86_64 10.6.5-3.20150824.el7 base 184 k
mesa-libgbm x86_64 10.6.5-3.20150824.el7 base 40 k
mesa-libglapi x86_64 10.6.5-3.20150824.el7 base 39 k
pango x86_64 1.36.8-2.el7 base 287 k
pixman x86_64 0.32.6-3.el7 base 254 k
測(cè)試是否安裝成功
[root@docker01 tesseract]# tesseract
Usage:
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 OCR
3 = 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.
查看當(dāng)前有哪些語言環(huán)境
[root@docker01 tesseract]# tesseract --list-langs
List of available languages (2):
eng
就一個(gè)英語環(huán)境然评。
語言包所在的目錄
[root@docker01 tessdata]# pwd
/usr/share/tesseract/tessdata
[root@docker01 tessdata]# ll
總用量 37624
drwxr-xr-x 2 root root 4096 10月 25 22:51 configs
-rw-r--r-- 1 root root 171918 6月 25 2015 eng.cube.bigrams
-rw-r--r-- 1 root root 38 6月 25 2015 eng.cube.fold
-rw-r--r-- 1 root root 181 6月 25 2015 eng.cube.lm
-rw-r--r-- 1 root root 857304 6月 25 2015 eng.cube.nn
-rw-r--r-- 1 root root 254 6月 25 2015 eng.cube.params
-rw-r--r-- 1 root root 13020078 6月 25 2015 eng.cube.size
-rw-r--r-- 1 root root 2444187 6月 25 2015 eng.cube.word-freq
-rw-r--r-- 1 root root 996 6月 25 2015 eng.tesseract_cube.nn
-rw-r--r-- 1 root root 21876550 6月 25 2015 eng.traineddata
-rw-r--r-- 1 root root 124215 10月 25 23:08 normal.traineddata
-rw-r--r-- 1 root root 568 1月 26 2016 pdf.ttf
drwxr-xr-x 2 root root 92 10月 25 22:51 tessconfigs
后期若要添加語言包仅财,則可下載語言包后放到這里面。
pkgs.org中對(duì)tesseract的安裝說明碗淌,已經(jīng)安裝后的一些文件信息
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,解壓碎罚,運(yùn)行train.bat文件即可運(yùn)行
![](https://img.alicdn.com/imgextra/i3/490506202/TB2XGIXaLOM.eBjSZFqXXculVXa-490506202.jpg)
運(yùn)行后界面圖
![](https://img.alicdn.com/imgextra/i3/490506202/TB2iqQdaQ5M.eBjSZFrXXXPgVXa-490506202.jpg)
至此兩個(gè)所需要的軟件安裝結(jié)束磅废。
初步識(shí)別工作
準(zhǔn)備幾張圖片
![](https://img.alicdn.com/imgextra/i3/490506202/TB2ub3XaOGO.eBjSZFpXXb3tFXa-490506202.jpg)
把這幾張圖片傳到安裝tesseract的機(jī)器上
[root@docker01 test01]# ll
總用量 24
-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
開始識(shí)別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
這是在該目錄下多了一個(gè)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
查看所識(shí)別到的內(nèi)容
[root@docker01 test01]# cat out.0.txt
[54v
和圖片上的I54v
有點(diǎn)差別。
批量識(shí)別所有內(nèi)容
[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
查看識(shí)別出的內(nèi)容
[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
和上面的圖片對(duì)應(yīng)荆烈,其實(shí)就一個(gè)3.gif
圖片識(shí)別對(duì)了
訓(xùn)練工作
合成圖片工作
返回到win系統(tǒng)上拯勉,運(yùn)行jTessBoxEditor工具,把所有圖片合成一張.tif
格式的圖片
![](https://img.alicdn.com/imgextra/i1/490506202/TB22Ep_a5KO.eBjSZPhXXXqcpXa-490506202.jpg)
打開所有要合成的圖片
![](https://img.alicdn.com/imgextra/i2/490506202/TB2Q7Gaa.OO.eBjSZFLXXcxmXXa-490506202.jpg)
命名要合成圖片的名字
注:有關(guān)這個(gè)命名有個(gè)說法憔购,必須要按以下格式命名
tif命名規(guī)范:
[lang].[fontname].exp[num].tif
其中l(wèi)ang為語言名稱宫峦,fontname為字體名稱,num為序號(hào)玫鸟,可以隨便定義导绷。
但我試了其他的明白,直接命名也是正常的屎飘。
![](https://img.alicdn.com/imgextra/i4/490506202/TB2EkSgaZCO.eBjSZFzXXaRiVXa-490506202.jpg)
提示創(chuàng)建成功妥曲,在圖片目錄下生成一個(gè)
mytest.tif
的文件![](https://img.alicdn.com/imgextra/i1/490506202/TB2XTd_a0uO.eBjSZFCXXXULFXa-490506202.jpg)
生成box文件工作
把mytest.tif
文件上傳到centos 7 系統(tǒng)上
[root@docker01 04test]# ll
總用量 100
-rw-r--r-- 1 root root 99212 10月 26 15:42 mytest.tif
在mytest.tif所在的目錄下打開一個(gè)命令行,產(chǎn)生相應(yīng)的Box文件(*.box)
來生成一個(gè)box文件枚碗,該文件記錄了tesseract識(shí)別出來的每一個(gè)字和其位置坐標(biāo)逾一。
[root@docker01 04test]# tesseract mytest.tif mytest batch.nochop makebox
Tesseract Open Source OCR Engine v3.04.00 with Leptonica
Page 1
Page 2
Page 3
Empty page!!
Empty page!!
Empty page!!
Page 4
Page 5
Page 6
Page 7
Empty page!!
Empty page!!
Empty page!!
Page 8
Page 9
Page 10
Page 11
Page 12
Page 13
Page 14
Page 15
Page 16
Page 17
Empty page!!
Empty page!!
Empty page!!
Page 18
Page 19
Page 20
Page 21
Empty page!!
Empty page!!
Empty page!!
Warning in pixReadMemTiff: tiff page 21 not found
這時(shí)目錄多出了一個(gè)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
修正文字內(nèi)容
把mytest.box下載下來,放到win系統(tǒng)下肮雨,放到之前mytest.tif目錄下遵堵。
![](https://img.alicdn.com/imgextra/i1/490506202/TB2IKqlaH5K.eBjy0FfXXbApVXa-490506202.jpg)
使用jTessBoxEditor開始修正文字
![](https://img.alicdn.com/imgextra/i4/490506202/TB2FtunaSuJ.eBjy0FgXXXBBXXa-490506202.jpg)
修正文字會(huì)遇到的幾種情況
-
普通情況
可以看到,識(shí)別到的第一個(gè)值是6
怨规,但圖片中的值為e
陌宿,所以開始手動(dòng)修改
修改后,回車波丰,然后點(diǎn)擊save
保存
然后進(jìn)行一張圖片修正
若識(shí)別到的圖片的文字與圖片上一樣壳坪,即可繼續(xù)下一張圖片識(shí)別 -
表中無內(nèi)容
部分圖片可能由于背景顏色關(guān)系,導(dǎo)致此張圖片無法識(shí)別掰烟,可跳過繼續(xù)下一張識(shí)別爽蝴。
-
識(shí)別一半
例如以下圖片,四個(gè)字符纫骑,只被分割成兩個(gè)
此時(shí)蝎亚,可以用到分割識(shí)別框以及調(diào)整識(shí)別框位置的功能
調(diào)整后的圖形
Run Tesseract for Training
產(chǎn)生字符特征文件(*.tr)
把修正后的box文件傳回centos7系統(tǒng)中,刪除原來在centos 7系統(tǒng)中的box文件
[root@docker01 03test]# rm 200test.box
rm:是否刪除普通文件 "200test.box"先馆?y
[root@docker01 03test]# rz -by
rz 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
目錄下都了一個(gè)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.txt
Compute the Character Set
產(chǎn)生計(jì)算字符集(unicharset)
[root@docker01 03test]# unicharset_extractor 200test.box
Extracting unicharset from 200test.box
Wrote unicharset file ./unicharset.
定義字體特征文件并聚集字符特征
新建文件“font_properties”发框。那么需要在目錄下新建一個(gè)名字為“font_properties”的文件,并且輸入文本 :
注意:這里 200test 必須與訓(xùn)練名中的名稱保持一致,填入下面內(nèi)容 ,這里全取值為0煤墙,表示字體不是粗體梅惯、斜體等等宪拥。
200test 0 0 0 0 0
[root@docker01 03test]# ll
總用量 1764
-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 2301 10月 27 01:00 unicharset
[root@docker01 03test]# cat font_properties
200test 0 0 0 0 0
執(zhí)行命令:
[root@docker01 03test]# mftraining -F font_properties -U unicharset 200test.tr
Warning: 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.tr
Reading 200test.tr ...
Clustering ...
Writing normproto ...
此時(shí),在目錄下應(yīng)該生成若干個(gè)文件了铣减,把unicharset, inttemp, normproto, pffmtable這四個(gè)文件加上前綴“200test.”她君。然后 合并訓(xùn)練文件
[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
修改文件,并合并訓(xùn)練文件
[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.
此時(shí)目錄下“test200.traineddata”的文件拷貝到tesseract程序目錄下的“tessdata”目錄徙歼。
[root@docker01 03test]# cp test200.traineddata /usr/share/tesseract/tessdata
查看當(dāng)前語言包有哪些
[root@docker01 tesseract_test]# tesseract --list-langs
List of available languages (4):
eng
normal
myfont
test200
至此犁河,新的語言包已訓(xùn)練完成,下一步就是要用此語言包來識(shí)別圖形文字
再次識(shí)別
還是最開始的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
用一個(gè)循環(huán)批量識(shí)別
[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
識(shí)別后輸出的文件
[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
查看文件內(nèi)容魄梯,以及對(duì)比圖片
[root@docker01 test01]# cat out.*
l54v
ikh6
ynxn
7y28
nl 9c
w4zb
圖片內(nèi)容
![](https://img.alicdn.com/imgextra/i3/490506202/TB2ub3XaOGO.eBjSZFpXXb3tFXa-490506202.jpg)
可以對(duì)比下最開始的識(shí)別情況,識(shí)別率大大提高了宾符。