python识别图像并提取文字的实现方法识别图像并提取文字的实现方法
前言前言
python图像识别一般基础到的就是tesseract了,在爬虫中处理验证码广泛使用。
安装安装
安装教程网上大都差不多,Windows下确实比较麻烦,涉及到各种路径、环境变量甚至与linux不同的路径分隔符,所以这里
的安装是基于Centos7。
1. 依赖安装依赖安装
yum install -y automake autoconf libtool gcc gcc-c++
2. 安装安装leptonica
Leptonica主要用于图像处理和图像分析
原则上所有的库文件都是可以直接用yum安装的,如果想要具体的某个版本,可以前往官方源下载对应版本然后按照对应方式
编译
wget http://www.leptonica.org/source/leptonica-1.74.4.tar.gz
tar -zxvf leptonica-1.74.4.tar.gz
cd leptonica-1.74.4/
./configure
make && make install
3. 安装安装tesseract
其他各版本可以在这里下载并自行编译,也提供直接使用的文件。
yum install tesseract
4. 验证安装验证安装
tesseract --version
5. 语言包下载语言包下载
前往tesseract-ocr/tessdata下载相应的语言包,然后将之移动到tessdata目录下,可以用whereis tesseract查看一下具体的目
录,我的是/usr/share/tesseract/tessdata/mv *.traineddata /usr/local/share/tessdata/
6. 查看目前已下载的语言查看目前已下载的语言
tesseract –list-langs
使用使用
# tesseract
Usage:
tesseract --help | --help-psm | --help-oem | --version
tesseract --list-langs [--tessdata-dir PATH] tesseract --print-parameters [options...] [configfile...] tesseract imagename|stdin outputbase|stdout [options...]
[configfile...]
OCR options:
--tessdata-dir PATH Specify the location of tessdata path.
--user-words PATH Specify the location of user words file.
--user-patterns PATH Specify the location of user patterns file.
-l LANG[+LANG] Specify language(s) used for OCR.
-c VAR=VALUE Set value for config variables.
Multiple -c arguments are allowed.
--psm NUM Specify page segmentation mode.
--oem NUM Specify OCR Engine mode.
NOTE: These options must occur before any configfile.
语法语法
tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...]