文字识别tesseract-ocr(python接口)

一、tesseract-ocr简介

       Tesseract-ocr引擎最先由HP实验室于1985年开始研发,至1995年时已经成为OCR业内最准确的三款识别引擎之一。2005年,Tesseract由美国内华达州信息技术研究所获得,并求诸于Google对Tesseract进行改进、消除Bug、优化工作。Tesseract目前已作为开源项目发布在Google Project。

二、tesseract-ocr环境配置

       运行环境:Win7+python3+tesseract 4.0.0-beta.1;本文参考依据:https://www.cnblogs.com/vipstone/p/8894145.html  。

1、安装pytesseract库

       pip install pytesseract 即可。

2、安装pytesseract-ocr工具

       pytesseract库是python的函数库,其依赖于tesseract-ocr工具的运行,因此需要下载安装tesseract-ocr,并将其与pytesseract库进行关联设置。

       pytesseract-ocr工具下载地址:https://github.com/UB-Mannheim/tesseract/wiki   ,点击“tesseract-ocr-w64-setup-v4.0.0-beta.1.20180414.exe”/“tesseract-ocr-w32-setup-v4.0.0-beta.1.20180414.exe”下载安装。注意:安装时应该选中中文包!

3、tesseract关联设置

      在pytesseract安装路径下(路径一般为:“....../lib/site-packages\pytesseract\pytesseract.py”)找到pytesseract.py文件,打开文件修改如下语句:

文字识别tesseract-ocr(python接口)

       上面的路径是pytesseract-ocr工具的安装路径。

三、字符识别实例

from PIL import Image
import pytesseract
import cv2

path = "test.jpg" #设置图片路径
img = cv2.imread(path) #读取图片
text = pytesseract.image_to_string(img,lang="chi_sim") #将图片中的文字转换成字符串
# text = pytesseract.image_to_string(Image.open(path),lang="chi_sim")
print(text) #打印输出

        运行效果如下:

文字识别tesseract-ocr(python接口)

四、tesseract库训练

        作为非常优秀的Ocr识别库,tesseract当然可以训练自己的数据模型,从而达到为我所用目的,后续文字会介绍如果训练自己的文字识别库。