Using TesseractOCR in Laravel

- 1 answer

Ad

I'm trialing out using TesseractOCR on a new website.

I've installed a new version of Laravel (5.2.4), installed Tesseract on my server (Debian Jessie) and installed a PHP wrapper for Tesseract; tesseract-ocr-for-php.

I've followed all the setup instructions and installed the package on the application.

In my routes.php file I had:

Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
echo $tesseract->recognize();
});

Where the image myimage.jpg exists inside a folder called images inside the public folder.

When I navigate to example.com/test I get:

ErrorException in TesseractOCR.php line 235: file_get_contents(/tmp/75176598.txt): failed to open stream: No such file or directory

According to the readme.md you can solve that issue by using $tesseract->setTempDir('./my-temp-dir');.

As such I tried changing my routes.php file to:

Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
$tesseract->setTempDir('/var/www/tesseract/public/images');
echo $tesseract->recognize();
});

However that just gives the same error with a different file path:

ErrorException in TesseractOCR.php line 235: file_get_contents(/var/www/tesseract/public/images/1770521095.txt): failed to open stream: No such file or directory

How do I solve this error?

Ad

Answer

Ad

I eventually figured out that you cannot provide TesseractOCR with an HTML link to an image, it needs to be an internal file path.

As asset() returns a URL, I just replaced this with public_path() and it worked just fine.

There was never anything wrong with permissions or the actual package, just that you need to provide a file path rather than a URL.

Ad
source: stackoverflow.com
Ad