Results tagged “ocr” from muse

Tesseract OCR 训练和识别总结

|

前段时间玩了一下 Tesseract OCR,结果可以看这篇文章。现在总结一下心得,希望对寻到这里读者也有帮助。

在训练时,第一个要确定的是怎么生成训练图像。我试过不同的字体渲染方式,最后决定还是用 Photoshop 生成,因为操作系统在渲染字体时加了彩色像素,如果直接抓屏取图,最后还是要处理成灰度的图像。第二个步骤是确定字体大小和图像的DPI值,我觉得这个要针对具体应用来决定,如果要识别手写字,图像应当和扫描仪扫出来的图像相近,如果要识别屏幕抓图,图像则应当和屏幕抓图相近。由此可以决定字体大小和图像DPI。

在训练过程中,Tesseract 经常抱怨 APPLY_BOXES 错误,我一开始浪费了许多时间更改字与字的间距。后来终于发现 -psm 这个参数,在运行时如果设置成 6 就可以解决这个识别问题,比如在生成 tr 文件时可以这样:

tesseract chi_sim.kaiti.exp1.png chi_sim.kaiti.exp1 -psm 6 nobatch box.train

个人觉得常用字符表可以有效的提高识别率,而且是最简单直接的办法。常用字在网上很多地方都有,只需要处理一下改成每个字单独一行,比如:

的
一
是
有
在
人
......

把这个文件存成 char_list,然后取最常用的字(比如排在前10%的字),剪切下来存到 frequent_char_list,注意是剪切,char_list 文件里不能再有这些最常用的字。然后生成 Tesseract 需要的 dawg 文件:

wordlist2dawg char_list chi_sim.word-dawg chi_sim.unicharset
wordlist2dawg frequent_char_list chi_sim.freq-dawg chi_sim.unicharset

这样一来,Tesseract 在识别的时候,会优先选择 frequent_char_list 里面的字。

在实际应用当中,我花了许多时间在提高识别率上,下面总结主要的几点:

  • 图片一定要预先处理过,比如锐化和黑白化,目的是去掉不必要的像素。
  • 字符分割可以规划到图片处理的步骤中,即增加字符之间的距离。
  • 对于标点符号的识别不要有太高的期望,这是个通病。

This is an installation guide for installing Tesseract OCR on Ubuntu 12.04 LTS.

First install the required libraries and tools for compiling.

sudo apt-get install libpng-dev libjpeg-dev libtiff-dev zlib1g-dev
sudo apt-get install gcc g++
sudo apt-get install autoconf automake libtool checkinstall

Install Leptonica from source. The latest version as of writing is 1.69.

wget http://www.leptonica.org/source/leptonica-1.69.tar.gz
tar -zxvf leptonica-1.69.tar.gz
cd leptonica-1.69
./configure
make
sudo checkinstall
sudo ldconfig

Then install Tesseract OCR from source.

wget https://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
tar -zxvf tesseract-ocr-3.02.02.tar.gz
cd tesseract-ocr
./autogen.sh
./configure
make (this may take a while)
sudo make install
sudo ldconfig

Finally, install the languages you want. Simply place the trained data under /usr/local/share/tessdata. You can do this through wget or FTP upload.

Below are some miscellaneous notes.

If you wish to call Tesseract from PHP, try this:

shell_exec("/usr/local/bin/tesseract input.png output -l eng");

On a side note, in the rare case you are running MAMP and the above code fails, you should edit the environment variables in /Applications/MAMP/Library/bin/envvars. Comment out the following lines as such:

#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH