-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveDefaultImages.pde
190 lines (147 loc) · 4.45 KB
/
moveDefaultImages.pde
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
/**
* moves images based on a reference image.
* checked image names are stored in a cache file
*
* Created for the "fbFaces" Project
*
* Author: Jörn Röder // joernroeder.de
*
*/
float accuracy = .01; // one percent of all pixels must be the same to identify the image
String[] referenceImageNames = {
"_noImageReferenceMale.jpg",
"_noImageReferenceFemale.jpg"
};
String defaultImageFolder = "_defaultImage";
String cacheFileName = "_cache.txt";
String folder;
String[] images;
ArrayList<String> unprocessedImages;
ArrayList<String> cachedImages;
String output = "";
String title = "";
int movedCount = 0;
String moveTxt = "";
ArrayList<PImage> referenceImages;
String cacheFile[];
void setup() {
size(800, 150);
folder = selectFolder("image Folder");
unprocessedImages = new ArrayList();
referenceImages = new ArrayList();
// load cache file
cacheFile = loadStrings(folder + "/" + cacheFileName);
if (cacheFile == null) {
//saveStrings(folder + "/" + cacheFileName, "");
}
// copy to arrayList
cachedImages = new ArrayList(Arrays.asList(cacheFile));
println(cachedImages.size());
// load reference images
for (int i = 0; i < referenceImageNames.length; i++) {
println(folder + "/" + referenceImageNames[i]);
PImage referenceImage = loadImage(folder + "/" + referenceImageNames[i]);
referenceImage.loadPixels();
referenceImages.add(referenceImage);
}
File path = new File(folder);
images = path.list();
// add images to unprocessed List
for (int i = 0; i < images.length; i++) {
// skip other files and referenceImage
if (images[i].indexOf(".jpg") != -1 && !isReferenceImageName(images[i]) && !inCache(images[i])) {
unprocessedImages.add(images[i]);
}
}
title = "Compare " + images.length + " images in:\n..." + path.getParent().substring(path.getParent().lastIndexOf("/")) + folder.substring(folder.lastIndexOf("/"));
}
Boolean inCache(String fileName) {
return cachedImages.contains(fileName);
}
Boolean isReferenceImageName(String name) {
for (int i = 0; i < referenceImageNames.length; i++) {
if (name.equals(referenceImageNames[i])) {
return true;
}
}
return false;
}
void draw() {
background(0);
compareImage();
// draw UI
textAlign(LEFT);
text(title + "\n\n" + output, 10, 30);
textAlign(RIGHT);
text("Moved " + movedCount + " Files:\n" + moveTxt, width - 20, 30);
}
void dispose() {
writeCacheFile();
}
ArrayList getRandomPixels(int maxPixels, PImage referenceImage) {
ArrayList ps = new ArrayList();
int max = min(maxPixels, referenceImage.width * referenceImage.height);
for (int i = 0; i < max * accuracy; i++) {
ps.add((int) random(0, max));
}
return ps;
}
void writeCacheFile() {
println("write cache");
String[] cached = cachedImages.toArray(new String[cachedImages.size()]);
println(cached.length);
saveStrings(folder + "/" + cacheFileName, cached);
}
void compareImage() {
output = "";
int imageIndex = (int) random(unprocessedImages.size());
if (imageIndex >= unprocessedImages.size() || imageIndex <= 0) {
output = "finished!";
return;
}
// get image
PImage img = loadImage(folder + "/" + unprocessedImages.get(imageIndex));
// image exists
if (img == null) {
return;
}
// load
img.loadPixels();
Boolean hasSameImage = false;
// compare
for (int i = 0; i < referenceImages.size(); i++) {
// get reference
PImage referenceImage = referenceImages.get(i);
// get random pixel positions to compare
ArrayList<Integer> ps = getRandomPixels(img.width * img.height, referenceImage);
Boolean sameImage = false;
for (int j = 0; j < ps.size(); j++) {
if (img.pixels[ps.get(j)] == referenceImage.pixels[ps.get(j)]) {
sameImage = true;
}
else {
sameImage = false;
break;
}
}
if (sameImage) {
hasSameImage = true;
}
}
// move image and delete from list
if (hasSameImage) {
File ff = new File(folder + "/" + unprocessedImages.get(imageIndex));
ff.renameTo(new File(folder + "/" + defaultImageFolder + "/" + unprocessedImages.get(imageIndex)));
movedCount++;
if (movedCount % 7 == 0) {
moveTxt = "";
}
moveTxt += unprocessedImages.get(imageIndex) + "\n";
}
// add to cache
cachedImages.add(unprocessedImages.get(imageIndex));
// remove from list
unprocessedImages.remove(imageIndex);
// update output
output += unprocessedImages.size() + " files left.\n";
}