【文字识别】Python3使用百度AI进行文字识别

将图片翻译成文字一般被称为光学文字识别(Optical Character Recognition,OCR)。可以实现OCR 的底层库并不多,目前很多库都是使用共同的几个底层OCR 库,或者是在上面进行定制。
Tesseract 是一个OCR 库,目前由Google 赞助(Google 也是一家以OCR 和机器学习技术闻名于世的公司)。Tesseract 是目前公认最优秀、最精确的开源OCR 系统。
除了极高的精确度,Tesseract 也具有很高的灵活性。它可以通过训练识别出任何字体(只要这些字体的风格保持不变就可以),也可以识别出任何Unicode 字符。

此外我们也可以调用百度AI的文字识别API进行文字识别,具体流程如下:

1. 百度智能云注册用户

百度智能云地址:https://cloud.baidu.com/

2. 找到产品服务 / 文字识别 - 概览,创建应用

应用有三个关键参数:AppID,API Key,Secret Key

3. python脚本调用文字识别接口

具体api文档参考下面OCR Python SDK地址

OCR Python SDK地址:https://ai.baidu.com/docs#/OCR-Python-SDK/fad9fbb6

4. 具体文字识别代码如下:只是展示了几个接口,具体的还是看上面OCR Python SDK地址文档说明

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者:cacho_37967865
#博客:https://blog.csdn.net/sinat_37967865
#文件:baiduAI.py
#日期:2019-06-18
#备注:Python利用百度AI进行文字识别, pip install baidu-aip
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from aip import AipOcr

# 定义常量
APP_ID = '16552814'
API_KEY = '93R0OpMdmPy31WBBgPEKE1qB'
SECRET_KEY = 'kIClrcG******'

# 初始化AipFace对象
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)


# 打开图片
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


# 调用通用文字识别接口
def basicGeneral(file):
    """ 如果有可选参数 """
    options = {}
    options["detect_direction"] = "true"  # 检测朝向
    options["detect_language"] = "true"  # 检测语言
    result = aipOcr.basicGeneral(file, options)
    return(result)


# 通用文字识别(高精度版)
def basicAccurate(file):
    options = {}
    options["detect_direction"] = "true"  # 检测朝向
    options["detect_language"] = "true"  # 检测语言
    result = aipOcr.basicAccurate(file, options)
    return (result)


# 识别一些网络上背景复杂,特殊字体的文字。
def webImage(file):
    options = {}
    options["detect_direction"] = "true"  # 检测朝向
    options["detect_language"] = "true"  # 检测语言
    result = aipOcr.webImage(file, options)
    return (result)


def main():
    file = get_file_content("bd.png")
    result = basicGeneral(file)
    print(result)
    for word in result['words_result']:
        print(word['words'])

if __name__ == '__main__':
    main()

识别后的文字:


版权声明:本文为sinat_37967865原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。