第一步,到百度智能雲申請接口資源
打開地址:https://cloud.baidu.com/?from=console,點擊產品下的通用場景文字識別
立即使用,跳轉頁領取免費資源(土豪可直接購買)
選擇全部,0元領取
領取后到應用列表創建應用,會生產三項數據
第二步,編寫腳本
首先要安裝百度api庫,pip install baidu-aip
代碼就直接貼了,沒有多少內容,主要先前創建應用獲得的appid、apikey 、Secret Key填到腳本里,再就是圖片跟腳本在一個目錄下
from aip import AipOcr APP_ID = '你的AppID' # 你的AppID API_KEY = '你的API Key' # 你的API Key SECRECT_KEY = '你的Secret Key' # 你的Secret Key client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('text.jpg')#圖片地址 """調用通用文字識別接口, 識別本地圖像""" result = client.basicGeneral(image) print(result)
執行以下,返回是一個json格式的內容
提取文字,或者保存到本地text里都行
for item in result['words_result']: print(item['words'])
至於對於圖片批量處理,可以把上面的代碼封裝成函數,然后讀取文件夾內圖片調用執行即可,批量處理的代碼如下
import os from aip import AipOcr APP_ID = 'xxxxx' # 你的AppID API_KEY = 'xxx' # 你的API Key SECRECT_KEY = 'xxx' # 你的Secret Key client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) def ocr(file): result = client.basicGeneral(file) for item in result['words_result']: print(item['words']) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() path = "C:\\Users\\\Beckham\\Desktop\\1202"#圖片存儲路徑 filenames = os.listdir(path) for file in filenames: if file.endswith('jpg'): print(file) image = get_file_content(str(file))# ocr(image) else: continue
百度智能雲還有其他好玩的應用,比如圖片識別、語音技術、人臉識別等等,后面有空再研究