1. 程式人生 > >菜鳥用Python 識別批量圖片,再讀取文字(用百度文字識別)

菜鳥用Python 識別批量圖片,再讀取文字(用百度文字識別)

直接上程式碼吧,本人菜鳥剛學的,有問題指教

MyPath = "G:\\python\\xxxxx\\"#這是讀取的圖片存放的資料夾的路徑,可以改為要讀取的資料夾
filesoure = MyPath
def baiduduqu(filesoure,filename):
	from aip import AipOcr
	import re
	import os
	APP_ID = 'xxxx'
	API_KEY = 'xxxxx'
	SECRET_KEY = 'xxxx'
	client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
	dakai = open(filename,'rb')
	duqu = dakai.read()
	message = client.basicGeneral(duqu)
	for duqu in message.get('words_result'):
		print(duqu.get('words'))

def filename (fielsoure,filetype):
	import os
	pathDir = os.listdir(filesoure)
	for allDir in pathDir:
		child = os.path.join('%s%s' % (filesoure,allDir))
		print(child)
		baiduduqu(filesoure,child)
def run():
	import os
	os.chdir(filesoure)
	for i in os.listdir(os.getcwd()):
		postfix = os.path.splitext(i)[1]
		if postfix == '.jpeg' or postfix == '.png':
			filename(filesoure,postfix)

if __name__ == '__main__':
   run()

實現以上程式碼需要安裝

AipOcr

如果已安裝pip,執行pip install baidu-aip即可,參考http://yuyin.baidu.com/docs/tts/196