Mac OCR 图像文字识别调研(tesseract & baidu clound)

OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法将形状翻译成计算机文字的过程。

本文记录使用python进行图片识别,尝试了两种办法,在此记录!

Tesseract


安装

Mac直接使用brew进行安装,未安装brew请点击!

# 安装tesseract的同时安装训练工具
brew install --with-training-tools tesseract

# 安装tesseract的同时安装所有语言,语言包比较大,建议不安装
brew install  --all-languages tesseract

# 安装tesseract,并安装训练工具和语言
brew install --all-languages --with-training-tools tesseract 

# 只安装tesseract,不安装训练工具,推荐安装!(我安装的这个!^_^)
brew install  tesseract

安装完成后,验证是否成功

tesseract -v

使用

命令行调用

# 获取帮助
tesseract --help
# imgNmae: 图片路径
# result: 识别结果,存放于result.txt文件
tesseract imgName result

Python调用

使用python调用,还需要安装两个包。

pip install pillow
pip insstall pytesseract

测试图片:
在这里插入图片描述
实例:

from PIL import Image
import pytesseract

image = Image.open('test.png')
text = pytesseract.image_to_string(image, lang='eng')
print(text)

输出结果:

/usr/local/bin/python3.7 /Users/test.py
The official site of the NBA | NBA.com

Process finished with exit code 0

如需识别中文或者其他语言,下载对应 语言库 即可。

百度云 OCR

安装

  • 首先需要注册一个百度账号,登录 后 在产品栏搜索 “文字识别”,点击 “立即使用” 跳转到概览页面。

  • 创建应用,拿到AppID、API Key、Secret Key
    在这里插入图片描述

  • 安装python库

pip install baidu-aip

使用

官方文档

from aip import AipOcr

config = {
    'appId': '',
    'apiKey': '',
    'secretKey': ''
}

client = AipOcr(**config)

def get_file_content(file):
    with open(file, 'rb') as fp:
        return fp.read()
        
image = get_file_content('test.png')
result = client.basicAccurate(image)

text = '\n'.join([w['words'] for w in result['words_result']])
print(text)

输出结果:

/usr/local/bin/python3.7 /Users/test.py
 The official site of the NBA NBA. come

Process finished with exit code 0

总结

  • 准确率
    经过多张图片的测试,发现百度AI的识别结果更加准确一些,也有可能是因为我对tesseract的了解不够全面。

  • 对新手友好度
    百度更好上手~接口文档全面滴很~功能也很多~主要是不用装很多依赖库~

  • 是否收费
    tesseract完全免费!
    百度 - 通用文字识别,一天可调用500次,不同功能的API有不同的次数限制,个人玩玩还是够用的!

猜你喜欢

转载自blog.csdn.net/lan_yangbi/article/details/89229742