python调用百度ai将图片识别为表格excel

python调用百度ai将图片识别为表格excel

ocr

百度ai官方文档:https://ai.baidu.com/ai-doc/OCR/Ik3h7y238

python 复制代码
import requests
import json
import base64
import time
'''
文档:https://ai.baidu.com/ai-doc/OCR/Ik3h7y238
'''

# 获取access_token
def get_access_token():
    client_id = "xxxxxxxxxxxxxxxxxx"
    client_secret = "xxxxxxxxxxxxxxxxxxxxxx"
    # client_id = '你的apikey'
    # client_secret = '你的Secret Key'
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(
        client_id, client_secret)
    response = requests.get(host).text
    data = json.loads(response)
    access_token = data['access_token']
    return access_token


# 获取返回信息

# 获取识别结果
def get_info(access_token):
    request_url = "https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/request"
    # 二进制方式打开图片文件
    f = open('1.jpg', 'rb')
    img = base64.b64encode(f.read())  # base64编码
    params = {"image": img}
    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())
    data_1 = response.json()
    return data_1


# 获取excel


def get_excel(requests_id, access_token):
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    pargams = {
        'request_id': requests_id,
        'result_type': 'excel'
    }
    url = 'https://aip.baidubce.com/rest/2.0/solution/v1/form_ocr/get_request_result'
    url_all = url + "?access_token=" + access_token
    res = requests.post(url_all, headers=headers, params=pargams)  # 访问链接获取excel下载页
    info_1 = res.json()['result']['ret_msg']
    excel_url = res.json()['result']['result_data']
    excel_1 = requests.get(excel_url).content
    print(excel_1)
    with open('识别结果.xls', 'wb+') as f:
        f.write(excel_1)
    print(info_1)


def main():
    print('正在处理中请稍后')
    access_token = get_access_token()
    data_1 = get_info(access_token)
    try:
        requests_id = data_1['result'][0]['request_id']
        if requests_id != '':
            print('识别完成')
    except:
        print('识别错误')
    print('正在获取excel')
    time.sleep(10)  # 延时十秒让网页图片转excel完毕,excel量多的话,转化会慢,可以延时长一点
    get_excel(requests_id, access_token)


main()
相关推荐
Shawn_Shawn18 分钟前
人工智能入门概念介绍
人工智能
极限实验室18 分钟前
程序员爆哭!我们让 COCO AI 接管 GitLab 审查后,团队直接起飞:连 CTO 都说“这玩意儿比人靠谱多了
人工智能·gitlab
Maynor9962 小时前
Z-Image: 100% Free AI Image Generator
人工智能
码界奇点2 小时前
Python从0到100一站式学习路线图与实战指南
开发语言·python·学习·青少年编程·贴图
爬点儿啥2 小时前
[Ai Agent] 10 MCP基础:快速编写你自己的MCP服务器(Server)
人工智能·ai·langchain·agent·transport·mcp
张人玉2 小时前
百度 AI 图像识别 WinForms 应用代码分析笔记
人工智能·笔记·百度
测试人社区-小明2 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
Spring AI学习3 小时前
Spring AI深度解析(9/50):可观测性与监控体系实战
java·人工智能·spring
Laravel技术社区3 小时前
pytesseract 中英文 识别图片文字
python
罗西的思考3 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法