Tesseract.js offline: ReferenceError: Tesseract is

2019-08-19 03:58发布

问题:

I have created a local web-page and I will use default Tesseract.js (copied from CDN tesseract.js, worker.js, eng.traineddata.gz, index.js). Here is the code I use:

In head:

<script scr="js/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 + '/js/worker.js',
    langPath: path + '/js/eng.traineddata.gz',
    corePath: path + '/js/index.js'
    });
    })();
</script>

Code that runs on the button :

var img = document.getElementById('UploadAndPreview');
    Tesseract.recognize(img)
    .progress(function(result) {
            document.getElementById("ocr_status")
                    .innerText = result["status"] + " (" +
                        (result["progress"] * 100) + "%)";
    })
    .then(function(result){
        console.log('Something is going on');
        console.log(result);
    })

After I open the page locally in browser, I get an error:

ReferenceError: Tesseract is not defined

What to do to fix it?