-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (237 loc) · 8.34 KB
/
index.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
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
const express = require('express');
const fs = require("fs");
const csv = require('csv')
const app = express();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
function getStatus() {
console.log(`The competition dataset contains ${metadata.length} images in total`);
console.log(`The verified dataset contains ${dataset.length} images in total`);
console.log(`${annotations.length} annotations have been made in total`);
console.log(`${states.to_annotate.length} images are waiting to be annotated`);
console.log(`${states.to_verify.length} annotations needs to be verified`);
}
port=3000;
const metadata = [];
const annotations = [];
const dataset = [];
const states = {
to_annotate: new Set(),
to_verify: new Set()
}
function readDataset(cb) {
fs.createReadStream('./data/dataset.csv')
.pipe(csv.parse({
columns:true
}))
.on('data', (row) => {
states.to_annotate.delete(row.image);
states.to_verify.delete(row.image);
dataset.push(row);
})
.on('end', () => {
console.log('dataset.csv successfully processed');
states.to_annotate = Array.from(states.to_annotate);
states.to_verify = Array.from(states.to_verify);
getStatus();
cb();
});
}
function readAnnotations(cb) {
fs.createReadStream('./data/annotations.csv')
.pipe(csv.parse({
columns:true
}))
.on('data', (row) => {
if(row.verified === "False") {
states.to_annotate.delete(row.image);
states.to_verify.add(row.image);
}
annotations.push(row);
})
.on('end', () => {
console.log('annotations.csv successfully processed');
readDataset(cb);
});
}
function readData(cb) {
fs.createReadStream('./data/metadata.csv')
.pipe(csv.parse({
columns:true
}))
.on('data', (row) => {
states.to_annotate.add(row.image);
metadata.push(row);
})
.on('end', () => {
console.log('metadata.csv successfully processed');
readAnnotations(cb);
});
}
const annotation_header = [
"identity",
"image",
"x1",
"y1",
"x2",
"y2",
"verified",
"judge_identity",
"judge_decision"
]
const dataset_header = [
"image",
"x1",
"y1",
"x2",
"y2"
]
function setupServer() {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.get("/image_to_annotate", (req, res) => {
if(states.to_annotate.length === 0) {
res.json({
"image": null
});
} else {
res.json({
"image": states.to_annotate[Math.floor(Math.random() * states.to_annotate.length)]
})
}
});
app.get("/image_to_verify", (req, res) => {
if(states.to_verify.length === 0) {
res.json({
"image": null
});
} else {
const image_to_verify = states.to_verify[Math.floor(Math.random() * states.to_verify.length)]
const annotation = annotations.find(annotation => annotation.image === image_to_verify);
res.json({
"image": annotation.image,
"x1": annotation.x1,
"y1": annotation.y1,
"x2": annotation.x2,
"y2": annotation.y2
});
}
});
app.post("/submit_annotation", (req, res) => {
const annotation = {
identity: req.cookies.identity,
image: req.body.image,
x1: req.body.x1,
y1: req.body.y1,
x2: req.body.x2,
y2: req.body.y2,
verified: "False",
judge_identity: "",
judge_decision: ""
}
let row = "";
for(let i=0; i<annotation_header.length; i++) {
row += annotation[annotation_header[i]] + ",";
}
row = row.substring(0, row.length-1);
annotations.push(annotation);
// append to file
fs.appendFile('./data/annotations.csv', row + "\n", function (err) {
if (err) throw err;
console.log('Results saved!');
});
const index = states.to_annotate.indexOf(annotation.image);
if (index > -1) {
states.to_annotate.splice(index, 1);
}
states.to_verify.push(annotation.image);
getStatus();
res.json({
"received": true
})
});
app.post("/submit_verification", (req, res) => {
console.log('Cookies: ', req.cookies);
console.log(req.body);
const annotation = annotations.find(annotation => annotation.image === req.body.image);
annotation.verified = "True";
annotation.judge_identity = req.cookies.identity;
// Remove image from to_verify
const index = states.to_verify.indexOf(annotation.image);
if (index > -1) {
states.to_verify.splice(index, 1);
}
if(req.body.positive) {
annotation.judge_decision = "accepted";
let dataset_annotation = {...annotation};
let x1 = parseFloat(annotation.x1);
let y1 = parseFloat(annotation.y1);
let x2 = parseFloat(annotation.x2);
let y2 = parseFloat(annotation.y2);
ox1 = Math.min(x1, x2);
oy1 = Math.min(y1, y2);
ox2 = Math.max(x1, x2);
oy2 = Math.max(y1, y2);
dataset_annotation.x1 = ox1;
dataset_annotation.y1 = oy1;
dataset_annotation.x2 = ox2;
dataset_annotation.y2 = oy2;
dataset.push(dataset_annotation);
// append to file
let row = "";
for(let i=0; i<dataset_header.length; i++) {
row += dataset_annotation[dataset_header[i]] + ",";
}
row = row.substring(0, row.length-1);
fs.appendFile('./data/dataset.csv', row + "\n", function (err) {
if (err) throw err;
console.log('Results saved!');
});
// Open the annotations.csv file and update the row
fs.readFile('./data/annotations.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
// Find the corresponding row
data = data.replace(
new RegExp(`${annotation.identity},${annotation.image},${annotation.x1},${annotation.y1},${annotation.x2},${annotation.y2},False,,\n`),
`${annotation.identity},${annotation.image},${annotation.x1},${annotation.y1},${annotation.x2},${annotation.y2},${annotation.verified},${annotation.judge_identity},${annotation.judge_decision}\n`
);
// Write the new data to the file
fs.writeFile('./data/annotations.csv', data, 'utf8', function (err) {
if (err) return console.log(err);
});
});
} else {
annotation.judge_decision = "rejected";
states.to_annotate.push(annotation.image);
// Open the annotations.csv file and update the row
fs.readFile('./data/annotations.csv', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
// Find the corresponding row
data = data.replace(
new RegExp(`${annotation.identity},${annotation.image},${annotation.x1},${annotation.y1},${annotation.x2},${annotation.y2},False,,\n`),
`${annotation.identity},${annotation.image},${annotation.x1},${annotation.y1},${annotation.x2},${annotation.y2},${annotation.verified},${annotation.judge_identity},${annotation.judge_decision}\n`
);
// Write the new data to the file
fs.writeFile('./data/annotations.csv', data, 'utf8', function (err) {
if (err) return console.log(err);
});
});
}
getStatus();
res.json({
"received": true
})
});
app.use(express.static('public'));
app.use("/images", express.static('images'));
app.use("/data", express.static('data'));
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
}
readData(setupServer);