当前位置:   article > 正文

百度(表格OCR异步接口)API调用流程_百度智能云表格文字识别异步接口怎么用

百度智能云表格文字识别异步接口怎么用

目录

1.调用费用:

 2.调用流程

1)注册百度账号并进行个人/企业认证

2)领取免费资源流程

2)1)百度智能云--控制台--产品服务--文字识别

2)2)领取免费资源

 2)3)勾选“通用场景OCR”,0元领取 

 3)获取APP_ID、API_KEY、Secret_Key

3)1)公有云服务--应用列表--创建应用

3)2)应用名称-(接口默认勾选,不需要额外操作)-应用描述--立即创建 

 3)3)成功获得APP_ID、API_KEY、Secret_Key

 4)运行代码(代码根据生成结果形式分为两类excel、json)

4)1)生成结果以excel形式保存

4)2)生成结果以json形式保存

3.json文件主要信息(举例信息以字典形式给出)


1.调用费用:

在控制台-免费资源页面,可领取所需接口的免费测试资源。个人认证 500 次/月,企业认证 1,000 次/月。

链接:百度智能云-管理中心 (baidu.com)

超出部分价格:0~5万次-->0.3元/次;5~10万次-->0.26元/次(其他详见表格所示价格)

 2.调用流程

1)注册百度账号并进行个人/企业认证

百度云链接:百度智能云-智能时代基础设施 (baidu.com)

2)领取免费资源流程

2)1)百度智能云--控制台--产品服务--文字识别

2)2)领取免费资源

 2)3)勾选“通用场景OCR”,0元领取 

 3)获取APP_ID、API_KEY、Secret_Key

同2)1)进入控制台界面

3)1)公有云服务--应用列表--创建应用

3)2)应用名称-(接口默认勾选,不需要额外操作)-应用描述--立即创建 

 3)3)成功获得APP_ID、API_KEY、Secret_Key

 4)运行代码(代码根据生成结果形式分为两类excel、json)

代码修改初始信息:APP_ID、API_KEY、Secret_Key、img_path、save_path

4)1)生成结果以excel形式保存

  1. # encoding: utf-8
  2. import os
  3. import sys
  4. import requests
  5. import time
  6. import tkinter as tk
  7. from tkinter import filedialog
  8. from aip import AipOcr
  9. import time
  10. """定义常量"""
  11. APP_ID = ''
  12. API_KEY = ''
  13. SECRET_KEY = ''
  14. """初始化AipFace对象"""
  15. client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
  16. image_path = 'D:\\table_generation-master\\demo\\table_recog_test'
  17. save_path = 'D:\\table_generation-master\\demo\\table_recog_test_output'
  18. """读取图片"""
  19. def get_file_content(filePath):
  20. with open(filePath, 'rb') as fp:
  21. return fp.read()
  22. """文件下载函数"""
  23. def file_download(url, file_path):
  24. r = requests.get(url)
  25. with open(file_path, 'wb') as f:
  26. f.write(r.content)
  27. if __name__ == "__main__":
  28. root = tk.Tk()
  29. root.withdraw()
  30. # data_dir = filedialog.askdirectory(title='请选择图片文件夹') + '/'
  31. # result_dir = filedialog.askdirectory(title='请选择输出文件夹') + '/'
  32. data_dir = image_path
  33. result_dir = save_path
  34. num = 0
  35. for name in os.listdir(data_dir):
  36. print ('{0} : {1} 正在处理:'.format(num+1, name.split('.')[0]))
  37. image = get_file_content(os.path.join(data_dir, name))
  38. res = client.tableRecognitionAsync(image, )
  39. print ("res:", res)
  40. if 'error_code' in res.keys():
  41. print ('Error! error_code: ', res['error_code'])
  42. sys.exit()
  43. req_id = res['result'][0]['request_id'] #获取识别ID号
  44. print("ID:", req_id)
  45. for count in range(1, 20): #OCR识别也需要一定时间,设定10秒内每隔1秒查询一次
  46. res = client.getTableRecognitionResult(req_id) #通过ID获取表格文件XLS地址
  47. print("xls_path{}".format(res))
  48. print(res['result']['ret_msg'])
  49. if res['result']['ret_msg'] == '已完成':
  50. break #云端处理完毕,成功获取表格文件下载地址,跳出循环
  51. else:
  52. time.sleep(1)
  53. url = res['result']['result_data']
  54. xls_name = name.split('.')[0] + '.xls'
  55. file_download(url, os.path.join(result_dir, xls_name))
  56. num += 1
  57. print ('{0} : {1} 下载完成。'.format(num, xls_name))
  58. time.sleep(1)

4)2)生成结果以json形式保存

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import copy
  4. import json
  5. from baidubce import bce_base_client
  6. from baidubce.auth import bce_credentials
  7. from baidubce.auth import bce_v1_signer
  8. from baidubce.http import bce_http_client
  9. from baidubce.http import handler
  10. from baidubce.http import http_methods
  11. from baidubce import bce_client_configuration
  12. import requests
  13. import base64
  14. import time
  15. # from try6 import webimage
  16. from urllib.parse import urlencode
  17. API_KEY = ''
  18. SECRET_KEY = ''
  19. img_path = "D:\\table_generation-master\\demo\\table_recog_test\\0M208145r02110zw_1.png"
  20. save_path = "D:\\table_generation-master\\demo\\baidu_result\\jit_baidu.json"
  21. def change_img_to_base64(image_path):
  22. """base64编码图片-->url"""
  23. with open(image_path, 'rb') as f:
  24. image_data = f.read()
  25. base64_data: bytes = base64.b64encode(image_data) # base64编码
  26. return base64_data
  27. # def change_base64_to_url(base64_data):
  28. def get_request_di():
  29. # client_id 为官网获取的AK, client_secret 为官网获取的SK
  30. host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(API_KEY,SECRET_KEY)
  31. response = requests.get(host)
  32. if response:
  33. access_token_get = response.json()["access_token"]
  34. request_url = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
  35. # 二进制方式打开图片文件
  36. f = open(img_path, 'rb')
  37. img_base64 = base64.b64encode(f.read())
  38. # img = webimage(img_base64, access_token_get)
  39. params = {"image":img_base64}
  40. request_url = request_url + "?access_token=" + access_token_get
  41. headers = {'content-type': 'application/x-www-form-urlencoded'}
  42. response = requests.post(request_url, data=params, headers=headers, json=True)
  43. if response:
  44. request_id = response.json()["result"][0]["request_id"]
  45. print(response.json())
  46. print(request_id)
  47. return request_id, access_token_get
  48. class ApiCenterClient(bce_base_client.BceBaseClient):
  49. def __init__(self, config=None):
  50. self.service_id = 'apiexplorer'
  51. self.region_supported = True
  52. self.config = copy.deepcopy(bce_client_configuration.DEFAULT_CONFIG)
  53. if config is not None:
  54. self.config.merge_non_none_values(config)
  55. def _merge_config(self, config=None):
  56. if config is None:
  57. return self.config
  58. else:
  59. new_config = copy.copy(self.config)
  60. new_config.merge_non_none_values(config)
  61. return new_config
  62. def _send_request(self, http_method, path,
  63. body=None, headers=None, params=None,
  64. config=None, body_parser=None):
  65. config = self._merge_config(config)
  66. if body_parser is None:
  67. body_parser = handler.parse_json
  68. return bce_http_client.send_request(
  69. config, bce_v1_signer.sign, [handler.parse_error, body_parser],
  70. http_method, path, body, headers, params)
  71. def demo(self,request_id, access_tok):
  72. path = b'/rest/2.0/solution/v1/form_ocr/get_request_result'
  73. headers = {}
  74. headers[b'Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  75. params = {}
  76. params['access_token'] = access_tok
  77. body = 'request_id={}&result_type=json'.format(request_id)
  78. return self._send_request(http_methods.POST, path, body, headers, params)
  79. if __name__ == '__main__':
  80. endpoint = 'https://aip.baidubce.com'
  81. ak = API_KEY
  82. sk = SECRET_KEY
  83. config = bce_client_configuration.BceClientConfiguration(credentials=bce_credentials.BceCredentials(ak, sk),
  84. endpoint=endpoint)
  85. client = ApiCenterClient(config)
  86. request_id, access_tok = get_request_di()
  87. print("access_token", access_tok)
  88. print("request_id", request_id)
  89. for count in range(1, 20):
  90. res_ = client.demo(request_id, access_tok)
  91. print(res_.__dict__['raw_data'])
  92. print(type(res_))
  93. res = json.loads(res_.__dict__["raw_data"])
  94. if res['result']['ret_msg'] == '已完成':
  95. break # 云端处理完毕,成功获取表格文件下载地址,跳出循环
  96. else:
  97. time.sleep(1)
  98. dict_json = json.dumps(res_, ensure_ascii=False)
  99. with open(save_path, 'w', encoding='utf-8') as fp:
  100. fp.write(dict_json+'\r\n')

3.json文件主要信息(举例信息以字典形式给出)

其他信息详见文字识别OCR (baidu.com)

  1. form_num:表格数量
  2. forms:表格内容信息
  3. footer:表尾信息
  4. header:表头数据信息
  5. body:表格主体部分数据
  6. rect:(top,left)-->左上角坐标;(width,height)-->单元格宽/高
  7. column:(初值为1)<只显示合并单元格的首位和末尾>
  8. row:(初值为1)
  9. word:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/705287
推荐阅读
相关标签
  

闽ICP备14008679号