Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Tesseract.js in a React app

I am working on an app using React. I want to be able to load a pic and then have Tesseract.js convert it to text. I am using react-dropzone to load the image file and I can add the image to page with an img tag. But when I try to run the ocr using Tesseract It gives me this error:

Uncaught SyntaxError: Unexpected token < at blob:http://localhost:3000/ccac34f4-1f4a-4ba6-b455-a44345b71012:1 (anonymous) @ blob:http://localhost:3000/ccac34f4-1f4a-4ba6-b455-a44345b71012:1

One post I read said to use a CDN, but then Tesseract is not included in my build process, which throws an error. So I think that I have to include it to use React.

like image 775
Brian Tatum Avatar asked Nov 03 '17 03:11

Brian Tatum


2 Answers

I had the same issue, and then I had to dig a bit to have it working using the CDN.

Here is what I did, I hope this can help a bit as a workaround:

  1. add <script src='https://cdn.rawgit.com/naptha/tesseract.js/1.0.10/dist/tesseract.js'></script> in index.html inside <head></head>
  2. add var Tesseract = window.Tesseract; in App.js

Reference : https://github.com/naptha/tesseract.js/issues/134

like image 58
Steve Lng C Avatar answered Sep 18 '22 14:09

Steve Lng C


I know it has been a long time for this question, right now [email protected] has fixed the issue, and it can be used with any frameworks like react, vue and angular with no more issues.

P.S. I am one of the contributors for tesseract.js right now, feel free to let me know if you have any issues/requirements. :)

like image 23
Jerome Wu Avatar answered Sep 19 '22 14:09

Jerome Wu