tesseract.js-utils
Advanced tools
Comparing version 1.0.0-beta.1 to 1.0.0-beta.2
{ | ||
"name": "tesseract.js-utils", | ||
"version": "1.0.0-beta.1", | ||
"version": "1.0.0-beta.2", | ||
"description": "Utilities for tesseract.js", | ||
@@ -45,3 +45,3 @@ "main": "src/index.node.js", | ||
"mocha": "^5.2.0", | ||
"tesseract.js-core": "^2.0.0-beta.4", | ||
"tesseract.js-core": "^2.0.0-beta.5", | ||
"testcafe": "^0.23.2", | ||
@@ -52,2 +52,4 @@ "webpack": "^4.25.1", | ||
"dependencies": { | ||
"bmp-js": "^0.1.0", | ||
"file-type": "^10.5.0", | ||
"idb-keyval": "^3.1.0", | ||
@@ -54,0 +56,0 @@ "node-fetch": "^2.3.0", |
@@ -1,2 +0,2 @@ | ||
const { set, get } = require('idb-keyval'); | ||
const { set, get, del } = require('idb-keyval'); | ||
@@ -6,2 +6,7 @@ module.exports = { | ||
writeCache: set, | ||
deleteCache: del, | ||
checkCache: path => ( | ||
get(path) | ||
.then(v => typeof v !== 'undefined') | ||
), | ||
}; |
const util = require('util'); | ||
const fs = require('fs'); | ||
const readFile = util.promisify(fs.readFile); | ||
const writeFile = util.promisify(fs.writeFile); | ||
module.exports = { | ||
readCache: uri => readFile(uri), | ||
writeCache: (uri, data) => writeFile(uri, data), | ||
readCache: util.promisify(fs.readFile), | ||
writeCache: util.promisify(fs.writeFile), | ||
deleteCache: path => ( | ||
util.promisify(fs.unlink)(path) | ||
.catch(() => {}) | ||
), | ||
checkCache: path => ( | ||
util.promisify(fs.access)(path, fs.F_OK) | ||
.then(err => (err === null)) | ||
.catch(() => false) | ||
), | ||
}; |
@@ -0,1 +1,3 @@ | ||
const cache = require('./common/browser/cache'); | ||
module.exports = { | ||
@@ -5,5 +7,6 @@ loadLang: require('./loadLang')({ | ||
gunzip: require('./common/browser/gunzip'), | ||
...require('./common/browser/cache'), | ||
...cache, | ||
}), | ||
readImage: require('./readImage'), | ||
cache, | ||
}; |
@@ -0,1 +1,3 @@ | ||
const cache = require('./common/node/cache'); | ||
module.exports = { | ||
@@ -5,5 +7,6 @@ loadLang: require('./loadLang')({ | ||
gunzip: require('./common/node/gunzip'), | ||
...require('./common/node/cache'), | ||
...cache, | ||
}), | ||
readImage: require('./readImage'), | ||
cache, | ||
}; |
@@ -5,3 +5,3 @@ const handleLang = modules => ({ | ||
cachePath, | ||
cache, | ||
cacheMethod, | ||
lang, | ||
@@ -14,3 +14,3 @@ }) => (data) => { | ||
} catch (err) { | ||
// Do nothing | ||
// TODO: Do some error handling here. | ||
} | ||
@@ -20,5 +20,7 @@ } | ||
} | ||
if (cache) { | ||
modules.writeCache(`${cachePath || '.'}/${lang}.traineddata`, data); | ||
if (['write', 'refresh', undefined].includes(cacheMethod)) { | ||
return modules.writeCache(`${cachePath || '.'}/${lang}.traineddata`, data) | ||
.then(() => data); | ||
} | ||
return data; | ||
@@ -30,5 +32,11 @@ }; | ||
cachePath, | ||
cacheMethod, | ||
...options | ||
}) => lang => ( | ||
modules.readCache(`${cachePath || '.'}/${lang}.traineddata`) | ||
}) => (lang) => { | ||
let { readCache } = modules; | ||
if (['refresh', 'none'].includes(cacheMethod)) { | ||
readCache = () => Promise.resolve(); | ||
} | ||
return readCache(`${cachePath || '.'}/${lang}.traineddata`) | ||
.then((data) => { | ||
@@ -38,3 +46,5 @@ if (typeof data === 'undefined') { | ||
} | ||
return handleLang(modules)({ cachePath, lang, ...options })(data); | ||
return handleLang(modules)({ | ||
cachePath, cacheMethod, lang, ...options, | ||
})(data); | ||
}) | ||
@@ -46,5 +56,7 @@ .catch(() => ( | ||
.then(buf => modules.gunzip(new Uint8Array(buf))) | ||
.then(handleLang(modules)({ cachePath, lang, ...options })) | ||
)) | ||
); | ||
.then(handleLang(modules)({ | ||
cachePath, cacheMethod, lang, ...options, | ||
})) | ||
)); | ||
}; | ||
@@ -66,3 +78,7 @@ /** | ||
* @param {string} dataPath - path to store data in mem | ||
* @param {boolean} cache - true for caching | ||
* @param {string} cacheMethod - method of cache invaliation, should one of following options: | ||
* write: read cache and write back (default option) | ||
* readOnly: read cache and not to write back (if cache does not exist, it is the same as none) | ||
* refresh: not to read cache and write back | ||
* none: not to read cache and not to write back | ||
* | ||
@@ -69,0 +85,0 @@ */ |
@@ -1,29 +0,49 @@ | ||
module.exports = (TessModule, buf) => { | ||
const ptr = TessModule._malloc(buf.length * Uint8Array.BYTES_PER_ELEMENT); | ||
TessModule.HEAPU8.set(buf, ptr); | ||
const pix = TessModule._pixReadMem(ptr, buf.length); | ||
if (TessModule.getValue(pix + (7 * 4), 'i32') === 0) { | ||
/* | ||
* Set a yres default value to prevent warning from tesseract | ||
* See kMinCredibleResolution in tesseract/src/ccstruct/publictypes.h | ||
*/ | ||
TessModule.setValue(pix + (7 * 4), 70, 'i32'); | ||
const bmp = require('bmp-js'); | ||
const fileType = require('file-type'); | ||
module.exports = (TessModule, iBuf) => { | ||
const buf = Buffer.from(iBuf); | ||
const type = fileType(buf); | ||
let bytesPerPixel = 0; | ||
let data = null; | ||
let pix = null; | ||
let w = 0; | ||
let h = 0; | ||
/* | ||
* Although leptonica should support reading bmp, there is a bug of "compressed BMP files". | ||
* As there is no solution, we need to use bmp-js for now. | ||
* @see https://groups.google.com/forum/#!topic/tesseract-ocr/4mPD9zTxdxE | ||
*/ | ||
if (type && type.mime === 'image/bmp') { | ||
const bmpBuf = bmp.decode(buf); | ||
data = TessModule._malloc(bmpBuf.data.length * Uint8Array.BYTES_PER_ELEMENT); | ||
TessModule.HEAPU8.set(bmpBuf.data, data); | ||
w = bmpBuf.width; | ||
h = bmpBuf.height; | ||
bytesPerPixel = 4; | ||
} else { | ||
const ptr = TessModule._malloc(buf.length * Uint8Array.BYTES_PER_ELEMENT); | ||
TessModule.HEAPU8.set(buf, ptr); | ||
pix = TessModule._pixReadMem(ptr, buf.length); | ||
if (TessModule.getValue(pix + (7 * 4), 'i32') === 0) { | ||
/* | ||
* Set a yres default value to prevent warning from tesseract | ||
* See kMinCredibleResolution in tesseract/src/ccstruct/publictypes.h | ||
*/ | ||
TessModule.setValue(pix + (7 * 4), 70, 'i32'); | ||
} | ||
[w, h] = Array(2).fill(0) | ||
.map((v, idx) => ( | ||
TessModule.getValue(pix + (idx * 4), 'i32') | ||
)); | ||
} | ||
const [w, h, d, spp, wpl, refcount, xres, yres, informat, special] = Array(10).fill(0) | ||
.map((v, idx) => ( | ||
TessModule.getValue(pix + (idx * 4), 'i32') | ||
)); | ||
return { | ||
w, | ||
h, | ||
d, | ||
spp, | ||
wpl, | ||
refcount, | ||
xres, | ||
yres, | ||
informat, | ||
special, | ||
data: pix, | ||
bytesPerPixel, | ||
data, | ||
pix, | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6806
177
6
+ Addedbmp-js@^0.1.0
+ Addedfile-type@^10.5.0
+ Addedbmp-js@0.1.0(transitive)
+ Addedfile-type@10.11.0(transitive)