恰巧用到了OCR批量識別,鑒於准確度沒有使用在本地訓練的TensorFlow-OCR,而是選擇了百度OCR,可選的方式多種多樣,比如Google文字識別,騰訊OCR等等,不一一列舉
很簡單的demo,參照開發文檔
http://ai.baidu.com/docs#/OCR-Python-SDK/80d64770
先去控制台注冊一個開發者賬號,並創建一個文字識別應用,在管理應用中可以看到AppID等相關信息
安裝SDK pip install baidu-aip
新建一個python文件
from aip import AipOcr
from glob import glob
from docx import Document
import os
import json
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
root_path = os.getcwd() #獲取當前路徑
document = Document() #初始化word文檔
num = 0
""" 讀取圖片 """
def get_file_content(FilePath):
with open(FilePath, 'rb') as fp:
return fp.read()
""" 調用通用文字識別, 圖片參數為本地圖片 """
def result(image_file,image):
""" 如果有可選參數 """
options = {}
#options["language_type"] = "CHN_ENG"#
options["detect_direction"] = "true"
#options["detect_language"] = "true"#
options["probability"] = "true"
#m = client.basicGeneral(image);
"""帶參數調用通用文字識別, 圖片參數為本地圖片 """
#client.basicGeneral(image,options)
#url = "https//www.x.com/sample.jpg"
#調用通用文字識別, 圖片參數為遠程url圖片
#client.basicGeneralUrl(url);
#普通
#res = client.basicGeneral(image,options)
#高精度
res = client.basicAccurate(image,options)
global num
num = num + 1
print("這是第"+str(num)+"個文件")
print(res)
if 'error_code' in res:
print("有錯誤")
File("出錯了,第"+image_file.split("/")[-1]+'個','/error')
else:
if(num>0):
document.add_page_break()
fname = image_file.split("/")[-1]
document.add_heading(fname,level=2) #添加二級標題
os.remove(image_file)
for item in res['words_result']:
print(item['words'])
File(item['words'],image_file)
write_word(item['words'])
'''文件寫入'''
def File(string,name):
with open(root_path+'/demo/text/'+name.split("/")[-1]+'.txt','a+') as f:
f.write('\n'+string)
def write_word(string):
document.add_paragraph(string)
document.save(root_path+'/demo/text/'+'test.docx')
"""指定目錄下所有文件路徑"""
image_files = glob(root_path+'/demo/image/*.*')
#image_files = glob('./image/*.*')
for image_file in sorted(image_files):
print(image_file)
image = get_file_content(image_file)
result(image_file,image)
支持導出到word文檔,可以在第51,52行中添加/修改將結果寫入的文件,7,8,9改成自己控制台的內容才可以使用,相應目錄都寫在文件中,只需稍微修改
自動讀取image文件夾下的所有文件,識別后將結果保存在text目錄下
目錄樹
.
├── BaiduOcr.py
├── error.txt
├── image
│ └── 028.jpg
└── text
