forked from zapolnoch/node-tesseract-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·37 lines (30 loc) · 1.01 KB
/
index.js
File metadata and controls
executable file
·37 lines (30 loc) · 1.01 KB
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
const exec = require("child_process").exec
const log = console.debug
function recognize(filename, config = {}) {
const options = getOptions(config)
const binary = config.binary || "tesseract"
const command = [binary, filename, "stdout", ...options].join(" ")
if (config.debug) log("command", command)
return new Promise((resolve, reject) => {
exec(command, {encoding: 'buffer', maxBuffer: 134217728}, (error, stdout, stderr) => {
if (config.debug) log(stderr)
if (error) reject(error)
resolve(stdout)
})
})
}
function getOptions(config) {
const ocrOptions = ["tessdata-dir", "user-words", "user-patterns", "psm", "oem", "dpi"]
return Object.entries(config)
.map(([key, value]) => {
if (["debug", "presets", "binary"].includes(key)) return
if (key === "lang") return `-l ${value}`
if (ocrOptions.includes(key)) return `--${key} ${value}`
return `-c ${key}=${value}`
})
.concat(config.presets)
.filter(Boolean)
}
module.exports = {
recognize,
}