目錄
- 百度api
百度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()
複制