-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindupe.py
159 lines (125 loc) · 4.69 KB
/
findupe.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
from turtle import st
import cv2
import numpy as np
import os
import shutil
import argparse
from skimage.metrics import structural_similarity as ssim
class TrieNode():
def __init__(self):
self.children = {}
self.eow = False
self.ind = []
class Trie():
def __init__(self):
self.root = TrieNode()
def Insert(self, s, i):
ptr = self.root
for c in s:
if c not in ptr.children:
ptr.children[c] = TrieNode()
ptr = ptr.children[c]
ptr.ind.append(i)
ptr.eow = True
def Search(self, s):
ptr = self.root
for c in s:
if c not in ptr.children:
return []
ptr = ptr.children[c]
return ptr.ind
class Findupe:
def flat(self, a):
x = ""
for i in a:
x += str(i)
return x
def isSim(self, imgx, imgy):
(score, diff) = ssim(rimg1,rimg2,full=True)
if score >= 0.9:
return True
else:
return False
parser = argparse.ArgumentParser()
parser.add_argument('-p', help='source directory path',dest='path')
parser.add_argument('-s', help='strict (only exact copies will be detected)', dest='strict', action='store_true')
args = parser.parse_args()
if args.path:
src = args.path
else:
parser.print_help()
exit(0)
try:
images = [f for f in os.listdir(src)]
des = os.getcwd()
dupes = os.path.join(des,"Dupes")
org = os.path.join(des,"Original")
try:
os.mkdir(dupes,0o666)
os.mkdir(org,0o666)
check = [False]*len(images)
cntdupe, cntorg, cnt = 0, 0, 1
trie = Trie()
obj = Findupe()
print("[INFO] Processing Images...")
for (i, img) in enumerate(images):
temp = cv2.imread(os.path.join(src,img), 0)
img = cv2.resize(temp, (8,8), interpolation=cv2.INTER_AREA)
flat_img = obj.flat(img)
trie.Insert(flat_img, i)
if args.strict:
for (i, img) in enumerate(images):
if check[i]:
continue
temp = cv2.imread(os.path.join(src,img), 0)
rimg = cv2.resize(temp, (8,8), interpolation=cv2.INTER_AREA)
flat_img = obj.flat(rimg)
ind = trie.Search(flat_img)
sz = len(ind)
if sz == 0:
continue
cntorg += 1
img = images[ind[0]]
shutil.copy(os.path.join(src,img), org)
cntdupe += sz-1
for j in ind[1:]:
img = images[j]
shutil.copy(os.path.join(src,img), dupes)
check[j] = True
print("[INFO] Images Processed ", end='')
print(f"\t dupes: {cntdupe}, org: {cntorg}")
else:
for (i, img1) in enumerate(images):
if not check[i]:
shutil.copy(os.path.join(src,img1), org)
cntorg += 1
temp1 = cv2.imread(os.path.join(src,img1), 0)
rimg1 = cv2.resize(temp1, (8,8), interpolation=cv2.INTER_AREA)
flat_img = obj.flat(rimg1)
ind = trie.Search(flat_img)
sz = len(ind)
if sz == 0:
continue
cntdupe += sz-1
for j in ind[1:]:
img = images[j]
shutil.copy(os.path.join(src,img), dupes)
check[j] = True
for j in range(i+1, len(images)):
if check[j]:
continue
img2 = images[j]
temp2 = cv2.imread(os.path.join(src,img2), 0)
rimg2 = cv2.resize(temp2, (8,8), interpolation=cv2.INTER_AREA)
if obj.isSim(rimg1, rimg2):
shutil.copy(os.path.join(src,img2), dupes)
cntdupe += 1
check[j] = True
print("[INFO] Images Processed {}/{}".format(i+1,len(images)), end='')
print(f"\t dupes: {cntdupe}, org: {cntorg}")
print(f'No. of dupes found: {cntdupe}')
print(f'No. of originals found: {cntorg}')
except FileExistsError:
print("[FileExistsError]: There already exists 2 folders called Dupes and Original in this directory, please make sure to remove them and try again")
except FileNotFoundError:
exit('[FileNotFoundError]: The specified directory wasn\'t found, please provide the full path!')