呼叫百度OCR模組進行文字識別
阿新 • • 發佈:2020-11-30
轉自:https://www.cnblogs.com/students/p/10826822.html
和https://mp.weixin.qq.com/s/RSSOJBm4KsU4EwX6J6Nt7w(這是參考的程式碼2)
1.登入百度雲平臺,建立應用
2.編寫程式碼
1 from aip import AipOcr 2 import codecs 3 import os 4 #讀取圖片函式 5 def ocr(path): 6 with open(path,'rb') as f: 7 return f.read() 8 def main(): 9 filename = "c.jpg" 10 print("已經收到,正在處理,請稍後....") 11 app_id = '16193547' 12 api_key = 'B0R5gbezdGSzCY4oIlOpuLy8' 13 secret_key = 'CyevG1PTfpPvkw9vwItPdya09GrzZ462' 14 client = AipOcr(app_id,api_key,secret_key) 15 #讀取圖片 16 image = ocr(filename) 17 #程序OCR識別 18 dict1 = client.general(image)19 # print(dict1) 20 with codecs.open(filename + ".txt","w","utf-8") as f: 21 for i in dict1["words_result"]: 22 f.write(str(i["words"] + "\r\n")) 23 print ("處理完成") 24 if __name__ == '__main__': 25 main()
效果圖:
3程式碼2
首先,說個大前提,我這種方法是用來識別圖片上的文字的。也就是說,你想把圖片上的文字扒下來,用我的方法肯定沒錯!
1 # 第一步:導包 2 from aip import Aipocr as ocr 3 # 第二步:讀取 4 with open(path,'rb') as f: 5 img = f.read() 6 # 第三步:呼叫 7 cli = ocr(appId, apiKey, secretKey) 8 # 第四步:識別 9 rlt = cli.general(img) 10 # 第五步:輸出 11 for line in rlt['words_result']: 12 print(line.get('words'))