【问题标题】:How To Use Tesseract OCR In PHP如何在 PHP 中使用 Tesseract OCR
【发布时间】:2022-02-10 16:01:40
【问题描述】:

我在我的 Windows 上使用 Xampp。 我想在 PHP 中使用 Tesseract OCR

我的 tesseract 安装在

C:/Program Files/Tesseract OCR/

Xampp 安装在

C:/xampp/

M 使用此代码在我的 php 中执行 teseract

exec("tesseract test.png test.txt");

但它不起作用......

请给我工作代码。

【问题讨论】:

  • 在我的情况下 shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" "E:\\xampp\\htdocs\\images\\' .$file_name.'" out');但不起作用

标签: php tesseract


【解决方案1】:

你的exec 函数应该是

exec("tesseract.exe test.png test.txt");

您在 tesseract 上缺少 .exe 扩展名。我只是犯了同样的错误!

此外,您的 tesseract 也应该安装在您的 xampp 服务器上。

【讨论】:

  • 嘿,我的代码是 shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe" "E:\\xampp\\htdocs\\images\\ '.$file_name.'" out');但它不起作用
  • @KrunalPandya,我已经 5 年多没有使用 Tesseract 了,所以我不太了解它的工作原理。您是否尝试过通过 shell 本身而不是通过 PHP 运行它?这将消除 PHP 的任何问题,并可以为您提供 Tesseract 提出的任何输出消息。祝你好运!
  • 它工作正常,但我有一些问题。我希望数据以数组格式转换为每一行,在我的图像中,有 4 列。
【解决方案2】:
#Debes tener eng.traineddata y el tesseract.exe en la carpeta prueba#
//este codigo funciona en el servidor local
<?php 
$imagen = file_get_contents('http://e-consultaruc.sunat.gob.pe/cl-ti-
itmrconsruc/captcha?accion=image');
file_put_contents('C:\AppServ\www\Sitio AAA\Prueba\foto.jpg', $imagen);
exec("tesseract.exe foto.jpg mitexto2.txt");
?>
<form action="obtener_imagen_captcha.php" method="post">
<input type="submit" value="Aceptar">
</form>

【讨论】:

    【解决方案3】:

    这是一个很好的教程。
    https://chillyfacts.com/convert-image-to-text-optical-character-recognition-ocr-using-php/

    这里的代码对我有用:

        <?php
        shell_exec('"C:\\Program Files\\Tesseract-OCR\\tesseract" "example_image.PNG" out');
        ?>
    

    (第 1 部分)这里是对这段代码的第一部分的解释,它定义了 Tesseract 的安装目录。因此,根据您的计算机文件更改目录。

    (Part 1)
    "C:\\Program Files\\Tesseract-OCR\\tesseract"
    

    (第 2 部分)代码的第二部分定义了图像文件的目录。所以在我的例子中,带有shell_exec() 函数的php 文件与我拥有图像文件example_image.PNG 的目录相同。如果你的图片有不同的目录,可以这样定义C:\\xampp\\htdocs\\images\\example_image.PNG

    (Part 2)
    "example_image.PNG"
    

    (第 3 部分) 第三部分定义输出文本文件的文件名。

    (Part 3)
    out
    

    【讨论】:

      【解决方案4】:

      回复太晚了。但希望能帮助其他人。代码是:

      shell_exec('"C:\\Program Files (x86)\\Tesseract-OCR\\tesseract" "F:\\WebApp5\\htdocs\\imgtotext\\images\\'.$file_name.'" out');
      

      这里 shell_exec('"你的 tesseract 文件位置" "你的图像位置" file_name').谢谢。

      【讨论】:

      • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
      【解决方案5】:

      比放到命令执行器更好的解决方案是这个包装器 -> https://github.com/thiagoalessio/tesseract-ocr-for-php

      如果你不想使用composer,我在这里贴了一些代码来规避t hat,只需在没有composer的情况下搜索php tesseract。

      在撰写本文时,我可以验证包装器至少可以工作到 PHP 8.0.13。

      【讨论】:

        【解决方案6】:

        请在检查前

        运行cmd管理员

        tesseract
        

        如果显示信息

        将您的 test.png 复制到桌面并运行此代码

        cd %userprofile%\desktop
        tesseract test.png test
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-02-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多