调用百度图像识别api处理网络图片(文字识别)

要想使用百度图像识别api得先注册一个百度账号,然后创建一个图像识别应用,获取其中的APPID、AK、SK

账号注册什么的就不多说了,直接开始

创建应用

在这里插入图片描述

创建成功

在这里插入图片描述

代码

# -*- coding: utf-8 -*- 
# @File : img2num.py

from aip import AipOcr
import urllib.request
'''
使用百度api读取图片中的文字
'''

""" 你的 APPID AK SK """
APP_ID = 'APPID '
API_KEY = 'AK '
SECRET_KEY = 'SK'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

options = {}  # 配置字典
options["language_type"] = "CHN_ENG"  # 识别文字类型
options["detect_direction"] = "true"  # 是否检测图片的朝向
options["detect_language"] = "true"  # 是否检测语言
options["probability"] = "true"  # 是否返回置信度

def get_file_content(filePath): # 本地图片
    with open(filePath, 'rb') as fp:
        return fp.read()  # 获取图片信息
        
# 获取网络图片内容
def get_img_word(url):
    res = urllib.request.urlopen(url)
    result = client.basicGeneral(res.read(), options)
    if "words_result" not in result.keys(): # 当识别失败时返回图片地址
        print(result)
        print("无结果")
        return url
    word = ""
    for i in result["words_result"]:
        word = i['words']
    print(word)
    return word

def main():
    img_url = "网络图片地址"
    get_img_word(img_url)

if __name__ == '__main__':
    main()

图片

在这里插入图片描述
转换结果

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值