目录
- 百度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()
复制