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