【发布时间】:2021-02-23 18:21:56
【问题描述】:
我正在使用 this 进行 ocr。这是 Tesseract ocr 的包装器(我之前自己安装了 Tesseract)。
起初我通过作曲家下载它,并在 repo 中遵循示例以及关于 SO 本身的几篇文章。并且所有可以显示在网络标签failed to load response data。
我的替代方法是我尝试下载 repo 本身,然后尝试从我的 index.php 调用它,出于测试目的,它位于类 TesseractOCR 所在的同一文件夹中。我尝试使用 repo 中的图像,也尝试在带有简单文本的白色背景图像上使用黑色字母。
This SO 的帖子看起来很有希望,但我不确定带有示例代码的 OP 文件在哪里...
use thiagoalessio\TesseractOCR\TesseractOCR;
//or//require "TesseractOCR.php";//if it's in the same dir as test.php
$content = new TesseractOCR('text.png');
$text = $content->run();
echo $text;
我错过了什么明显的东西吗?任何帮助表示赞赏。
EDIT1:我尝试在 win powershell cli 中使用。通过将text.png 放在安装了tesseract 的目录中,然后以管理员权限调用shell,随后输入tesseract text.png output,这将在同一目录中创建output.txt,并带有来自该图像的识别文本。
所以 tesseract 它的工作,我用 php wrapper 的实现不是。
EDIT2: 忘记加了,页面本身显示:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
不知道为什么会这样。
编辑3:
我的代码:
try{
//use thiagoalessio\TesseractOCR\TesseractOCR;
require "./vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php";
echo $temp;//It's value is set in TesseractOCR.php
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
$temp 变量中设置的值通过状态文件路径可见,那么为什么 TesseractOCRclass 本身不可见?
编辑4:
即使我将绝对路径放在包含类的TesseractOCR.php 中,在include 语句中,它也不起作用。
它抛出这个错误:
Fatal error: Uncaught Error: Class 'TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
This is TesseractOCR.//echoed text from file that holds TesseractOCR class.
包含路径:
include ("C:/xampp/htdocs/myocr/vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php");
如果我使用(在 repo 自述文件中建议,use thiagoalessio\TesseractOCR\TesseractOCR;,那么它会抛出:
Fatal error: Uncaught Error: Class 'thiagoalessio\TesseractOCR\TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
我的问题是:它如何命中测试消息,但不会命中 TesseractOCR 类?
EDIT5:
如果我 require_once "./vendor/autoload.php"; ,它会抛出:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract The current $PATH is C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Eddie\AppData\Local\Microsoft\WindowsApps;;C:\Users\Eddie\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Eddie\AppData\Roaming\npm;C:\Users\Eddie\AppData\Roaming\Composer\vendor\bin;C:\Program Files\heroku\bin in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0 C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\ht in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
【问题讨论】:
-
任何错误消息、部分结果、转储,以及任何可以帮助我们识别问题的东西?尝试将您的代码包含在
try { ... } catch($e) { var_dump($e); }块中,并在此处发布错误消息(如果有)。 -
另外,请确保您的 text.png 存在 (
file_exists('text.png');) 在您当前的工作目录中(检查getcwd();) -
@ΔO'deltazero' 不。我尝试转储包含
new TesseractOCR('text1.png')的变量。没什么,只是网络消息“加载响应数据失败”.. -
尝试使用
try { ... } catch ...块捕获并显示错误。见php.net/manual/en/internals2.opcodes.catch.php -
@ΔO'deltazero' 试过了。没什么新鲜的。我编辑了帖子,再看一遍。