我使用Ghostscript将PDF文件中的图像剥离为jpg并运行Tesseract以保存这样的txt内容:
代码:
$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中出现“无法打开的流”错误。
我做错了什么?
答案 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,了解设置HOME
或PATH
是否对此进行排序?