百度智能云手写文字识别接口调用过程

创建应用:找到应用列表

百度智能云-登录

复制API key与Secret Key 

把get_token.py中的client_id=改成自己的api key&client_secret=改成自己的secret key

get_token.py:获取百度智能云的接口的token

#模块1:获取百度智能云的接口的token
#get_token.py
#输出token
import requests
import json


def main():
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=改成自己的api key&client_secret=改成自己的secret key"

    payload = ""
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    response = requests.request("POST", url, headers=headers, data=payload)

    print(response.text)


if __name__ == '__main__':
    main()

复制的token

修改predict.py中的tocken

然后运行predict.py:调用接口进行手写汉字识别:


# encoding:utf-8
import requests
import base64
import json
import numpy as np
from PIL import Image
import cv2
'''
手写文字识别
模块2:predict.py   获取接口返回的汉字信息(字数、行数、位置信息)
获取
输入:图片地址
输出:汉字信息
2.1字数
2.2行数
2.3每一行的汉字内容
2.4每一行的汉字位置信息
2.5按行分割保存的的图片
'''

request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
# 二进制方式打开图片文件
file_path=r"C:\Users\czjie\Desktop\label\0\0a8f405386964870e88e118dfa9accd.png"
save_path=r"C:\Users\czjie\Desktop\Images"
#=r"C:\Users\czjie\Desktop\f78d1c499a888b6e445fa658f7af637.jpg"
f = open(file_path, 'rb')
img = base64.b64encode(f.read())

image = Image.open(file_path)
output_image_array1 = np.array(image)

params = {"image":img}
#access_token = '[调用鉴权接口获取的token]'
access_token = '[复制的token粘贴]'
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print (response.json())
    json_str=response.json()
    #<class 'dict'>  字典类型
    #print("返回值的类别",type(json_str))
    """json_str
    {'words_result': [{'location': {'top': 127, 'left': 37, 'width': 299, 'height': 56}, 'words': '安得广厦千万间'},
                      {'location': {'top': 194, 'left': 29, 'width': 435, 'height': 62}, 'words': '大庇天下寒士俱欢颜'},
                      {'location': {'top': 265, 'left': 30, 'width': 320, 'height': 55}, 'words': '风雨不动安如山'}],
     'words_result_num': 3, 
     'log_id': 1655900090402811384}
    """
    #列表json_str["words_result"]
    num=0
    #按行保存
    for i in json_str["words_result"]:
        #处理行数递增
        num += 1
        #i 字典  例子:{'location': {'top': 127, 'left': 37, 'width': 299, 'height': 56}, 'words': '安得广厦千万间'}
        #i["words"]                     例子:'安得广厦千万间'
        #len(i['words'])  一行的字数
        #i['location']  一行位置信息      例子: {'top': 127, 'left': 37, 'width': 299, 'height': 56}
        top=i['location']['top']
        left=i['location']['left']
        width=i['location']['width']
        height=i['location']['height']

        new_image_qq = output_image_array1[top:top + height + 2, left:left + width + 2]
        new_save_path=save_path+"\\"+file_path.split("\\")[-1].split(".")[0]+f"_{num}.jpg"
        cv2.imwrite(new_save_path, new_image_qq)
        #print(f"此行一共{len(i['words'])}个字")
        print(i["words"])

    print(f"一共{json_str['words_result_num']}行汉字")

返回结果例子:

{
    "log_id": 620759800,
    "words_result": [
        {
            "location": {
                "left": 56,
                "top": 0,
                "width": 21,
                "height": 210
            },
            "words": "3"
        }
    ],
    "words_result_num": 1
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值