首页 > 解决方案 > 用于 php 的 OCR 包装器

问题描述

我将用于ocr。这是 Tesseract ocr 的包装器(我以前安装了 Tesseract 本身)。

起初,我通过 composer 下载了它,并在 repo 中遵循了示例以及 SO 本身的几篇文章。并且所有可以显示的都在网络选项卡failed to load response data中。

我的替代方法是我尝试下载 repo 本身,然后尝试从我index.php那里调用它,用于测试目的,它位于类所在的同一文件夹中TesseractOCR。我尝试使用 repo 中的图像,也尝试在带有简单文本的白色背景图像上使用黑色字母。

这篇 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 包装器的实现不是。

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值通过状态文件路径可见,那么为什么TesseractOCR类本身不可见呢?

Edit4: 即使我将TesseractOCR.php包含类的绝对路径放在包含语句中,它也不起作用。它抛出这个错误:

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: 如果 i 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

顺便说一句,我将它的补丁添加到 env 变量中: 环境路径

标签: phpocrtesseract

解决方案


我解决了!我的问题是我不知道 php 中的自动加载器。帮助我的链接是这个

我的项目结构是这样的:

  1. 创建项目文件夹 myocr.
  2. 在之前,下载最新的稳定版Tesseract并安装它。
  3. 根据您的系统,您可能需要为系统环境变量添加值。你需要在这里做

控制板

然后

p1

然后

p2

然后

p3

我假设它通过提供的图像自我解释。

  1. 接下来是通过 composer获取TesseractOCR :
    composer require thiagoalessio/tesseract_ocr
  1. 最后,在 repo 中使用代码示例之前,您需要调用 autoloader。索引.php:
    require_once('./vendor/autoload.php');//<-This!
    use thiagoalessio\TesseractOCR\TesseractOCR;
    
    $content = new TesseractOCR('text1.png');
    $text = $content->run();
    echo $text;

这对我有用。要照顾的关键是目录结构。 localhost > myocr > index.php使用它的代码,在使用作曲家之后,您将获得供应商目录。它的内容。新中的图像路径是图像和图像TesseractOCR('text1.png');所在的目录。index.php


推荐阅读