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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <thread>
#include <getopt.h>
#if PATH_VALSIZE == 2
#include <cwchar>
#endif
#ifdef _WIN32 //for the superior operating system
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <processenv.h>
#include <shellapi.h>
#endif
#include "signature.hpp"
#include "subslice_signature.hpp"
#include "signature_db.hpp"
#include "thread_pool.hpp"
#define DEBUG 0
namespace fs = std::filesystem;
int ctr;
int recursive;
int njobs = 1;
double threshold = 0.3;
std::vector<fs::path> paths;
std::vector<fs::path> files;
size_t nsliceh = 3;
size_t nslicev = 3;
signature_config cfg_full =
{
9, //slices
3, //blur_window
2, //min_window
true, //crop
true, //comp
0.5, //pr
1./128, //noise_threshold
0.05, //contrast_threshold
0.25 //max_cropping
};
signature_config cfg_subslice =
{
4, //slices
16, //blur_window
2, //min_window
false, //crop
true, //comp
0.5, //pr
1./64, //noise_threshold
0.05, //contrast_threshold
0.25 //max_cropping
};
struct sig_eq
{
bool operator()(const signature& a, const signature& b) const
{
//return a.distance(b) < 0.1;
return a == b;
}
};
typedef std::pair<size_t, int> slice_info;
signature_db *sdb;
int parse_arguments(int argc, char **argv)
{
recursive = 0;
int help = 0;
option longopt[]=
{
{"recursive", no_argument , &recursive, 1},
// {"destdir" , required_argument, 0 , 'D'},
{"jobs" , required_argument, 0 , 'j'},
// {"threshold", required_argument, 0 , 'd'},
{"help" , no_argument , &help , 1},
{0 , 0 , 0 , 0}
};
while(1)
{
int idx = 0;
int c = getopt_long(argc, argv, "rhj:", longopt, &idx);
if (!~c) break;
switch (c)
{
case 0:
if (longopt[idx].flag) break;
if (std::string("jobs") == longopt[idx].name)
sscanf(optarg, "%d", &njobs);
//if(std::string("threshold") == longopt[idx].name)
//sscanf(optarg, "%lf", &threshold);
break;
case 'r':
recursive = 1;
break;
case 'h':
help = 1;
break;
case 'j':
sscanf(optarg, "%d", &njobs);
break;
case 'd':
//sscanf(optarg, "%lf", &threshold);
break;
}
}
#ifdef _WIN32 //w*ndows, ugh
wchar_t *args = GetCommandLineW();
int wargc;
wchar_t **wargv = CommandLineToArgvW(args, &wargc);
if (wargv && wargc == argc)
{
for (; optind < argc; ++optind)
paths.push_back(wargv[optind]);
}
#else
for (; optind < argc; ++optind)
paths.push_back(argv[optind]);
#endif
if (help || argc < 2)
{
printf(
"Usage: %s [OPTION] PATH...\n"
"Detect potentially duplicate images in PATHs and optionally perform an action on them.\n\n"
" -h, --help Display this help message and exit.\n"
" -r, --recursive Recurse into all directories.\n"
" -j, --jobs Number of concurrent tasks to run at once.\n"
// " -d, --threshold Threshold distance below which images will be considered similar.\n"
,argv[0]
);
return 1;
}
if (threshold > 1 || threshold < 0)
{
puts("Invalid threshold value.");
return 2;
}
if (threshold < 1e-6) threshold = 1e-6;
if (!paths.size())
{
puts("Missing image path.");
return 2;
}
return 0;
}
void build_file_list(fs::path path, bool recursive, std::vector<fs::path> &out)
{
if (recursive)
{
auto dirit = fs::recursive_directory_iterator(path);
for (auto &p : dirit)
{
std::fstream st(p.path(), std::ios::binary | std::ios::in);
char c[8];
st.read(c, 6);
if (st.gcount() < 6) continue;
if(!memcmp(c,"\x89PNG\r\n", 6) || !memcmp(c,"\xff\xd8\xff", 3))
{
out.push_back(p.path());
#if DEBUG > 0
printf("%ld, %s\n", out.size() - 1, out.back().c_str());
#endif
}
st.close();
}
}
else
{
auto dirit = fs::directory_iterator(path);
for(auto &p : dirit)
{
std::fstream st(p.path(), std::ios::binary | std::ios::in);
char c[8];
st.read(c, 6);
if (st.gcount() < 6) continue;
if(!memcmp(c,"\x89PNG\r\n", 6) || !memcmp(c,"\xff\xd8\xff", 3))
{
out.push_back(p.path());
#if DEBUG > 0
printf("%ld, %s\n", out.size() - 1, out.back().c_str());
#endif
}
st.close();
}
}
}
int main(int argc,char** argv)
{
if (int pr = parse_arguments(argc, argv)) return pr - 1;
puts("building list of files to compare...");
for (auto &p : paths)
build_file_list(p, recursive, files);
printf("%lu files to compare.\n", files.size());
puts("initializing database...");
sdb = new signature_db();
puts("computing signature vectors...");
populate_cfg_t pcfg = {
nsliceh,
nslicev,
cfg_full,
cfg_subslice,
threshold,
[](size_t c, int){printf("%lu\r", c); fflush(stdout);},
njobs
};
sdb->populate(files, pcfg);
puts("grouping similar images...");
sdb->group_similar();
sdb->batch_get_signature_begin();
std::vector<dupe_t> dupes = sdb->dupe_pairs();
for (auto &p : dupes)
{
fs::path p1, p2;
std::tie(p1, std::ignore) = sdb->get_signature(p.id1);
std::tie(p2, std::ignore) = sdb->get_signature(p.id2);
#if PATH_VALSIZE == 2
wprintf(L"%ls %ls %f\n", p1.c_str(), p2.c_str(), p.distance);
#else
printf("%s %s %f\n", p1.c_str(), p2.c_str(), p.distance);
#endif
}
std::vector<std::vector<size_t>> gp = sdb->groups_get();
for (auto gi = gp.begin(); gi != gp.end(); ++gi)
{
if (gi->size() < 2) continue;
printf("group #%lu:\n", gi - gp.begin());
for (auto &id : *gi)
{
fs::path p;
std::tie(p, std::ignore) = sdb->get_signature(id);
#if PATH_VALSIZE == 2
wprintf(L"\t%ls\n", p.c_str());
#else
printf("\t%s\n", p.c_str());
#endif
}
}
sdb->batch_get_signature_end();
sdb->to_db_file("test.sigdb");
delete sdb;
return 0;
}
|