Using TesseractOCR in Laravel
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?
Answer
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.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?