在 Laravel 中使用 TesseractOCR
Using TesseractOCR in Laravel
我正在新网站上试用 TesseractOCR。
我已经安装了 Laravel (5.2.4) 的新版本,在我的服务器 (Debian Jessie) 上安装了 Tesseract,并为 Tesseract 安装了 PHP 包装器; tesseract-ocr-for-php.
我已按照所有设置说明进行操作并在应用程序上安装了程序包。
在我的 routes.php
文件中我有:
Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
echo $tesseract->recognize();
});
图像 myimage.jpg
位于 public
文件夹内名为 images
的文件夹中。
当我导航到 example.com/test
时,我得到:
ErrorException in TesseractOCR.php line 235:
file_get_contents(/tmp/75176598.txt): failed to open stream: No such
file or directory
根据 readme.md
,您可以使用 $tesseract->setTempDir('./my-temp-dir');
.
解决该问题
因此,我尝试将 routes.php
文件更改为:
Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
$tesseract->setTempDir('/var/www/tesseract/public/images');
echo $tesseract->recognize();
});
然而,使用不同的文件路径只会给出相同的错误:
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
如何解决这个错误?
我最终发现你不能为图像提供带有 HTML link 的 TesseractOCR,它需要是内部文件路径。
作为asset()
returns a URL,我只是将其替换为public_path()
并且效果很好。
权限或实际包从来没有任何问题,只是您需要提供文件路径而不是 URL。
我正在新网站上试用 TesseractOCR。
我已经安装了 Laravel (5.2.4) 的新版本,在我的服务器 (Debian Jessie) 上安装了 Tesseract,并为 Tesseract 安装了 PHP 包装器; tesseract-ocr-for-php.
我已按照所有设置说明进行操作并在应用程序上安装了程序包。
在我的 routes.php
文件中我有:
Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
echo $tesseract->recognize();
});
图像 myimage.jpg
位于 public
文件夹内名为 images
的文件夹中。
当我导航到 example.com/test
时,我得到:
ErrorException in TesseractOCR.php line 235: file_get_contents(/tmp/75176598.txt): failed to open stream: No such file or directory
根据 readme.md
,您可以使用 $tesseract->setTempDir('./my-temp-dir');
.
因此,我尝试将 routes.php
文件更改为:
Route::get('/test', function () {
$tesseract = new TesseractOCR(asset('images/myimage.jpg'));
$tesseract->setTempDir('/var/www/tesseract/public/images');
echo $tesseract->recognize();
});
然而,使用不同的文件路径只会给出相同的错误:
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
如何解决这个错误?
我最终发现你不能为图像提供带有 HTML link 的 TesseractOCR,它需要是内部文件路径。
作为asset()
returns a URL,我只是将其替换为public_path()
并且效果很好。
权限或实际包从来没有任何问题,只是您需要提供文件路径而不是 URL。