資源簡(jiǎn)介
9b8dee39408392b66c4a776e4e7e05ce.py
代碼片段和文件信息
from?aip?import?AipImageClassify
from?PIL?import?ImageImageDrawImageFont
import?os
import?shutil
import?os.path?as?op
import?time
“““?你的?APPID?AK?SK??“““
APP_ID?=?‘*******‘
API_KEY?=?‘*******‘
SECRET_KEY?=?‘*******‘
client?=?AipImageClassify(APP_ID?API_KEY?SECRET_KEY)
“““?顯示所有圖片?“““
sywj=os.listdir(os.getcwd())????#?所有文件來(lái)自于當(dāng)前文檔
print(‘當(dāng)前文件夾下共有‘len(sywj)‘個(gè)文件:‘sywj)
ad=input(‘輸入文件路徑:‘)
path=ad
filenames?=?os.listdir(path)
print(‘\n圖片文件包括:‘)
for?filename?in?filenames:
????print(filename)
“““?讀取圖片?“““
#?while?True:
def?get_file_content(filePath):
????with?open(filePath?‘rb‘)?as?fp:
????????return?fp.read()
for?filename?in?filenames:
????image?=?get_file_content(path+‘\\‘+filename)#?打開(kāi)要識(shí)別的圖片
????“““?調(diào)用通用物體識(shí)別?“““
????client.advancedGeneral(image);
????“““?如果有可選參數(shù)?“““
????options?=?{}
????options[“baike_num“]?=?5
????“““?帶參數(shù)調(diào)用通用物體識(shí)別?“““
????d=client.advancedGeneral(image?options)
????#?print(d)
????“““?檢查結(jié)果?“““
????dsg=d[‘result_num‘]#?幾個(gè)識(shí)別結(jié)果
????#?print(dsg)
????sc=‘\n識(shí)別結(jié)果‘
????print(sc)
????for?cf?in?range(dsg):
????????identify=d[‘result‘][cf][‘keyword‘]
????????probabilly=d[‘result‘][cf][‘score‘]
????????print(identifyprobabilly)
????????i=d[‘result‘][0][‘keyword‘]
????????img=Image.open(path+‘\\‘+filename)
????????img.save(i+‘.jpg‘)
????print(‘圖片已保存‘)
while?True:
????o=‘yes‘
????p=‘no‘
????z=input(‘是否要新建文件夾:‘)
????#?print(z)
????if?z?==?o:
????????while?True:
????????????xdwjj=input(‘\n請(qǐng)輸入要新建的文件名:‘)
????????????if?not?os.?path.exists(xdwjj):
????????????????os.mkdir(xdwjj)
????????????????print(‘文件夾‘xdwjj‘已新建‘)
????????????????break
????????????else:
????????????????print(‘文件(夾)‘xdwjj‘已存在,另?yè)Q一個(gè)‘)
????????#獲取批量文件處理的總路徑
????
評(píng)論
共有 條評(píng)論