-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
164 lines (134 loc) · 4.42 KB
/
tools.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
/* All backend functions are defined in this module */
const fs = require('fs');
const path = require('path');
const mimeCheck = require('mime-types');
const newLine = require('os').EOL;
const tempDir = path.join(__dirname, 'temp');
const dataDir = path.join(__dirname, 'public', 'data');
const outDir = path.join(dataDir, 'outputs');
const picDir = path.join(dataDir, 'pictures');
function json2csv(tags, labels) {
var data = tags.join(',');
for (var i in labels) {
var row = [];
for (var k in tags) {
if (labels[i].hasOwnProperty(tags[k])) {
row.push(labels[i][tags[k]]);
} else {
row.push[''];
}
}
data = data + newLine + row.join(',');
}
return data;
}
function writeCSV(fileName, data, callback) {
if(!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
}
fileName = getSafeFileName(fileName);
fs.writeFile(path.join(tempDir, fileName), data, (err) => {
if (err) {
console.log(`E/writeCSV: cannot write file ${fileName}. ${err.message}`);
return false;
} else {
console.log(`I/writeCSV: wrote ${fileName} file`);
}
callback(fileName);
});
return true;
}
function getSafeFileName(fileName, suffix=1) {
let safeName = fileName;
let extIndex = fileName.lastIndexOf('.');
while (fs.existsSync(path.join(tempDir, safeName)) && suffix <= Number.MAX_SAFE_INTEGER) {
// file exists, change file name
safeName = fileName.substr(0, extIndex) + '-' + suffix + fileName.substr(extIndex);
suffix++;
}
// file doesn't exist, safe to use existing name
return safeName;
}
function parseTagsNlabels(fileDir, fileList, tagList) {
out = [];
for (var i in fileList) {
var fileName = fileList[i];
var json = {'src': path.join(fileDir,fileName),
'filename': String(fileName),
'id': Number(i),
'tags':tagList};
out.push(json);
}
return out;
}
function getImagesByDir(dir, callback) {
imageList = [];
fs.readdir(dir, (err, items) => {
if(err) {
console.log('E/getImagesByDir:', err.message);
} else {
for(var i in items) {
mime_check = mimeCheck.lookup(items[i]);
if(typeof mime_check == 'string' && mime_check.indexOf('image')!=-1) {
imageList.push(items[i]);
}
}
callback(imageList);
}
});
}
function extnLessName(filename) {
return path.basename(filename, path.extname(filename));
}
function linkUserDir(target, source) {
// overrides existing
var temp = source + '.bak';
fs.symlinkSync(target, temp, 'dir');
fs.renameSync(temp, source);
}
function normalizeBox(item, ogSize, scaleSize) {
var W = scaleSize.width / ogSize.width;
var H = scaleSize.height / ogSize.height;
item["scaled"] = {
startX: Math.round(item.location.startX * W),
startY: Math.round(item.location.startY * H),
endX: Math.round(item.location.endX * W),
endY: Math.round(item.location.endY * H)
}
return item;
}
function loadJSON(filePath, callback){
fs.readFile(filePath, (err, data) => {
if(err) throw err;
callback(data);
});
}
function scaleImage(Jimp, imagePath, saveDir, scale){
Jimp.read(imagePath, (err, image) => {
if(err) {
console.log('E/scaleImage:', err.message);
} else {
if(!fs.existsSync(path.join(saveDir, path.basename(imagePath)))
|| image.bitmap.width != scale.width
|| image.bitmap.height != scale.height) {
image.resize(scale.width, scale.height)
.write(path.join(saveDir, path.basename(imagePath)));
console.log('I/scaleImage: saved scaled image');
}
}
});
}
exports.getSafeFileName = getSafeFileName;
exports.json2csv = json2csv;
exports.writeCSV = writeCSV;
exports.parseTagsNlabels = parseTagsNlabels;
exports.getImagesByDir = getImagesByDir;
exports.linkUserDir = linkUserDir;
exports.extnLessName = extnLessName;
exports.normalizeBox = normalizeBox;
exports.loadJSON = loadJSON;
exports.scaleImage = scaleImage;
exports.dataDir = dataDir;
exports.tempDir = tempDir;
exports.outDir = outDir;
exports.picDir = picDir;