-
Notifications
You must be signed in to change notification settings - Fork 6
/
processor.cpp
578 lines (469 loc) · 14.6 KB
/
processor.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <cstdio>
#include <vector>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <algorithm>
#include <podofo/podofo.h>
#include "box.h"
#include "log.h"
#include "math.h"
#include "read.h"
#include "data.h"
#include "boxes.h"
#include "rotate.h"
#include "pixels.h"
#include "extract.h"
#include "processor.h"
Processor::Processor(int threads, bool website, Database& db)
: defaultForm(*this),
exiting(false),
waiting(false),
extractT(extractImages, threads),
parseT(parseImage, threads),
db(db),
website(website),
statusWaiting(0),
statusNewItem(false)
{
}
Processor::~Processor()
{
exit();
}
void extractImages(Form* form)
{
std::list<FormImage> newImages;
try
{
// Get the images from the PDF
newImages = extract(form->filename, *form);
// Set number of pages to however many images we got out of the PDF
form->pages = newImages.size();
}
catch (const std::runtime_error& error)
{
form->log(form->filename + ", " + error.what());
form->processor.finish(form->id);
return;
}
catch (const PoDoFo::PdfError& error)
{
error.PrintErrorMsg();
//return error.GetError();
// Don't write this to screen
form->log(form->filename + ", " + error.what(), LogType::Error);
form->processor.finish(form->id);
return;
}
catch (...)
{
form->log("Unhandled exception");
form->processor.finish(form->id);
return;
}
// If no images, we're done
if (newImages.empty())
form->processor.finish(form->id);
// Add these extracted images to the queue after making sure there aren't
// any extraction errors
for (FormImage& i : newImages)
form->processor.parseT.queue(&i);
// Move these new images to the actual list. Do this here instead
// of directly appending them to the list so that we only need
// to lock this once.
std::unique_lock<std::mutex> lock(form->images_mutex);
form->formImages.splice(form->formImages.end(), newImages);
}
void parseImage(FormImage* formImage)
{
// Use this to get a unique ID each time this function is called,
// used for writing out the debug images
static long long static_thread_id = 0;
const long long thread_id = static_thread_id++;
// When this thread has an error, write message including thread id, but
// continue processing the rest of the images.
try
{
// Find all blobs in the image
Blobs blobs(formImage->image);
// Box information for this image
Data data;
// Find all the boxes
std::vector<Coord> boxes = findBoxes(formImage->image, blobs, data);
if (boxes.size() > TOTAL_BOXES)
throw std::runtime_error("too many boxes detected");
if (boxes.size() < TOTAL_BOXES)
throw std::runtime_error("some boxes not detected");
if (DEBUG)
{
std::ostringstream s_orig;
s_orig << "debug" << thread_id << "_orig.png";
formImage->image.save(s_orig.str(), true, false, true);
}
// Rotate the image
Coord rotate_point;
double rotation = findRotation(formImage->image, boxes, rotate_point);
// Negative since the origin is the top-left point (this is the 4th quadrant)
if (rotation != 0)
{
formImage->image.rotate(-rotation, rotate_point);
formImage->image.rotateVector(boxes, rotate_point, -rotation);
// The blobs are constant, so just recalculate them all
blobs = Blobs(formImage->image);
}
// Sort all boxes with respect to y if we rotated counter-clockwise
if (rotation > 0)
std::sort(boxes.begin(), boxes.end());
// Sort the bottom row of boxes by increasing x coordinates.
std::sort(boxes.begin()+BOT_START-1, boxes.begin()+BOT_END, CoordXSort());
// Determine what is black (changes in BW, color, and grayscale)
double black = findBlack(formImage->image, blobs, boxes, data);
// Find ID number
long long id = findID(formImage->image, blobs, boxes, data, black);
std::vector<Answer> answers;
// Don't bother finding answers if we couldn't even get the student ID
if (id != DefaultID)
answers = findAnswers(formImage->image, blobs, boxes, data, black);
// Debug information
if (DEBUG)
{
for (const Coord& box : boxes)
formImage->image.mark(box);
std::ostringstream s;
s << "debug" << thread_id << ".png";
formImage->image.save(s.str());
}
formImage->id = id;
formImage->answers = answers;
formImage->thread_id = thread_id;
}
catch (const std::runtime_error& error)
{
if (DEBUG)
{
std::ostringstream s;
s << "debug" << thread_id << ".png";
formImage->image.save(s.str());
}
std::ostringstream msg;
if (!formImage->form.processor.website)
msg << "thread #" << thread_id << ", " << formImage->image.filename() << " - ";
msg << error.what();
formImage->form.log(msg.str());
}
// Another page is complete
formImage->form.incDone();
// If we're in website mode, add this form update to the queue so that
// connections can send another update if waiting on this form
if (formImage->form.processor.website)
{
// Get how close this form is to being done
int percentage = smartFloor(
100.0*formImage->form.getDone()/formImage->form.pages);
// Return 99 if it's "done," the last percent is adding it to
// the database, etc.
if (percentage == 100)
percentage = 99;
formImage->form.processor.statusAdd(
Status(formImage->form.id, percentage));
}
// If done, save results
if (formImage->form.getDone() == formImage->form.pages)
formImage->form.processor.finish(formImage->form.id);
}
void Processor::wait()
{
waiting = true;
// Just in case somebody tries wait() after exit()
if (!exiting)
{
extractT.wait();
parseT.wait();
}
}
void Processor::exit()
{
// You can only call this once
if (exiting)
return;
// Tell all waiting threads to exit
exiting = true;
statusWakeAll();
// Only exit these threads if we haven't already waited for them to complete
// (thus they already exited)
if (!waiting)
{
extractT.exit();
parseT.exit();
}
}
Form& Processor::findForm(long long id)
{
std::unique_lock<std::mutex> lock(forms_mutex);
std::list<Form>::iterator i = std::find_if(forms.begin(), forms.end(),
FormPredicate(id));
if (i == forms.end())
return defaultForm;
else
return *i;
}
bool Processor::done(long long id)
{
Form& form = findForm(id);
return form == defaultForm;
}
void Processor::finish(long long id)
{
// Only do this when used with the website. We don't want to delete
// forms passed in as command line arguments.
if (!website)
return;
std::string summary;
std::string exported;
std::string filename;
{
std::unique_lock<std::mutex> lock(forms_mutex);
std::list<Form>::iterator form = std::find_if(forms.begin(), forms.end(),
FormPredicate(id));
// Not found
if (form == forms.end())
return;
// Get output
summary = print(*form);
exported = csv(*form);
filename = form->filename;
// Delete from list
forms.erase(form);
}
// Save to database
db.updateForm(id, summary, exported);
// We're done, send final update
statusAdd(Status(id, 100));
// Delete off disk last, if we get killed right before doing this,
// we'll see this file still exists and reprocess this form on start
if (std::remove(filename.c_str()) != 0)
log("Couldn't delete form \"" + filename + "\"");
}
std::string Processor::print(long long id)
{
Form& form = findForm(id);
return print(form);
}
std::string Processor::print(Form& form)
{
std::ostringstream out;
if (form == defaultForm)
return "";
// Find the key based on the key's ID
out << std::left;
if (DEBUG)
out << std::setw(5) << "#";
out << std::setw(10) << "ID"
<< "\tAnswers (key first)"
<< std::endl;
int total = 0;
bool found = false;
std::vector<Answer> key;
for (const FormImage& i : form.formImages)
{
if (i.id == form.key)
{
if (found)
{
log("key already found, skipping this one");
}
else
{
if (DEBUG)
out << std::setw(5) << i.thread_id;
out << std::setw(10) << i.id << "\t";
for (const Answer b : i.answers)
{
if (b != Answer::Blank)
{
out << b << " ";
++total;
}
}
out << std::endl;
key = i.answers;
found = true;
}
}
}
if (found)
{
typedef std::vector<Answer>::size_type size_type;
// Grade student's exams
std::map<long long, double> scores;
for (const FormImage& i : form.formImages)
{
if (i.id == DefaultID)
{
out << i.thread_id << ": failed to determine student ID" << std::endl;
}
else if (i.id != form.key)
{
if (DEBUG)
out << std::left << std::setw(5) << i.thread_id;
out << std::left << std::setw(10) << i.id << "\t";
int same = 0;
// Only score the ones that the teacher didn't leave blank
for (size_type q = 0; q < i.answers.size() && q < key.size(); ++q)
{
if (key[q] != Answer::Blank)
{
out << i.answers[q] << " ";
if (key[q] == i.answers[q])
++same;
}
}
out << std::endl;
scores[i.id] = (total>0)?1.0*same/total:1;
}
}
// Output scores
out << std::endl << "Scores" << std::endl;
for (const std::pair<long long, double>& score: scores)
{
out << " " << std::left << std::setw(10) << score.first << " "
<< std::fixed << std::setprecision(2) << std::right << std::setw(6)
<< score.second*100 << "%" << std::endl;
}
}
else
{
out << "Key not found. Given IDs:" << std::endl;
for (const FormImage& i : form.formImages)
if (i.id != DefaultID)
out << " " << i.id << std::endl;
}
if (!form.output.empty())
out << std::endl << form.output;
return out.str();
}
std::string Processor::csv(long long id)
{
Form& form = findForm(id);
return csv(form);
}
std::string Processor::csv(Form& form)
{
std::ostringstream out;
if (form == defaultForm)
return "";
// Print header
out << "ID, Score";
int total = 0;
std::vector<Answer> key;
for (const FormImage& i : form.formImages)
{
if (i.id == form.key)
{
for (unsigned int j = 0; j < i.answers.size(); ++j)
{
if (i.answers[j] != Answer::Blank)
{
out << ", " << (j+1);
++total;
}
}
key = i.answers;
break;
}
}
out << std::endl;
// Print key
for (const FormImage& i : form.formImages)
{
if (i.id == form.key)
{
out << i.id << ", KEY";
for (const Answer b : i.answers)
if (b != Answer::Blank)
out << ", " << b;
out << std::endl;
key = i.answers;
break;
}
}
typedef std::vector<Answer>::size_type size_type;
// Print student responses and scores
for (const FormImage& i : form.formImages)
{
if (i.id == DefaultID)
{
out << "Unknown, 0%" << std::endl;
}
else if (i.id != form.key)
{
// Calculate score
int same = 0;
// Only score the ones that the teacher didn't leave blank
for (size_type q = 0; q < i.answers.size() && q < key.size(); ++q)
if (key[q] != Answer::Blank && key[q] == i.answers[q])
++same;
double score = (total>0)?1.0*same/total:1;
// Print
out << i.id << ", " << score*100 << "%";
for (size_type q = 0; q < i.answers.size() && q < key.size(); ++q)
{
if (key[q] != Answer::Blank)
{
// Don't use _ for blank here, just make the cell blank
if (i.answers[q] == Answer::Blank)
out << ", ";
else
out << ", " << i.answers[q];
}
}
out << std::endl;
}
}
return out.str();
}
void Processor::add(long long id, long long key, const std::string& filename)
{
std::unique_lock<std::mutex> lock(forms_mutex);
forms.push_back(Form(id, key, filename, *this));
extractT.queue(&forms.back());
}
std::vector<Status> Processor::statusWait()
{
std::unique_lock<std::mutex> lck(status_mutex);
++statusWaiting;
while (true)
{
statusCond.wait(lck, [this] { return statusNewItem || exiting; });
if (statusNewItem || exiting)
break;
}
--statusWaiting;
// Copy it so we can return this even if we clear it below
std::vector<Status> results = status;
// Everybody waiting grabbed the status updates already
if (statusWaiting == 0)
{
status.clear();
statusNewItem = false;
}
return results;
}
void Processor::statusAdd(const Status& newStatus)
{
{
std::unique_lock<std::mutex> lock(status_mutex);
status.push_back(std::move(newStatus));
statusNewItem = true;
}
statusCond.notify_all();
}
void Processor::statusWakeAll()
{
std::unique_lock<std::mutex> lck(status_mutex);
statusNewItem = true;
statusCond.notify_all();
}