在tesseract.js中加载离线lang数据

时间:2017-06-09 05:37:17

标签: javascript tesseract.js

我正在尝试将自己训练过的数据加载到tesseract.js。当文件放在本地时,我试图离线加载所有内容。我使用的代码如下所示:

<script src="tesseract.js"></script>

<script>
//Set the worker, core and lang to local files
(function() {
var path = (function() { //absolute path
    var pathArray = window.location.pathname.split( '/' );
    pathArray.pop(); //Remove the last ("**.html")
    return window.location.origin + pathArray.join("/");
})();
console.log(path);

window.Tesseract = Tesseract.create({
    workerPath: path + '/worker.js',
    //langPath: path + '/traineddata/',
    corePath: path + '/index.js',
});
})();
</script>

<script>
function recognizeFile(file){
    document.querySelector("#log").innerHTML = ''

    Tesseract.recognize(file, {
        lang: document.querySelector('#langsel').value
    })
        .progress(function(packet){
            console.info(packet)
            progressUpdate(packet)

        })
        .then(function(data){
            console.log(data)
            progressUpdate({ status: 'done', data: data })
        })
}
</script>

如果没有设置langPath,上面的代码工作正常,但是当我将langPath指向本地文件夹时,Tesseract无法加载任何带有以下错误的内容:

Failed loading language 'eng'
Tesseract couldn't load any languages!

...

AdaptedTemplates != NULL:Error:Assert failed:in file ../classify/adaptmatch.cpp, line 190
SCRIPT0: abort() at Error
   at Na (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:36:24)
   at ka (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:511:83)
   at Module.de._abort (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:377:166)
   at $L (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:387:55709)
   at jpa (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:392:22274)
   at lT (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:391:80568)
   at mT (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:391:80698)
   at BS (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:391:69009)
   at bP (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:387:110094)
   at jT (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:391:80280)
   at RJ (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:387:19088)
   at QJ (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:387:17789)
   at zI (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:403:90852)
   at tw (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:49079)
   at rw (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:48155)
   at lw (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:39071)
   at _v (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:22565)
   at aw (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:24925)
   at cw (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:401:27237)
   at oj (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:386:24689)
   at Og (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:386:10421)
   at $.prototype.Recognize (file:///C:/Users/user/Downloads/tesseract.js-master/dist/index.js:558:379)
   at Anonymous function (file:///C:/Users/user/Downloads/tesseract.js-master/dist/worker.js:8814:9)
   at Anonymous function (file:///C:/Users/user/Downloads/tesseract.js-master/dist/worker.js:8786:9)
   at xhr.onerror (file:///C:/Users/user/Downloads/tesseract.js-master/dist/worker.js:8429:9)
If this abort() is unexpected, build with -s ASSERTIONS=1 which can give more information.
index.js (8,1)

我在/ traineddata文件夹中同时拥有eng.traineddata和eng.traineddata.gz,因为显然跳过了ungzip进程。有什么我忽略的吗?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

我知道这个问题很老但最近我需要在我的一个项目中使用Tesseract.js。我需要在本地加载数据文件,所以这就是我所做的。

而不是创建新员工。我修改了可用的默认工作程序选项。所以我没有使用Tesseract.createWorker并直接设置路径而是使用识别。

 Tesseract.workerOptions.langPath = 
           window.location.origin // take protocol://domain.com part
           + "/scripts/tesseract/dist/"; // location of data files

 //you could set core and worker paths too but I didn't need it
 Tesseract.workerOptions.workerPath = 
           window.location.origin // take protocol://domain.com part
           + "/scripts/tesseract/dist/worker.js"; // location of worker.js

 //you could set core and worker paths too but I didn't need it
 Tesseract.workerOptions.corePath = 
           window.location.origin // take protocol://domain.com part
           + "/scripts/tesseract/dist/index.js"; // location of index.js

//example lang path would be protocol://domain.com/scripts/tesseract/dist/

通过这样做,我保持工人和核心路径不变,指向默认CDN。

PS:当使用本地worker.js和core.js路径时,我在worker.js中的postMessage()上收到了未被捕获的错误。这就是为什么我只使用langData的本地路径。我仍然不知道如何解决它或为什么会发生这种情况。但是,您可以关注此问题herehere

答案 1 :(得分:0)

我通过从tesseract.js-core 0.1.0提取corePath文件解决了该问题

window.Tesseract = Tesseract.create({
   workerPath: window.location.origin + "/tesseract/worker.js", //tesseract.js-1.0.10
   langPath: window.location.origin + "/tesseract/",
   corePath: window.location.origin + "/tesseract/index.js", //tesseract.js-core-0.1.0
});

还有https://github.com/naptha/tessdata/tree/gh-pages/3.02的语言gz