从一张图片中识别出中文,通过python来实现,这种这么高大上的黑科技我们普通人自然搞不了,去github找了一个似乎能满足需求的开源库-tesseract-ocr:
You may encounter build failures or other breakages.
Please create pull-requests instead of filing issues.
==> Installing dependencies for tesseract: libpng, libtiff, leptonica
==> Installing tesseract dependency: libpng
==> Downloading https://homebrew.bintray.com/bottles/libpng-1.6.24.el_capitan.bo
################################################################## 100.0%
==> Pouring libpng-1.6.24.el_capitan.bottle.tar.gz
�� /usr/local/Cellar/libpng/1.6.24: 26 files, 1.2M
==> Installing tesseract dependency: libtiff
==> Downloading https://homebrew.bintray.com/bottles/libtiff-4.0.6_2.el_capitan.
################################################################## 100.0%
==> Pouring libtiff-4.0.6_2.el_capitan.bottle.tar.gz
�� /usr/local/Cellar/libtiff/4.0.6_2: 261 files, 3.4M
==> Installing tesseract dependency: leptonica
==> Downloading https://homebrew.bintray.com/bottles/leptonica-1.73.el_capitan.b
################################################################## 100.0%
==> Pouring leptonica-1.73.el_capitan.bottle.tar.gz
�� /usr/local/Cellar/leptonica/1.73: 50 files, 5.4M
==> Installing tesseract
==> Downloading https://homebrew.bintray.com/bottles/tesseract-3.04.01_2.el_capi
################################################################## 100.0%
==> Pouring tesseract-3.04.01_2.el_capitan.bottle.tar.gz
�� /usr/local/Cellar/tesseract/3.04.01_2: 76 files, 39M
在Mac上真的很快,就弄好了
笔者的开发环境如下:
macosx
python 3.6
brew
安装tesseract
brew install tesseract
1
安装python对应的包:pytesseract
pip install pytesseract 记得sudo避免权限问题
怎么用?
如果要识别中文需要下载对应的训练集:https://github.com/tesseract-ocr/tessdata
,下载”chi_sim.traineddata”,50.2 MB 然后copy到训练数据集的存放路径,如:
/tessearact/3.0xxx/share/
执行的代码:
!/usr/bin/env python3
-- coding: utf-8 --
import pytesseract
from PIL import Image
open image
image = Image.open(‘test.png’)
code = pytesseract.image_to_string(image, lang=’chi_sim’)
print(code)