-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
72 lines (60 loc) · 2.01 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
var os = require('os');
var fs = require('fs');
var path = require('path');
var async = require('async');
var uuid = require('node-uuid');
var needle = require('needle');
var tesseract = require('node-tesseract');
var debug = require('debug')('ocr-by-image-url');
module.exports = {
getImageText: function (url, proxy, callback) {
if (typeof proxy === "function") {
callback = proxy;
proxy = null;
}
var wget = function (callback) {
debug('http get image: ' + url);
var options = {
output: path.join(os.tmpdir(), 'node-ocr-by-image-url-' + uuid.v4() + '.png')
};
if (proxy) {
options.proxy = proxy;
}
needle.get(url, options, function (error, response, body) {
if (error) {
callback(error);
return;
}
debug('successfully downloaded to: ' + options.output);
callback(null, options.output);
});
};
var ocr = function (file, callback) {
debug('tesseract process image ' + file);
tesseract.process(file, function (error, text) {
if (error) {
callback(error);
return;
}
debug('ocr result: ' + text);
callback(null, file, text);
});
};
var remove_file = function (file, text, callback) {
debug('remove image ', file);
fs.unlink(file, function (error) {
if (error) {
debug('could not remove temp image for ocr detection', file);
}
callback(null, file, text);
});
};
async.waterfall([wget, ocr, remove_file], function (error, file, text) {
if (error) {
callback(error);
return;
}
callback(null, text);
});
}
};