-
Notifications
You must be signed in to change notification settings - Fork 2
/
remove_duplicates.py
159 lines (135 loc) · 4.72 KB
/
remove_duplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from skimage.metrics import structural_similarity as ssim
from collections import defaultdict
from tqdm import tqdm
import numpy as np
import cv2
import sys
import os
def merge_common(lists):
neigh = defaultdict(set)
visited = set()
for each in lists:
for item in each:
neigh[item].update(each)
def comp(node, neigh=neigh, visited=visited, vis=visited.add):
nodes = set([node])
next_node = nodes.pop
while nodes:
node = next_node()
vis(node)
nodes |= neigh[node] - visited
yield node
for node in neigh:
if node not in visited:
yield sorted(comp(node))
def keep_widest_img(data):
widest_file = max(data, key=lambda i: i[0])
for item in data:
if item[1] == widest_file[1]:
continue
else:
os.remove(item[1])
def keep_highest_name(data):
num_filenames = [(item[1].split('/')[-1].split(',')[0], item[1])
for item in data]
highest_filename = max(num_filenames, key=lambda i: int(i[0]))
widest_file = max(data, key=lambda i: int(i[0]))
img = cv2.imread(widest_file[1])
for item in data:
os.remove(item[1])
cv2.imwrite(highest_filename[1], img)
def mse(first_img, second_img):
err = np.sum((first_img.astype("float") - second_img.astype("float")) ** 2)
err /= float(first_img.shape[0] * first_img.shape[1])
return err
def dhash(image, hashSize=8):
resized_img = cv2.resize(image, (hashSize + 1, hashSize))
diff = resized_img[:, 1:] > resized_img[:, :-1]
return sum([2 ** i for (i, v) in enumerate(diff.flatten()) if v])
def dhash_path(path, hashSize=8):
if path.endswith(".webm") or path.endswith(".mp4") or path.endswith(".gif"):
return None
image = cv2.imread(path, 0)
if image is None:
return None
return dhash(image, hashSize=hashSize)
def remove_duplicates(directory, custom=True):
image_data = {}
pic_hashes = {}
print("Calculating dhashes")
for rel_path in tqdm(os.listdir(directory)):
path = directory + "/" + rel_path
img = cv2.imread(path, 0)
if img is None:
continue
image_data[path] = [img, cv2.resize(
img, (8, 8), interpolation=cv2.INTER_AREA), img.shape[1]]
image_hash = dhash(img)
if image_hash is None:
continue
elif image_hash in pic_hashes:
pic_hashes[image_hash].append(path)
else:
pic_hashes[image_hash] = [path]
dupe_list = []
for key in pic_hashes.keys():
if len(pic_hashes[key]) > 1:
dupe_list.append(pic_hashes[key])
if len(dupe_list) == 0:
print("No Duplicates Found via Dhash")
else:
count = 0
for i in dupe_list:
for j in i:
count += 1
count -= len(dupe_list)
print("Deleting " + str(count) + " dupes found via Dhash")
for dupes in tqdm(dupe_list):
data = [(image_data[path][2], path) for path in dupes]
if custom:
keep_highest_name(data)
else:
keep_widest_img(data)
paths = [directory + "/" + item for item in os.listdir(directory)]
for key in image_data:
if key not in paths:
image_data[key] = None
dupe_list = []
print("Calculating MSE and SSIM")
for data in tqdm(image_data):
mse_ssim = [(key, mse(image_data[data][1], image_data[key][1]), ssim(image_data[data][1], image_data[key][1]))
for key in image_data if data is not key and image_data[data] is not None and image_data[key] is not None]
dupe = [item[0] for item in mse_ssim if item[2] > 0.95 and item[1] < 20]
if dupe != []:
dupe.insert(0, data)
dupe_list.append(dupe)
dupe_list = list(merge_common(dupe_list))
if len(dupe_list) == 0:
print("No Duplicates Found")
sys.exit()
else:
count = 0
for i in dupe_list:
for j in i:
count += 1
count -= len(dupe_list)
print("Deleting " + str(count) + " dupes found via SSIM and MSE")
for dupes in tqdm(dupe_list):
data = [(image_data[path][2], path) for path in dupes]
if custom:
keep_highest_name(data)
else:
keep_widest_img(data)
if __name__ == '__main__':
args = sys.argv[1:]
directory = ""
if len(args) == 0:
from tkinter.filedialog import askdirectory
from tkinter import Tk
Tk().withdraw()
directory = askdirectory()
else:
directory = os.path.abspath(args[0])
remove_duplicates(directory, False)