Python--文字识别--Tesseract

来源:互联网 发布:拜占庭容错共识算法 编辑:程序博客网 时间:2024/05/16 00:57

1.介绍

Tesseract 是一个 OCR 库,目前由 Google 赞助(Google 也是一家以 OCR 和机器学习技术闻名于世的公司)。Tesseract 是目前公认最优秀、最精确的开源 OCR 系统。 除了极高的精确度,Tesseract 也具有很高的灵活性。它可以通过训练识别出任何字体,也可以识别出任何 Unicode 字符。

2.安装

以下所有都是在mac上安装实现的

//安装tesseract的同时安装训练工具brew install --with-training-tools tesseract//安装tesseract的同时安装所有语言,语言包比较大,如果安装的话时间较长,建议不安装,按需选择brew install  --all-languages tesseract//安装tesseract,并安装训练工具和语言brew install --all-languages --with-training-tools tesseract //只安装tesseract,不安装训练工具brew install  tesseract

3.下载语言库

下载地址:https://github.com/tesseract-ocr/tessdata
默认自带的是英语
根据自己的需求选择所要的语言库,在这里我们选择的是简体中文所以选择的库是:chi_sim.traineddata
将文件拷贝到到:/usr/local/Cellar/tesseract/(你下载的版本号)/share/tessdata目录下。

4.Tesseract的命令行使用

一般使用:

//默认使用eng文字库, imgName是图片的地址,result识别结果tesseract imgName result

指定语言:

//指定使用简体中文tesseract -l chi_sim imgName result//查看本地存在的语言库tesseract --list-langs

指定多语言:

//指定多语言,用+号相连tesseract -l chi_sim+eng imgName result

5.Tesseract在Python中使用

通过 pip 安装支持Python 版本的 Tesseract库

pip install pytesseract

通过Python代码的简单实现

import pytesseractfrom PIL import Imageimage = Image.open('/Users/admin/Desktop/test.jpg')text = pytesseract.image_to_string(image)print text

是爬虫中的验证码的识别,可以通过更换别人训练好的语言包来识别.如果想自己通过训练来获得语言包也是可以的.

6.Tesseract训练

这里放个官方文档,等我练好了再分享
https://github.com/tesseract-ocr/tesseract/wiki

原创粉丝点击