OCR

Aip

新建AipOcr

AipOcr是OCR的Python SDK客户端,为使用OCR的开发人员提供了一系列的交互方法。

from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

读取图片,二进制格式

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')

本地通用文字识别

""" 调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image);

本地带参通用文字识别

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image, options)

远程通用文字识别,传入图片网络路径

url = "https//www.x.com/sample.jpg"

""" 调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url);

远程带参通用文字识别

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url, options)

 

Tesseract

OCR: 光学文字识别

Tesseract 是一个OCR 库,目前由Google 赞助(Google 也是一家以OCR 和机器学习技术闻名于世的公司)。Tesseract 是目前公认最优秀、最精确的开源OCR 系统

除了极高的精确度,Tesseract 也具有很高的灵活性。它可以通过训练识别出任何字体(只要这些字体的风格保持不变就可以),也可以识别出任何Unicode 字符.

Tesseract对于彩色图片的识别效果没有黑白图片的效果好

pytesseract

pytesseract是Tesseract关于Python的接口,可以使用Python调用Tesseract了.

不过还需要一个Python的图片处理模块,可以安装pillow. 

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = 'C://Program Files (x86)/Tesseract-OCR/tesseract.exe'
text = pytesseract.image_to_string(Image.open('E://figures/other/poems.jpg'))

print(text)

不过该方法识别率太差.

 

posted @ 2019-08-17 16:27  JamJarBranch  阅读(251)  评论(0)    收藏  举报