php exec()和tesseract go''无法打开输入文件'

时间:2012-04-17 20:40:51

标签: php exec tesseract

我使用Ghostscript将PDF文件中的图像剥离为jpg并运行Tesseract以保存这样的txt内容:

  • Ghostscript位于c:\ engine \ gs \
  • 位于c:\ engine \ tesseract \
  • 中的Tesseract
  • web位于pdf / jpg / txt dir = file / 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替换putenv()引用(c:/ engine / tesseract / tesseract ...)会返回a.m.错误
  • 我在Tesseract文件夹中保留了strip1.jpg并运行了exec(tesseract'c:/engine/tesseract/strip1.jpg'...)返回a.m.错误
  • 离开path / strip1.jpg周围的萎缩者将一个空数组作为消息返回,并且不会创建ocr.txt文件。
  • 将命令直接写入exec()引用而不是使用$ exec不会进行更改。

我做错了什么?

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

PHP中缺少的环境变量可能就是问题所在。请查看my question here,了解设置HOMEPATH是否对此进行排序?