python实现批量图片文字识别(ocr)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                python实现批量图片文字识别(ocr)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                最近有ocr方面的需求,而且是批量的,python能不能干這么件事呢,肯定是可以的,基于百度智能云和python結合,實現了我們的需求,今天分享出來,做個備份
import glob from os import path import os from aip import AipOcr from PIL import Image'''使用時只需要修改百度智能云接口,位于baiduOCR函數,和圖片存放位置,位于倒數第三行'''def baiduOCR(outfile):"""利用百度api識別文本,并保存提取的文字picfile: 圖片文件名outfile: 輸出文件"""filename = path.basename(picfile)APP_ID = '你的APP_ID'API_KEY = '你的API_KEY'SECRET_KEY = '你的SECRET_KEY'client = AipOcr(APP_ID, API_KEY,SECRET_KEY)i = open(picfile, 'rb')img = i.read()print("正在識別圖片:\t" + filename)message = client.basicGeneral(img) # 通用文字識別,每天 50 000 次免費# message = client.basicAccurate(img) # 通用文字高精度識別,每天 500 次免費print("識別成功!")i.close()with open(outfile, 'a+',encoding='utf-8') as fo:fo.writelines("+" * 60 + '\n')fo.writelines("識別圖片:\t" + filename + "\n" * 2)fo.writelines("文本內容:\n")# 輸出文本內容for text in message.get('words_result'):fo.writelines(text.get('words') + '\n')fo.writelines('\n' * 2)print("文本導出成功!")print()if __name__ == "__main__":open('result.txt', 'a+',encoding='utf-8').close()outfile = 'result.txt'for picfile in glob.glob("C:\\Users\\25801路西\\Desktop\\test_image\\*"):baiduOCR(outfile)print('圖片文本提取結束!文本輸出結果位于 %s 文件中。' % outfile)使用時只需要修改百度智能云接口,位于baiduOCR函數,和圖片存放位置,位于倒數第三行
獲取百度智能云接口的教程有很多,我就不做贅述了,識別效果還是很不錯的哦
效果展示
python實現批量(上萬張)圖片文字識別并寫入excel表格
總結
以上是生活随笔為你收集整理的python实现批量图片文字识别(ocr)的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 项目三:公寓管理系统_ssm
 - 下一篇: 2015.7.26-2015.7.31意