B05.有意思的小东西 -百度OCR文字识别(图片转文字)[python]

简单版

首先你需要到百度AI申请响应的接口,拿到APP_ID, API_KEY, SECRET_KEY。 https://ai.baidu.com
这篇博客和 百度语音识别[python] 类似,要学会举一反三。

from aip import AipOcr

""" 读取密码 """
def getPassword(path="my_password.txt"):
    with open(path, "r", encoding="utf-8") as f:
        APP_ID = f.readline().strip()
        API_KEY = f.readline().strip()
        SECRET_KEY = f.readline().strip()
    return APP_ID, API_KEY, SECRET_KEY


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

""" 识别图片内容 """
def getContent(file_path="简单图片.jpg"):
    APP_ID, API_KEY, SECRET_KEY = getPassword()
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
    image = get_file_content(file_path)

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

if __name__ == "__main__":
    getContent()

例子为截图保存下来的: 来自我之前的博客 常见激活函数,及其优缺点 - 面试篇
在这里插入图片描述
代码运行结果:
在这里插入图片描述
再解析一下json格式数据即可。

    res = client.basicGeneral(image)
    words_result = res['words_result']
    result_list = []
    for item in words_result:
        result_list.append(item['words'])
    result_str = "".join(result_list)
    print(result_str)

在这里插入图片描述
这样的结果就是我们想要的了。还不错,完全正确!
【吐槽:这个比百度语音识别简单多了,效果还好】

图片URL+参数版

我们还可以通过图片的url链接,直接进行文字识别。同时,还可以加一些可选参数,比如设定识别文字的语言等。
代码如下:

'''从图片的url进行识别'''
def getTextFromPictureUrl(url):
    # url为图片的url链接
    # 并且带可选参数options
    APP_ID, API_KEY, SECRET_KEY = getPassword()
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

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

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

    """ 带参数调用通用文字识别, 图片参数为远程url图片 """
    res = client.basicGeneralUrl(url, options)
    words_result = res['words_result']
    result_list = []
    for item in words_result:
        result_list.append(item['words'])
    result_str = "".join(result_list)
    return result_str

其中,url为: https://i-blog.csdnimg.cn/blog_migrate/95047be873985322ad5cefe272903ba3.jpeg

效果如图:
在这里插入图片描述

手机拍照后文字识别

直接调用本地getContent()函数即可。
做一组对照实验,数据源是同一个,内容相同。
在这里插入图片描述
没想到效果这么好!
如图:
在这里插入图片描述
至此,结束,完美。

全部代码

from aip import AipOcr

""" 读取密码 """
def getPassword(path="my_password.txt"):
    with open(path, "r", encoding="utf-8") as f:
        APP_ID = f.readline().strip()
        API_KEY = f.readline().strip()
        SECRET_KEY = f.readline().strip()
    return APP_ID, API_KEY, SECRET_KEY


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

""" 识别图片内容 """
def getContent(file_path="简单图片.jpg"):
    APP_ID, API_KEY, SECRET_KEY = getPassword()
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
    image = get_file_content(file_path)

    """ 调用通用文字识别, 图片参数为本地图片 """
    res = client.basicGeneral(image)
    words_result = res['words_result']
    result_list = []
    for item in words_result:
        result_list.append(item['words'])
    result_str = "".join(result_list)
    return result_str

'''从图片的url进行识别'''
def getTextFromPictureUrl(url):
    # url为图片的url链接
    # 并且带可选参数options
    APP_ID, API_KEY, SECRET_KEY = getPassword()
    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

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

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

    """ 带参数调用通用文字识别, 图片参数为远程url图片 """
    res = client.basicGeneralUrl(url, options)
    words_result = res['words_result']
    result_list = []
    for item in words_result:
        result_list.append(item['words'])
    result_str = "".join(result_list)
    return result_str

if __name__ == "__main__":
    print("本地图片识别:")
    res1 = getContent(file_path="简单图片.jpg")
    print(res1)
    print("=="*30, "\n")

    url = "https://i-blog.csdnimg.cn/blog_migrate/95047be873985322ad5cefe272903ba3.jpeg"
    res2 = getTextFromPictureUrl(url)
    print("图片url带参数识别:")
    print(res2)
    print("=="*30, "\n")

    print("手机拍照后识别:")
    res3 = getContent(file_path="手机拍照.jpg")
    print(res3)


资料

api文档介绍
具备功能:通用文字识别、卡证文字识别、票据文字识别、教育场景文字识别、汽车场景文字识别、其它文字识别等。
对应sdk文档

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值