有个需求,须要从一张图片中识别出中文,经过python来实现,这种这么高大上的黑科技咱们普通人天然搞不了,去github找了一个彷佛能知足需求的开源库-tesseract-ocr:python
Tesseract的OCR引擎目前已做为开源项目发布在Google Project,其项目主页在这里查看https://github.com/tesseract-ocr,
它支持中文OCR,并提供了一个命令行工具。python中对应的包是pytesseract. 经过这个工具咱们能够识别图片上的文字。git
笔者的开发环境以下:github
安装tesseract web
brew install tesseract
安装python对应的包:pytesseractmacos
pip install pytesseract
怎么用?svg
若是要识别中文须要下载对应的训练集:https://github.com/tesseract-ocr/tessdata
,下载”chi_sim.traineddata”,而后copy到训练数据集的存放路径,如:工具
具体代码就几行:spa
#!/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)
OCR速度比较慢,你们能够拿一张包含中文的图片试验一下。命令行