/
getMrz.js
180 lines (166 loc) · 4.32 KB
/
getMrz.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
'use strict';
const path = require('path');
const fs = require('fs-extra');
const minimist = require('minimist');
const { Image } = require('image-js');
const { getMrz } = require('..');
const argv = minimist(process.argv.slice(2));
exec().catch(console.error);
async function exec() {
if (argv.file) {
const pathname = path.resolve(argv.file);
console.time(pathname);
const result = {};
try {
await getMrz(await Image.load(pathname), {
debug: true,
out: result
});
} catch (e) {
console.error(e);
}
console.timeEnd(pathname);
await saveImages(
pathname,
result,
path.join(path.dirname(pathname), 'out')
);
await saveSingleReport(
pathname,
result,
path.join(path.dirname(pathname), 'out')
);
} else if (argv.dir) {
const dirname = path.resolve(argv.dir);
const files = (await fs.readdir(dirname)).filter((f) => {
f = f.toLowerCase();
return f.endsWith('jpg') || f.endsWith('png') || f.endsWith('jpeg');
});
const out = path.join(dirname, 'out');
const toSave = [];
await fs.emptyDir(out);
for (let file of files) {
console.log(`process ${file}`);
const imagePath = path.join(dirname, file);
console.time(imagePath);
const result = {};
try {
getMrz(await Image.load(imagePath), {
debug: true,
out: result
});
} catch (e) {
console.error(e);
}
console.timeEnd(imagePath);
await saveImages(imagePath, result, out);
toSave.push([imagePath, result]);
}
await saveReports(toSave, out);
}
}
async function saveImages(imagePath, images, out) {
const filename = path.basename(imagePath);
const ext = path.extname(filename);
const pngName = filename.replace(ext, '.png');
for (const prefix in images) {
const kind = path.join(out, prefix);
await fs.ensureDir(kind);
await images[prefix].save(path.join(kind, pngName));
}
}
const head = `
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script src="https://omnipotent.net/jquery.sparkline/2.1.2/jquery.sparkline.js"></script>
</head>
`;
const sparklines = `
<script type="text/javascript">
$(function() {
/** This code runs when everything has been loaded on the page */
/* Inline sparklines take their values from the contents of the tag */
$('.histogram').sparkline('html', {
type: 'line',
width: 400,
height: 100
});
});
</script>
`;
async function saveSingleReport(imagePath, images, out) {
const prefixes = Object.keys(images);
const report = `
<!doctype html>
<body>
${head}
<table>
<tbody>
<tr>
<th>Name</th>
${getHeaders(prefixes)}
</tr>
${getTableRow(imagePath, prefixes, images)}
</tbody>
</table>
${sparklines}
</body>
</html>
`;
await fs.writeFile(path.join(out, 'report.html'), report);
}
async function saveReports(results, out) {
let longestResult = { length: 0 };
const rows = [];
for (const [imagePath, images] of results) {
const prefixes = Object.keys(images);
if (prefixes.length > longestResult.length) {
longestResult = prefixes;
}
rows.push(getTableRow(imagePath, prefixes, images));
}
const report = `
<!doctype html>
${head}
<body>
<table>
<tbody>
<tr>
<th>Name</th>
${getHeaders(longestResult)}
</tr>
${rows.join('\n')}
</tbody>
</table>
${sparklines}
</body>
</html>
`;
await fs.writeFile(path.join(out, 'report.html'), report);
}
function getHeaders(prefixes) {
return prefixes.map((prefix) => `<th>${prefix}</th>`).join('\n');
}
function getTableRow(imagePath, prefixes, images) {
const filename = path.basename(imagePath);
const ext = path.extname(filename);
const pngName = filename.replace(ext, '.png');
return `
<tr>
<td>${filename}</td>
${prefixes
.map(
(prefix) =>
`<td><img style="max-width: 500px;" src="${prefix}/${pngName}"</td>`
)
.join('\n')}
<td>${
images.crop
? `<span class="histogram">${images.crop
.grey()
.histogram.join(',')}</span>`
: ''
}</td>
</tr>
`;
}