@ -1,28 +1,30 @@
@@ -1,28 +1,30 @@
var latestJob ;
var Module ;
var base ;
var adapter = { } ;
var latestJob ,
Module ,
base ,
adapter = { } ,
dump = require ( './dump.js' ) ,
desaturate = require ( './desaturate.js' ) ;
function dispatchHandlers ( packet , send ) {
function respond ( status , data ) {
send ( {
jobId : packet . jobId ,
status : status ,
status ,
action : packet . action ,
data : data
} )
data
} ) ;
}
respond . resolve = respond . bind ( this , 'resolve' )
respond . reject = respond . bind ( this , 'reject' )
respond . progress = respond . bind ( this , 'progress' )
respond . resolve = respond . bind ( this , 'resolve' ) ;
respond . reject = respond . bind ( this , 'reject' ) ;
respond . progress = respond . bind ( this , 'progress' ) ;
latestJob = respond ;
try {
if ( packet . action === 'recognize' ) {
handleRecognize ( packet . payload , respond )
handleRecognize ( packet . payload , respond ) ;
} else if ( packet . action === 'detect' ) {
handleDetect ( packet . payload , respond )
handleDetect ( packet . payload , respond ) ;
}
} catch ( err ) {
respond . reject ( err )
@ -32,13 +34,13 @@ exports.dispatchHandlers = dispatchHandlers;
@@ -32,13 +34,13 @@ exports.dispatchHandlers = dispatchHandlers;
exports . setAdapter = function setAdapter ( impl ) {
adapter = impl ;
}
} ;
function handleInit ( req , res ) {
var MIN _MEMORY = 100663296 ;
if ( [ 'chi_sim' , 'chi_tra' , 'jpn' ] . indexOf ( req . options . lang ) != - 1 ) {
if ( [ 'chi_sim' , 'chi_tra' , 'jpn' ] . includes ( req . options . lang ) ) {
MIN _MEMORY = 167772160 ;
}
@ -50,23 +52,17 @@ function handleInit(req, res){
@@ -50,23 +52,17 @@ function handleInit(req, res){
Module = Core ( {
TOTAL _MEMORY : MIN _MEMORY ,
TesseractProgress ( percent ) {
latestJob . progress ( { status : 'recognizing text' , progress : Math . max ( 0 , ( percent - 30 ) / 70 ) } )
latestJob . progress ( { status : 'recognizing text' , progress : Math . max ( 0 , ( percent - 30 ) / 70 ) } ) ;
} ,
onRuntimeInitialized ( ) { }
} )
} ) ;
Module . FS _createPath ( "/" , "tessdata" , true , true )
base = new Module . TessBaseAPI ( )
res . progress ( { status : 'initializing tesseract' , progress : 1 } )
Module . FS _createPath ( "/" , "tessdata" , true , true ) ;
base = new Module . TessBaseAPI ( ) ;
res . progress ( { status : 'initializing tesseract' , progress : 1 } ) ;
}
}
var dump = require ( './dump.js' )
var desaturate = require ( './desaturate.js' )
function setImage ( Module , base , image ) {
var imgbin = desaturate ( image ) ,
width = image . width ,
@ -74,51 +70,55 @@ function setImage(Module, base, image){
@@ -74,51 +70,55 @@ function setImage(Module, base, image){
var ptr = Module . allocate ( imgbin , 'i8' , Module . ALLOC _NORMAL ) ;
base . SetImage ( Module . wrapPointer ( ptr ) , width , height , 1 , width ) ;
base . SetRectangle ( 0 , 0 , width , height )
base . SetRectangle ( 0 , 0 , width , height ) ;
return ptr ;
}
function loadLanguage ( req , res , cb ) {
var lang = req . options . lang ;
var lang = req . options . lang ,
langFile = lang + '.traineddata' ;
if ( ! Module . _loadedLanguages ) Module . _loadedLanguages = { } ;
if ( lang in Module . _loadedLanguages ) return cb ( ) ;
adapter . getLanguageData ( req , res , function ( data ) {
res . progress ( { status : 'loading ' + lang + '.traineddata' , progress : 0 } )
Module . FS _createDataFile ( 'tessdata' , lang + ".traineddata" , data , true , false ) ;
res . progress ( { status : 'loading ' + langFile , progress : 0 } ) ;
Module . FS _createDataFile ( 'tessdata' , langFile , data , true , false ) ;
Module . _loadedLanguages [ lang ] = true ;
res . progress ( { status : 'loading ' + lang + '.traineddata' , progress : 1 } )
cb ( )
res . progress ( { status : 'loading ' + langFile , progress : 1 } ) ;
cb ( ) ;
} )
}
function handleRecognize ( req , res ) {
handleInit ( req , res )
handleInit ( req , res ) ;
loadLanguage ( req , res , ( ) => {
var options = req . options ;
loadLanguage ( req , res , function ( ) {
var lang = req . options . lang ;
function progressUpdate ( progress ) {
res . progress ( { status : 'initializing api' , progress : progress } ) ;
}
res . progress ( { status : 'initializing api' , progress : 0 } )
base . Init ( null , lang )
res . progress ( { status : 'initializing api' , progress : 0.3 } )
progressUpdate ( 0 ) ;
base . Init ( null , req . options . lang ) ;
progressUpdate ( . 3 ) ;
var options = req . options ;
for ( var option in options ) {
if ( options . hasOwnProperty ( option ) ) {
base . SetVariable ( option , options [ option ] ) ;
}
}
res . progress ( { status : 'initializing api' , progress : 0.6 } )
progressUpdate ( . 6 ) ;
var ptr = setImage ( Module , base , req . image ) ;
res . progress ( { status : 'initializing api' , progress : 1 } )
progressUpdate ( 1 ) ;
base . Recognize ( null )
base . Recognize ( null ) ;
var result = dump ( Module , base )
var result = dump ( Module , base ) ;
base . End ( ) ;
Module . _free ( ptr ) ;
@ -129,40 +129,34 @@ function handleRecognize(req, res){
@@ -129,40 +129,34 @@ function handleRecognize(req, res){
function handleDetect ( req , res ) {
handleInit ( req , res )
handleInit ( req , res ) ;
req . options . lang = 'osd' ;
loadLanguage ( req , res , function ( ) {
loadLanguage ( req , res , ( ) => {
base . Init ( null , 'osd' ) ;
base . SetPageSegMode ( Module . PSM _OSD _ONLY ) ;
base . Init ( null , 'osd' )
base . SetPageSegMode ( Module . PSM _OSD _ONLY )
var ptr = setImage ( Module , base , req . image ) ,
results = new Module . OSResults ( ) ;
var ptr = setImage ( Module , base , req . image ) ;
var results = new Module . OSResults ( ) ;
var success = base . DetectOS ( results ) ;
if ( ! success ) {
if ( ! base . DetectOS ( results ) ) {
base . End ( ) ;
Module . _free ( ptr ) ;
res . reject ( "failed to detect os" )
res . reject ( "Failed to detect OS" ) ;
} else {
var charset = results . get _unicharset ( )
var best = results . get _best _result ( )
var oid = best . get _orientation _id ( ) ,
var best = results . get _best _result ( ) ,
oid = best . get _orientation _id ( ) ,
sid = best . get _script _id ( ) ;
var result = {
base . End ( ) ;
Module . _free ( ptr ) ;
res . resolve ( {
tesseract _script _id : sid ,
script : charset . get _script _from _script _id ( sid ) ,
script : results . get _uni charset( ) . get _script _from _script _id ( sid ) ,
script _confidence : best . get _sconfidence ( ) ,
orientation _degrees : [ 0 , 270 , 180 , 90 ] [ oid ] ,
orientation _confidence : best . get _oconfidence ( )
} ) ;
}
base . End ( ) ;
Module . _free ( ptr ) ;
res . resolve ( result )
}
} )
} ) ;
}