‘’’
Traverse the entire folder , See all the pictures , Use matchTemplate() Methods to judge the similarity of two pictures
95% The above , Delete similar pictures
‘’’
import os
import cv2
import sys
# Traverse the file under the target path , take jpeg Insert files into the list
def file_list(catalogue):
photos = []
photos_route = []
# Traverse the upper directory
for root, dirs, files in os.walk(catalogue):
# Check how many subdirectories exist
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’:
# Get the picture content and path
photos,photos_route = file_list(‘./image/face_catalogue’)
#print(len(photos))
compare_list = file_compare(photos,photos_route)
#print(‘compare_list’,compare_list)
# Proposed with high similarity
tmp_list =[]
for compare in compare_list:
tmp_list.append(compare['image2'])
# Delete
for tmp in set(tmp_list):
print(' Delete :',tmp)
os.remove(tmp)