tesseractocr
Advanced tools
Comparing version 1.1.1 to 1.2.0
57
docs.md
@@ -12,3 +12,3 @@ # API Documentation of `tesseractocr` Node.js module | ||
Pass an image to Tesseract to recognize text from it. | ||
This method spawns a new tesseract process with the given options and streams the source into it. | ||
This method spawns a new tesseract process with the given options and streams the source into it. | ||
Callback is fired when the child process gets finished. | ||
@@ -57,4 +57,33 @@ | ||
recognize('image.tiff').then(console.log, console.error) | ||
``` | ||
``` | ||
### recognize.cancel(promise) : undefined | ||
Abort an ongoing recognition process. | ||
#### Examples | ||
```js | ||
const proc = tesseract.recognize(source) | ||
// ... for e.g. an http server... | ||
request.on('timeout', () => recgnize.cancel(proc)) | ||
``` | ||
### recognize.cancelAll() : undefined | ||
Abort all the ongoing recognition processes. | ||
#### Examples | ||
```js | ||
for (let i = 0; i < 10; i++) { | ||
tesseract | ||
.recognize(source + i) | ||
.then(console.log) | ||
} | ||
process.on('exit', () => recognize.cancelAll()) | ||
``` | ||
### tesseract.listLanguages([execPath], [callback]) : Promise | ||
@@ -82,3 +111,3 @@ | ||
```js | ||
const recognize = require('tesseractocr') | ||
const recognize = require('tesseractocr') | ||
@@ -96,8 +125,24 @@ recognize('source', (err, text) => { /* ... */ }) | ||
let text = await recognize(source) | ||
// do stuffs with recognized text... | ||
// do stuff with recognized text... | ||
text = text.split(' ') | ||
return text | ||
} | ||
``` | ||
### #3 | ||
Recognition can be aborted in the event of e.g. a client timeout. This terminates the given Tesseract child process: | ||
```js | ||
const proc = reconize(source) | ||
request.on('timeout', () => recognize.cancel(proc)) | ||
``` | ||
It's also possible to terminate all the in-progress Tesseract processes in the event of e.g. force shutdown: | ||
```js | ||
process.on('exit', recognize.cancelAll()) | ||
``` |
46
index.js
@@ -10,3 +10,6 @@ 'use strict' | ||
exports.recognize = recognize | ||
exports.cancel = cancel | ||
exports.cancelAll = cancelAll | ||
exports.default = recognize | ||
exports.processCount = processCount | ||
@@ -29,3 +32,4 @@ const RX_LANG = /^\w*?[a-z]{3}(_[a-z]{3,4})?\w*?$/ | ||
destroy = require('destroy'), | ||
IECE = error('InvalidExitCodeError', initInvalidExitCodeError) | ||
IECE = error('InvalidExitCodeError', initInvalidExitCodeError), | ||
procs = new Map // map for tracking in-progress recognition processes | ||
@@ -192,5 +196,10 @@ // make recognize() an EventEmitter to be able to fire 'warning' events when necessary | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
switch (typeof options) { | ||
case 'function': | ||
callback = options | ||
options = {} | ||
break | ||
case 'undefined': | ||
options = {} | ||
} | ||
@@ -203,3 +212,3 @@ | ||
return new Promise((resolve, reject) => { | ||
const p = new Promise((resolve, reject) => { | ||
getBinaryPath(options, (err, tesseract) => { | ||
@@ -252,2 +261,7 @@ let child, stream, transform, | ||
process.nextTick(() => { | ||
procs.set(p, child) | ||
recognize.emit('started', p) | ||
}) | ||
function onError(err) { | ||
@@ -266,2 +280,7 @@ onExit() | ||
function onExit() { | ||
process.nextTick(() => { | ||
procs.delete(p) | ||
recognize.emit('finished', p) | ||
}) | ||
if (stream) | ||
@@ -275,4 +294,21 @@ destroy(stream) | ||
}) | ||
return p | ||
} | ||
function cancel(promise) { | ||
const proc = procs.get(promise) | ||
if (proc) | ||
proc.kill() | ||
} | ||
function cancelAll() { | ||
procs.forEach(proc => proc.kill()) | ||
} | ||
function processCount() { | ||
return procs.size | ||
} | ||
function listLanguages(execPath, callback) { | ||
@@ -279,0 +315,0 @@ let options = {} |
{ | ||
"name": "tesseractocr", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Node.js wrapper for Tesseract OCR CLI.", | ||
@@ -38,4 +38,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^12.0.1" | ||
"tap": "^12.4.0" | ||
} | ||
} |
@@ -56,4 +56,8 @@ [![view on npm](http://img.shields.io/npm/v/tesseractocr.svg?style=flat-square)](https://www.npmjs.com/package/tesseractocr) | ||
## Changelog | ||
The project's changelog is available [here](/changelog.md) | ||
## License | ||
[MIT](/LICENSE) | ||
[MIT](/LICENSE) |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
@@ -0,0 +0,0 @@ 'use strict' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
272089
26
4380
63
7
7