当前位置:   article > 正文

python实现批量识别图片文字,生成对应的txt文件_python百度ai识别的文字怎么导出文件

python百度ai识别的文字怎么导出文件

目录

百度api

第一步:打开网站

https://ai.baidu.com/
  • 1

第二步:注册登录

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

登录成功,直接输入这个

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

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

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

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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

在这里插入图片描述

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

闽ICP备14008679号