目录

百度api

第一步:打开网站

https://ai.baidu.com/

第二步:注册登录

在这里插入图片描述
第三步:

登录成功,直接输入这个

https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/overview/index

不用一个一个点击进入这个页面,直接输入上面的地址,只要登录成功,就可以进入
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
以上准备好之后,直接上代码

import os
import time
import uuid

from aip import AipOcr

# 定义常量    换成你网站的
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

# 初始化AipFace对象

aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)

# 定义参数变量
options = {
    'detect_direction': 'true',
    'language_type': 'CHN_ENG',
}

filePath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'word')
filePath1 = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'text')
pictures = os.listdir(filePath)


class MetaPicture(object):

    def read_picture(self):



        for picture in pictures:

            picture_path = os.path.join(filePath, picture)
            # print(picture_path)
            # print(picture_path.split('\\')[-1].split('.')[0]  )
            def get_file_content(filePath):
                with open(filePath, 'rb') as fp:
                    return fp.read()

            time.sleep(1)
            # 调用通用文字识别接口
            result = aipOcr.basicGeneral(get_file_content(picture_path), options)
            print(result)
            if len(result) > 2   :

                words_result = result['words_result']

            word = ""
            for i in range(len(words_result)):
                word += words_result[i]['words']
                word += "\n"
            word += "\n\n\n"

            with open(filePath1+'\\'+str(picture_path.split('\\')[-1].split('.')[0])+'.txt', 'w') as text:
                text.write(word)



def main():
    metaPicture = MetaPicture()

    metaPicture.read_picture()

if __name__ == '__main__':
    main()

在这里插入图片描述

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐