1

我使用 Ghostscript 将 PDF 文件中的图像剥离为 jpg 并运行 Tesseract 以保存 txt 内容,如下所示:

  • Ghostscript 位于 c:\engine\gs\
  • Tesseract 位于 c:\engine\tesseract\
  • 网页位于 pdf/jpg/txt 目录 = 文件/tmp/

代码:

$pathgs = "c:\\engine\\gs\\";
$pathtess = "c:\\engine\\tesseract\\";
$pathfile = "file/tmp/"

// Strip images
putenv("PATH=".$pathgs);
$exec = "gs -dNOPAUSE -sDEVICE=jpeg -r300 -sOutputFile=".$pathfile."strip%d.jpg ".$pathfile."upload.pdf -q -c quit";
shell_exec($exec);

// OCR
putenv("PATH=".$pathtess);
$exec = "tesseract.exe '".$pathfile."strip1.jpg' '".$pathfile."ocr' -l eng";
exec($exec, $msg);
print_r($msg);
echo file_get_contents($pathfile."ocr.txt");

剥离图像(只有 1 页)工作正常,但 Tesseract 回应:

Array
  (
    [0] => Tesseract Open Source OCR Engine v3.01 with Leptonica
    [1] => Cannot open input file: 'file/tmp/strip1.jpg'
  )

并且没有生成 ocr.txt 文件,从而导致 PHP 中出现“打开流失败”错误。

  • 将 strip1.jpg 复制到 c:/engine/tesseract/ 文件夹并从命令运行 Tesseract (tesseract strip1.jpg ocr.txt -l eng) 运行没有任何问题。
  • 用 exec(c:/engine/tesseract/tesseract ... ) 替换 putenv() 引用会返回 am 错误
  • 我将 strip1.jpg 保存在 Tesseract 文件夹中并运行 exec(tesseract 'c:/engine/tesseract/strip1.jpg' ... ) 返回 am 错误
  • 去掉 path/strip1.jpg 周围的撇号会返回一个空数组作为消息,并且不会创建 ocr.txt 文件。
  • 将命令直接写入 exec() 引用而不是使用 $exec 不会进行更改。

我究竟做错了什么?

4

2 回答 2

1

Halfer,你让我开心:-)

与您帖子中描述的方式不完全相同,但如下所示:

$path = str_replace("index.php", "../".$pathfile, $_SERVER['SCRIPT_FILENAME']);

$descriptors = array(
   0 => array("pipe", "r"),
   1 => array("pipe", "w"),
   2 => array("pipe", "w")
);
$cwd = $pathtess;
$command = "tesseract ".$path."strip1.jpg" ".$path."ocr -l eng";

$process = proc_open($command, $descriptors, $pipes, $cwd);

if(is_resource($process)) {
    fclose($pipes[0]);
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);
}

echo file_get_contents($path."ocr.txt");
于 2012-04-19T21:20:12.617 回答
0

也许 PHP 中缺少的环境变量是这里的问题。看看我的问题,看看是否设置HOMEPATH解决这个问题?

于 2012-04-19T17:50:27.037 回答