Python中文图片OCR

有个需求,需要从一张图片中识别出中文,通过python来实现,这种这么高大上的黑科技我们普通人自然搞不了,去github找了一个似乎能满足需求的开源库-tesseract-ocr

Tesseract的OCR引擎目前已作为开源项目发布在Google Project,其项目主页在这里查看https://github.com/tesseract-ocr
它支持中文OCR,并提供了一个命令行工具。python中对应的包是pytesseract. 通过这个工具我们可以识别图片上的文字。

笔者的开发环境如下:

  • macosx
  • python 3.6
  • brew

**安装tesseract **

brew install tesseract

安装python对应的包:pytesseract

pip install pytesseract
这里写图片描述

怎么用?

如果要识别中文需要下载对应的训练集:https://github.com/tesseract-ocr/tessdata
,下载"chi_sim.traineddata",然后copy到训练数据集的存放路径,如:

这里写图片描述
这里写图片描述

具体代码就几行:

#!/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速度比较慢,大家可以拿一张包含中文的图片试验一下。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容