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

猜你喜欢

转载自blog.csdn.net/wwj_748/article/details/78109680