创建应用:找到应用列表
复制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
}