-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_eval.js
63 lines (45 loc) · 1.54 KB
/
run_eval.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
import fs from 'fs';
import path from 'path';
import scribe from 'scribe.js-ocr';
// Set evaluation options
scribe.opt.ignorePunct = true;
// Directory containing images
const truthDir = 'ground_truth';
/** @type {Parameters<typeof scribe.compareOCR>[2]} */
const compOptions = {
ignorePunct: scribe.opt.ignorePunct,
ignoreCap: scribe.opt.ignoreCap,
confThreshHigh: scribe.opt.confThreshHigh,
confThreshMed: scribe.opt.confThreshMed,
};
/**
*
* @param {string} compDir
*/
async function evalOCR(compDir) {
const evalStatsDocArr = [];
const files = fs.readdirSync(truthDir);
const comp = path.basename(compDir);
for (const file of files) {
const filePathTruth = path.join(truthDir, file);
const filePathComp = path.join(compDir, file.replace(/\.truth\.hocr/, '.hocr'));
if (!fs.existsSync(filePathComp)) {
console.log(`Skipping comparison due to missing file: ${file}`);
continue;
}
await scribe.importFiles([filePathComp]);
await scribe.importFilesSupp([filePathTruth], 'Ground Truth');
const res = await scribe.compareOCR(scribe.data.ocr.active, scribe.data.ocr['Ground Truth'], compOptions);
let evalStatsDoc = scribe.utils.calcEvalStatsDoc(res.metrics);
evalStatsDoc = {
file: file,
...evalStatsDoc,
}
evalStatsDocArr.push(evalStatsDoc);
await scribe.terminate();
}
const csvStr = scribe.utils.convertToCSV(evalStatsDocArr);
fs.writeFileSync(`stats/eval_stats_${comp}.csv`, csvStr);
}
const arg = process.argv[2];
evalOCR(arg).catch((err) => console.error(err));