900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python实现批量识别图片文字 生成对应的txt文件

python实现批量识别图片文字 生成对应的txt文件

时间:2023-06-14 08:00:31

相关推荐

python实现批量识别图片文字 生成对应的txt文件

目录

百度api

百度api

第一步:打开网站

/

第二步:注册登录

第三步:

登录成功,直接输入这个

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

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

以上准备好之后,直接上代码

import osimport timeimport uuidfrom 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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。