百度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 = ''
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)
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()
|