轉自:https://www.cnblogs.com/students/p/10826822.html(代碼可實現,但是識別效率有點低)
和https://mp.weixin.qq.com/s/RSSOJBm4KsU4EwX6J6Nt7w(這是參考的代碼2,代碼有問題)
1.登錄百度雲平台,創建應用
2.編寫代碼
1 from aip import AipOcr#要安裝baidu-aip包,不是aip包 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'))