1、首先去百度注冊一個(gè)賬戶,然后選擇對應(yīng)的識別類型創(chuàng)建對應(yīng)的應(yīng)用,獲取AppID,APIKey,SecretKey,請參考百度官方接入文檔http://ai.baidu.com/docs#/Begin/top
2、官方使用文檔http://ai.baidu.com/docs#/OCR-Python-SDK/top
創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為近千家服務(wù),創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!
#-*- coding: UTF-8 -*-
#前提是python已安裝aip庫--》pip install baidu-aip
import os
from aip import AipOcr
import json
APP_ID = '你注冊賬號創(chuàng)建應(yīng)用后得到的APPID'
API_KEY = '你注冊賬號創(chuàng)建應(yīng)用后得到的API_KEY'
SECRET_KEY = '你注冊賬號創(chuàng)建應(yīng)用后得到的SECRET_KEY '
aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
os.chdir("E:\\office\\src_pic") #你需要轉(zhuǎn)換的圖片目錄
dirs = os.listdir()
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
print('開始處理,共'+str(len(dirs))+"張圖片。")
flag=0
T = 0 #統(tǒng)計(jì)處理圖片成功的數(shù)量
for filePath in dirs:
if filePath.split('.')[-1]=='txt':continue
flag+=1
print('正在處理第'+str(flag)+'張圖片')
try:
result = aipOcr.basicGeneral(get_file_content(filePath), options)
except BaseException as e:
print(e)
else:
try:
with open(filePath.split('.')[0]+'.txt','w',encoding='utf-8') as f:
for i in result['words_result']:
f.write(i['words']+'\n')
T += 1
except BaseException as e :
print(e)
else:
print('處理完成')
print('{}全部處理完成!{}'.format("="*30,"="*30))
print('處理成功的圖片有{}張,處理失敗的圖片有{}張'.format(T,len(dirs)-T))