‘’’
遍歷整個文件夾,查看所有圖片,使用 matchTemplate()方法判斷兩個圖片相似度
95%以上的,將相似圖片刪除
‘’’
import os
import cv2
import sys
#遍歷目標路徑下文件,將jpeg文件插入列表內
def file_list(catalogue):
photos = []
photos_route = []
# 遍歷上層目錄
for root, dirs, files in os.walk(catalogue):
# 查看存在多少個子目錄
if files == []:
#print(‘root’, root, ‘dirs’, dirs, ‘files’, files)
for dir in dirs:
for root_son, dirs_son, files_son in os.walk(catalogue + “/%s” % dir):
#print(‘dir’, dir, ‘files’, files_son)
for file_son in files_son:
file_text = os.path.splitext(file_son)
#print(‘file_text’,file_text)
if file_text[1].upper() == ‘.JPEG’:
#print(‘file’, catalogue + “/%s/%s” % (dir, file_son))
img = cv2.imread(catalogue + “/%s/%s” % (dir, file_son), 0)
imga = cv2.resize(img, (400, 400))
photos.append(imga)
photos_route.append(catalogue + “/%s/%s” % (dir, file_son))
return photos,photos_route
def file_compare(photos,photos_route):
print(len(photos),len(photos_route),photos_route[0])
count_num = len(photos)
compare_list = []
for i in range(count_num):
for j in range(0+i+1,count_num):
temp_amg = photos[i]
results =cv2.matchTemplate(photos[i],photos[j],cv2.TM_CCOEFF_NORMED)
if results > 0.95:
#print(photos_route[i],photos_route[j],str(results))
dictionary ={'image1':photos_route[i],'image2':photos_route[j],'compare':results}
compare_list.append(dictionary)
return compare_list
if name == ‘main’:
#獲得圖片內容與路徑
photos,photos_route = file_list(‘./image/face_catalogue’)
#print(len(photos))
compare_list = file_compare(photos,photos_route)
#print(‘compare_list’,compare_list)
#相似度較高提出
tmp_list =[]
for compare in compare_list:
tmp_list.append(compare['image2'])
#刪除
for tmp in set(tmp_list):
print('刪除:',tmp)
os.remove(tmp)