Browse Source

Fix lint error

pull/265/head
Jerome Wu 6 years ago
parent
commit
af05a27d35
  1. 116
      src/common/circularize.js
  2. 46
      src/common/desaturate.js
  3. 144
      src/common/dump.js
  4. 93
      src/common/job.js

116
src/common/circularize.js

@ -5,59 +5,75 @@
// a (circular) DOM-like interface for walking // a (circular) DOM-like interface for walking
// through the data. // through the data.
module.exports = function circularize(page){ module.exports = (iPage) => {
page.paragraphs = [] const page = {
page.lines = [] ...iPage,
page.words = [] paragraphs: [],
page.symbols = [] lines: [],
words: [],
symbols: [],
};
page.blocks.forEach(function(block){ page.blocks.forEach((iBlock) => {
block.page = page; const block = {
...iBlock,
page,
lines: [],
words: [],
symbols: [],
};
block.lines = [] block.paragraphs.forEach((iPara) => {
block.words = [] const para = {
block.symbols = [] ...iPara,
block,
page,
words: [],
symbols: [],
};
block.paragraphs.forEach(function(para){ para.lines.forEach((iLine) => {
para.block = block; const line = {
para.page = page; ...iLine,
paragraph: para,
block,
page,
symbols: [],
};
para.words = [] line.words.forEach((iWord) => {
para.symbols = [] const word = {
...iWord,
line,
paragraph: para,
block,
page,
};
para.lines.forEach(function(line){ word.symbols.forEach((iSym) => {
line.paragraph = para; const sym = {
line.block = block; ...iSym,
line.page = page; word,
line,
paragraph: para,
block,
page,
};
line.symbols = [] sym.line.symbols.push(sym);
sym.paragraph.symbols.push(sym);
line.words.forEach(function(word){ sym.block.symbols.push(sym);
word.line = line; sym.page.symbols.push(sym);
word.paragraph = para; });
word.block = block; word.paragraph.words.push(word);
word.page = page; word.block.words.push(word);
word.symbols.forEach(function(sym){ word.page.words.push(word);
sym.word = word; });
sym.line = line; line.block.lines.push(line);
sym.paragraph = para; line.page.lines.push(line);
sym.block = block; });
sym.page = page; para.page.paragraphs.push(para);
});
sym.line.symbols.push(sym) });
sym.paragraph.symbols.push(sym) return page;
sym.block.symbols.push(sym) };
sym.page.symbols.push(sym)
})
word.paragraph.words.push(word)
word.block.words.push(word)
word.page.words.push(word)
})
line.block.lines.push(line)
line.page.lines.push(line)
})
para.page.paragraphs.push(para)
})
})
return page
}

46
src/common/desaturate.js

@ -1,24 +1,30 @@
// This converts an image to grayscale /* eslint-disable no-bitwise */
/* eslint-disable max-len */
module.exports = function desaturate(image){ // This converts an image to grayscale
var width, height; module.exports = (image) => {
if(image.data){ if (image.data) {
var src = image.data; const src = image.data;
width = image.width, const { width, height } = image;
height = image.height; const dst = new Uint8Array(width * height);
var dst = new Uint8Array(width * height); const srcLength = src.length | 0;
var srcLength = src.length | 0, srcLength_16 = (srcLength - 16) | 0; const srcLength16 = (srcLength - 16) | 0;
let i = 0;
let j = 0;
for (var i = 0, j = 0; i <= srcLength_16; i += 16, j += 4) { for (; i <= srcLength16; i += 16, j += 4) {
// convert to grayscale 4 pixels at a time; eveything with alpha gets put in front of 50% gray // convert to grayscale 4 pixels at a time; eveything with alpha gets put in front of 50% gray
dst[j] = (((src[i] * 77 + src[i+1] * 151 + src[i+2] * 28) * src[i+3]) + ((255-src[i+3]) << 15) + 32768) >> 16 dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16;
dst[j+1] = (((src[i+4] * 77 + src[i+5] * 151 + src[i+6] * 28) * src[i+7]) + ((255-src[i+7]) << 15) + 32768) >> 16 dst[j + 1] = (((src[i + 4] * 77 + src[i + 5] * 151 + src[i + 6] * 28) * src[i + 7]) + ((255 - src[i + 7]) << 15) + 32768) >> 16;
dst[j+2] = (((src[i+8] * 77 + src[i+9] * 151 + src[i+10] * 28) * src[i+11]) + ((255-src[i+11]) << 15) + 32768) >> 16 dst[j + 2] = (((src[i + 8] * 77 + src[i + 9] * 151 + src[i + 10] * 28) * src[i + 11]) + ((255 - src[i + 11]) << 15) + 32768) >> 16;
dst[j+3] = (((src[i+12] * 77 + src[i+13] * 151 + src[i+14] * 28) * src[i+15]) + ((255-src[i+15]) << 15) + 32768) >> 16 dst[j + 3] = (((src[i + 12] * 77 + src[i + 13] * 151 + src[i + 14] * 28) * src[i + 15]) + ((255 - src[i + 15]) << 15) + 32768) >> 16;
}
// finish up
for (; i < srcLength; i += 4, j += 1) {
dst[j] = (((src[i] * 77 + src[i + 1] * 151 + src[i + 2] * 28) * src[i + 3]) + ((255 - src[i + 3]) << 15) + 32768) >> 16;
}
return dst;
} }
for (; i < srcLength; i += 4, ++j) //finish up return null;
dst[j] = (((src[i] * 77 + src[i+1] * 151 + src[i+2] * 28) * src[i+3]) + ((255-src[i+3]) << 15) + 32768) >> 16 // throw { err: 'Invalid ImageData' };
image = dst; };
} else { throw 'Invalid ImageData' }
return image
}

144
src/common/dump.js

@ -1,27 +1,46 @@
module.exports = function DumpLiterallyEverything(Module, base){ // the generated HOCR is excessively indented, so
var ri = base.GetIterator(); // we get rid of that indentation
var blocks = [];
var block, para, textline, word, symbol;
function enumToString(value, prefix){ const deindent = (html) => {
return (Object.keys(Module) const lines = html.split('\n');
.filter(function(e){ return e.substr(0, prefix.length + 1) == prefix + '_' }) if (lines[0].substring(0, 2) === ' ') {
.filter(function(e){ return Module[e] === value }) for (let i = 0; i < lines.length; i += 1) {
.map(function(e){ return e.slice(prefix.length + 1) })[0]) if (lines[i].substring(0, 2) === ' ') {
lines[i] = lines[i].slice(2);
}
} }
}
return lines.join('\n');
};
module.exports = (Module, base) => {
const ri = base.GetIterator();
const blocks = [];
let block;
let para;
let textline;
let word;
let symbol;
ri.Begin() const enumToString = (value, prefix) => (
Object.keys(Module)
.filter(e => (e.substr(0, prefix.length + 1) === `${prefix}_`))
.filter(e => Module[e] === value)
.map(e => e.slice(prefix.length + 1))[0]
);
ri.Begin();
do { do {
if(ri.IsAtBeginningOf(Module.RIL_BLOCK)){ if (ri.IsAtBeginningOf(Module.RIL_BLOCK)) {
var poly = ri.BlockPolygon(); const poly = ri.BlockPolygon();
var polygon = null; let polygon = null;
// BlockPolygon() returns null when automatic page segmentation is off // BlockPolygon() returns null when automatic page segmentation is off
if(Module.getPointer(poly) > 0){ if (Module.getPointer(poly) > 0) {
var n = poly.get_n(), const n = poly.get_n();
px = poly.get_x(), const px = poly.get_x();
py = poly.get_y(), const py = poly.get_y();
polygon = []; polygon = [];
for(var i = 0; i < n; i++){ for (let i = 0; i < n; i += 1) {
polygon.push([px.getValue(i), py.getValue(i)]); polygon.push([px.getValue(i), py.getValue(i)]);
} }
Module._ptaDestroy(Module.getPointer(poly)); Module._ptaDestroy(Module.getPointer(poly));
@ -29,44 +48,39 @@ module.exports = function DumpLiterallyEverything(Module, base){
block = { block = {
paragraphs: [], paragraphs: [],
text: ri.GetUTF8Text(Module.RIL_BLOCK), text: ri.GetUTF8Text(Module.RIL_BLOCK),
confidence: ri.Confidence(Module.RIL_BLOCK), confidence: ri.Confidence(Module.RIL_BLOCK),
baseline: ri.getBaseline(Module.RIL_BLOCK), baseline: ri.getBaseline(Module.RIL_BLOCK),
bbox: ri.getBoundingBox(Module.RIL_BLOCK), bbox: ri.getBoundingBox(Module.RIL_BLOCK),
blocktype: enumToString(ri.BlockType(), 'PT'), blocktype: enumToString(ri.BlockType(), 'PT'),
polygon: polygon polygon,
} };
blocks.push(block) blocks.push(block);
} }
if(ri.IsAtBeginningOf(Module.RIL_PARA)){ if (ri.IsAtBeginningOf(Module.RIL_PARA)) {
para = { para = {
lines: [], lines: [],
text: ri.GetUTF8Text(Module.RIL_PARA), text: ri.GetUTF8Text(Module.RIL_PARA),
confidence: ri.Confidence(Module.RIL_PARA), confidence: ri.Confidence(Module.RIL_PARA),
baseline: ri.getBaseline(Module.RIL_PARA), baseline: ri.getBaseline(Module.RIL_PARA),
bbox: ri.getBoundingBox(Module.RIL_PARA), bbox: ri.getBoundingBox(Module.RIL_PARA),
is_ltr: !!ri.ParagraphIsLtr(),
is_ltr: !!ri.ParagraphIsLtr() };
} block.paragraphs.push(para);
block.paragraphs.push(para)
} }
if(ri.IsAtBeginningOf(Module.RIL_TEXTLINE)){ if (ri.IsAtBeginningOf(Module.RIL_TEXTLINE)) {
textline = { textline = {
words: [], words: [],
text: ri.GetUTF8Text(Module.RIL_TEXTLINE), text: ri.GetUTF8Text(Module.RIL_TEXTLINE),
confidence: ri.Confidence(Module.RIL_TEXTLINE), confidence: ri.Confidence(Module.RIL_TEXTLINE),
baseline: ri.getBaseline(Module.RIL_TEXTLINE), baseline: ri.getBaseline(Module.RIL_TEXTLINE),
bbox: ri.getBoundingBox(Module.RIL_TEXTLINE) bbox: ri.getBoundingBox(Module.RIL_TEXTLINE),
} };
para.lines.push(textline) para.lines.push(textline);
} }
if(ri.IsAtBeginningOf(Module.RIL_WORD)){ if (ri.IsAtBeginningOf(Module.RIL_WORD)) {
var fontInfo = ri.getWordFontAttributes(), const fontInfo = ri.getWordFontAttributes();
wordDir = ri.WordDirection(); const wordDir = ri.WordDirection();
word = { word = {
symbols: [], symbols: [],
choices: [], choices: [],
@ -90,75 +104,55 @@ module.exports = function DumpLiterallyEverything(Module, base){
font_size: fontInfo.pointsize, font_size: fontInfo.pointsize,
font_id: fontInfo.font_id, font_id: fontInfo.font_id,
font_name: fontInfo.font_name, font_name: fontInfo.font_name,
} };
var wc = new Module.WordChoiceIterator(ri); const wc = new Module.WordChoiceIterator(ri);
do { do {
word.choices.push({ word.choices.push({
text: wc.GetUTF8Text(), text: wc.GetUTF8Text(),
confidence: wc.Confidence() confidence: wc.Confidence(),
}) });
} while (wc.Next()); } while (wc.Next());
Module.destroy(wc) Module.destroy(wc);
textline.words.push(word) textline.words.push(word);
} }
var image = null; // let image = null;
// var pix = ri.GetBinaryImage(Module.RIL_SYMBOL) // var pix = ri.GetBinaryImage(Module.RIL_SYMBOL)
// var image = pix2array(pix); // var image = pix2array(pix);
// // for some reason it seems that things stop working if you destroy pics // // for some reason it seems that things stop working if you destroy pics
// Module._pixDestroy(Module.getPointer(pix)); // Module._pixDestroy(Module.getPointer(pix));
if(ri.IsAtBeginningOf(Module.RIL_SYMBOL)){ if (ri.IsAtBeginningOf(Module.RIL_SYMBOL)) {
symbol = { symbol = {
choices: [], choices: [],
image: image, image: null,
text: ri.GetUTF8Text(Module.RIL_SYMBOL), text: ri.GetUTF8Text(Module.RIL_SYMBOL),
confidence: ri.Confidence(Module.RIL_SYMBOL), confidence: ri.Confidence(Module.RIL_SYMBOL),
baseline: ri.getBaseline(Module.RIL_SYMBOL), baseline: ri.getBaseline(Module.RIL_SYMBOL),
bbox: ri.getBoundingBox(Module.RIL_SYMBOL), bbox: ri.getBoundingBox(Module.RIL_SYMBOL),
is_superscript: !!ri.SymbolIsSuperscript(), is_superscript: !!ri.SymbolIsSuperscript(),
is_subscript: !!ri.SymbolIsSubscript(), is_subscript: !!ri.SymbolIsSubscript(),
is_dropcap: !!ri.SymbolIsDropcap(), is_dropcap: !!ri.SymbolIsDropcap(),
} };
word.symbols.push(symbol) word.symbols.push(symbol);
var ci = new Module.ChoiceIterator(ri); const ci = new Module.ChoiceIterator(ri);
do { do {
symbol.choices.push({ symbol.choices.push({
text: ci.GetUTF8Text(), text: ci.GetUTF8Text(),
confidence: ci.Confidence() confidence: ci.Confidence(),
}) });
} while (ci.Next()); } while (ci.Next());
Module.destroy(ci) // Module.destroy(i);
} }
} while (ri.Next(Module.RIL_SYMBOL)); } while (ri.Next(Module.RIL_SYMBOL));
Module.destroy(ri) Module.destroy(ri);
return { return {
text: base.GetUTF8Text(), text: base.GetUTF8Text(),
html: deindent(base.GetHOCRText()), html: deindent(base.GetHOCRText()),
confidence: base.MeanTextConf(), confidence: base.MeanTextConf(),
blocks,
blocks: blocks,
psm: enumToString(base.GetPageSegMode(), 'PSM'), psm: enumToString(base.GetPageSegMode(), 'PSM'),
oem: enumToString(base.oem(), 'OEM'), oem: enumToString(base.oem(), 'OEM'),
version: base.Version(), version: base.Version(),
}
}
// the generated HOCR is excessively indented, so
// we get rid of that indentation
function deindent(html){
var lines = html.split('\n')
if(lines[0].substring(0, 2) === " "){
for (var i = 0; i < lines.length; i++) {
if (lines[i].substring(0,2) === " ") {
lines[i] = lines[i].slice(2)
}
}; };
} };
return lines.join('\n')
}

93
src/common/job.js

@ -1,81 +1,86 @@
const adapter = require('../node/index.js') const adapter = require('../node/');
let jobCounter = 0; let jobCounter = 0;
module.exports = class TesseractJob { module.exports = class TesseractJob {
constructor(instance){ constructor(instance) {
this.id = 'Job-' + (++jobCounter) + '-' + Math.random().toString(16).slice(3, 8) jobCounter += 1;
this.id = `Job-${jobCounter}-${Math.random().toString(16).slice(3, 8)}`;
this._instance = instance; this._instance = instance;
this._resolve = [] this._resolve = [];
this._reject = [] this._reject = [];
this._progress = [] this._progress = [];
this._finally = [] this._finally = [];
} }
then(resolve, reject){ then(resolve, reject) {
if(this._resolve.push){ if (this._resolve.push) {
this._resolve.push(resolve) this._resolve.push(resolve);
}else{ } else {
resolve(this._resolve) resolve(this._resolve);
} }
if(reject) this.catch(reject); if (reject) this.catch(reject);
return this; return this;
} }
catch(reject){
if(this._reject.push){ catch(reject) {
this._reject.push(reject) if (this._reject.push) {
}else{ this._reject.push(reject);
reject(this._reject) } else {
reject(this._reject);
} }
return this; return this;
} }
progress(fn){
this._progress.push(fn) progress(fn) {
this._progress.push(fn);
return this; return this;
} }
finally(fn) { finally(fn) {
this._finally.push(fn) this._finally.push(fn);
return this; return this;
} }
_send(action, payload){
_send(action, payload) {
adapter.sendPacket(this._instance, { adapter.sendPacket(this._instance, {
jobId: this.id, jobId: this.id,
action: action, action,
payload: payload payload,
}) });
} }
_handle(packet){ _handle(packet) {
var data = packet.data; const { data } = packet;
let runFinallyCbs = false; let runFinallyCbs = false;
if(packet.status === 'resolve'){ if (packet.status === 'resolve') {
if(this._resolve.length === 0) console.log(data); if (this._resolve.length === 0) console.log(data);
this._resolve.forEach(fn => { this._resolve.forEach((fn) => {
var ret = fn(data); const ret = fn(data);
if(ret && typeof ret.then == 'function'){ if (ret && typeof ret.then === 'function') {
console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.') console.warn('TesseractJob instances do not chain like ES6 Promises. To convert it into a real promise, use Promise.resolve.');
} }
}) });
this._resolve = data; this._resolve = data;
this._instance._dequeue() this._instance._dequeue();
runFinallyCbs = true; runFinallyCbs = true;
}else if(packet.status === 'reject'){ } else if (packet.status === 'reject') {
if(this._reject.length === 0) console.error(data); if (this._reject.length === 0) console.error(data);
this._reject.forEach(fn => fn(data)) this._reject.forEach(fn => fn(data));
this._reject = data; this._reject = data;
this._instance._dequeue() this._instance._dequeue();
runFinallyCbs = true; runFinallyCbs = true;
}else if(packet.status === 'progress'){ } else if (packet.status === 'progress') {
this._progress.forEach(fn => fn(data)) this._progress.forEach(fn => fn(data));
}else{ } else {
console.warn('Message type unknown', packet.status) console.warn('Message type unknown', packet.status);
} }
if (runFinallyCbs) { if (runFinallyCbs) {
this._finally.forEach(fn => fn(data)); this._finally.forEach(fn => fn(data));
} }
} }
} };

Loading…
Cancel
Save