-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkImageClassifier.js
202 lines (186 loc) · 5.37 KB
/
darkImageClassifier.js
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
const fs = require('fs');
const { resolve } = require('path');
const { reject } = require('q');
const { Console } = require('console');
const gm = require('gm').subClass({imageMagick: true});
const PNG = require("pngjs").PNG;
let pathToFolder = process.argv[2]; // allow path to be given as parameter from shell
let pathToImage = '';
let fileNames = new Array();
let files; // save files from given directory in global variable
// so that promises can execute after `readdir` method
let fileScores = new Array();
function promiseImageScore() {
return new Promise((resolve, reject) => {
getImageScore(resolve, reject);
});
}
function prmoiseSavingFileNames() {
return new Promise((resolve, reject) => {
addFileNames(resolve, reject);
});
}
function promiseReadingDir() {
return new Promise((resolve, reject) => {
readDir(resolve, reject);
});
}
function promiseSaveAllTheScores() {
return new Promise((resolve, reject) => {
saveAllTheScores(resolve, reject);
});
}
function promiseOrderImagesByScore() {
return new Promise((resolve, reject) => {
orderImagesByScore(resolve, reject);
});
}
function promiseRenameFilesInOrder() {
return new Promise((resolve, reject) => {
renameFilesInOrder(resolve, reject);
});
}
function getImageScore(resolve, reject) {
let img = gm(pathToImage);
// Get the PNG buffer
img.toBuffer("PNG", (err, buff) => {
if (err) return reject(err);
// Get the image size
img.size((err, size) => {
if (err) {
console.log(err);
return reject(err);
}
// Parse the PNG buffer
let str = new PNG();
str.end(buff);
// After it's parsed...
str.on("parsed", buffer => {
// Get the pixels from the image
let idx, score = 0, rgb = {r: 0, g: 0, b: 0};
for (let y = 0; y < size.height; y++)
for (let x = 0; x < size.width; x++) {
idx = (size.width * y + x) << 2;
rgb.r = buffer[idx];
rgb.g = buffer[idx + 1];
rgb.b = buffer[idx + 2];
score += (rgb.r + rgb.g + rgb.b) / 765;
}
return resolve(score / (size.height * size.width));
});
str.on("error", e => {
return reject(e);
});
});
});
}
function checkAllScoresAreSaved() {
if(fileScores.length != fileNames.length) return false;
for(let i = 0; i < fileScores.length; i++)
if(fileScores[i] < 0) return false;
return true;
}
function checkScoresAreOrdered() {
for(let i = 0; i < fileScores.length - 1; i++)
if(fileScores[i] < fileScores[i + 1]) return false;
return true;
}
function saveAllTheScores(resolve, reject) {
for(let i = 0; i < fileNames.length; i++) {
pathToImage = fileNames[i];
let localIndex = i;
fileScores.push(-1);
promiseImageScore()
.then(imageScore => {
fileScores[localIndex] = imageScore;
if(checkAllScoresAreSaved()) resolve();
})
.catch(e => {
throw e;
});
}
}
function addFileNames(resolve, reject) {
files.forEach(file => {
pathToImage = pathToFolder + '/' + file;
fileNames.push(pathToImage);
if(files.length == fileNames.length) {
resolve();
return;
}
});
}
function readDir(resolve, reject) {
// see which images are to be found in the specificd directory
fs.readdir(pathToFolder, (err, Files) => {
if (err) return reject('Unable to scan directory: ' + err);
files = Files;
if(files) resolve();
else reject(new Error('could not load files from the given directory path...'));
});
}
function orderImagesByScore(resolve, reject) {
// order them in descending order
// the brigthest image has to be the first one
// the darkes image has to be the last one
for(let i = 0; i < fileScores.length - 1; i++)
for(let j = i + 1; j < fileScores.length; j++) {
if(fileScores[i] < fileScores[j]) {
let aux = fileScores[i];
fileScores[i] = fileScores[j];
fileScores[j] = aux;
aux = fileNames[i];
fileNames[i] = fileNames[j];
fileNames[j] = aux;
// check if the scores are orderd in the if, as well,
// to avoid not checking the last state of the vector
if(checkScoresAreOrdered()) resolve();
}
if(checkScoresAreOrdered()) resolve();
}
}
function renameFilesInOrder(resolve, reject) {
for(let i = 0; i < fileNames.length; i++) {
let extenstion = fileNames[i].slice(fileNames[i].indexOf('.'));
let newName = pathToFolder + '/' + i + extenstion;
fs.rename(fileNames[i], newName, err => {
if (err) reject(err);
else if(i == fileNames.length - 1) resolve();
});
}
}
promiseReadingDir()
.then(() => {
console.log('finished reading dir files...');
prmoiseSavingFileNames()
.then(() => {
console.log('saved all the file names!');
promiseSaveAllTheScores()
.then(() => {
console.log('saved scores of the files');
promiseOrderImagesByScore()
.then(() => {
console.log('ordered images by score');
promiseRenameFilesInOrder()
.then(() => {
console.log('renamed files in the right order!');
})
.catch(err => {
console.error(err);
})
})
.catch(err => {
throw err;
})
})
.catch(err => {
throw err;
})
})
.catch(err => {
throw err;
});
})
.catch(err => {
throw err;
});