Need help with sending p5.Image to Tesseract.js

After correctly loading an image I get the below error message when trying to recognize text on an image with Tesseract.js.

tesseract.js:356 Uncaught DOMException: Failed to execute 'postMessage' on 'Worker': HTMLCanvasElement object could not be cloned. at https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:356:25 at loadImage (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:415:16) at Object.sendPacket (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:354:5) at TesseractJob._send (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:549:21) at https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:643:9 at Array. (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:673:5) at TesseractWorker._dequeue (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:683:19) at TesseractWorker._delay (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:675:32) at TesseractWorker.recognize (https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js:635:16) at recognizeFile (http://localhost:8080/:41:19) (anonymous) @ tesseract.js:356 loadImage @ tesseract.js:415 sendPacket @ tesseract.js:354 _send @ tesseract.js:549 (anonymous) @ tesseract.js:643 (anonymous) @ tesseract.js:673 _dequeue @ tesseract.js:683 _delay @ tesseract.js:675 recognize @ tesseract.js:635 recognizeFile @ (index):41 keyPressed @ (index):35 e._onkeydown @ p5.min.js:9

This is my code:

<title>Test</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/p5.min.js" type="text/javascript"></script>
<script src='https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js' type="text/javascript"></script>

<script>

  var img;

  function preload() 
  {
    // http://cheesiemack.com/wp/wp-content/uploads/2012/06/CT-872NED.jpg
    img = loadImage("licenseplate.jpg");
  }

  function setup() 
  {
    canvas = createCanvas (windowWidth, windowHeight);
  }

  function draw() 
  {
    background (255, 255, 0);

    image (img, 0, 0);
  }

  function keyPressed() 
  {
    recognizeFile (img);
  }


  function recognizeFile (img)
  {
    Tesseract.recognize (img, {lang: 'eng'})
    .then (function(data) {console.log (data.text)})
  }


  function windowResized() 
  {
    resizeCanvas (windowWidth, windowHeight);
  }

</script>

Answers

  • edited August 2017 Answer ✓
  • edited August 2017 Answer ✓
    • I've refactored your sketch to use my approach. B-)
    • And it's available online here: http://Bl.ocks.org/GoSubRoutine/241b4070e20a13f1c3dd8f852390baa0
    • In order to ignite the OCR job over the license plate image, just click on it.
    • You're gonna need to hit F12 to open browser's console tab in order to see the entire progress and the final results.
    • And below's the file pair "index.html" & "sketch.js", so you can run the program locally as well:

    index.html:

    <script async src=http://CDN.JSDelivr.net/npm/p5></script>
    <script defer src=http://CDN.JSDelivr.net/gh/naptha/tesseract.js/dist/tesseract.min.js></script>
    <script defer src=sketch.js></script>
    

    sketch.js:

    /**
     * Tesseract License Plate OCR (v1.0.2)
     * MarcoHeleno & GoToLoop (2017-Feb-23)
     *
     * Forum.Processing.org/two/discussion/23878/
     * need-help-with-sending-p5-image-to-tesseract-js#Item_2
     *
     * Bl.ocks.org/GoSubRoutine/241b4070e20a13f1c3dd8f852390baa0
     */
    
    "use strict";
    
    const HTTP = 'http:' + '//',
          PROX = 'CORS-Anywhere.HerokuApp.com/',
          SITE = 'CheesieMack.com/',
          FOLD = 'wp/wp-content/uploads/2012/06/',
          FILE = 'CT-872NED.jpg',
          PATH = HTTP + PROX + SITE + FOLD + FILE,
          LOCAL = false;
    
    let img;
    
    function preload() {
      img = loadImage(LOCAL && FILE || PATH, console.info);
    }
    
    function setup() {
      createCanvas(img.width, img.height).mousePressed(() => ocr(img.canvas));
      background(img);
    }
    
    function ocr(imageLike) {
      Tesseract.recognize(imageLike).progress(print).then(logFoundWords);
    }
    
    function logFoundWords(json) {
      console.table(json.lines);
      print(json);
    }
    

    P.S.: I had to use http://CORS-Anywhere.HerokuApp.com in order to load the remote image "CT-872NED.jpg" from http://CheesieMack.com, b/c it wasn't CORS-enabled there! :-O

  • edited August 2017

    @GotoLoop, thanks for taking the time to look into this and giving me a such detailed explanation! Much appreciated!

Sign In or Register to comment.