【问题标题】:PHP Unable to execute Pytesseract in Python via shell_exec()PHP 无法通过 shell_exec() 在 Python 中执行 Pytesseract
【发布时间】:2016-08-29 04:26:23
【问题描述】:

我正在使用 Postman 将 base64 图像发送到我的 Apache 网络服务器上的 PHP 文件。图像始终发送成功。 PHP 脚本执行 python 脚本以从图像中提取文本(使用 Pytesseract/Tesseract-OCR)并将输出发送回 PHP。 (使用 Windows 10,如果这很重要)

前两个打印语句总是在 Postman 中返回,但第三和第四个打印语句不返回。仅当注释掉 pytesseract 行时,最后一个 print 语句才返回。

当我自己运行python脚本时,所有打印语句都成功返回。

Python (test.py)

from PIL import Image 
import pytesseract
import sys

print "Print 1"
print "Print 2"

filename = "test.jpg"
#filename = sys.argv[1]
text = pytesseract.image_to_string(Image.open("Images/"+filename))
print text

#Final print statement appears on POSTMAN only if the tesseract code does not run

a = "Print"
b = 1+2
print a, b

PHP (connection.php)

<?php
 header('Content-type : bitmap; charset=utf-8');

 if(isset($_POST["encoded_string"])){
  $encoded_string = $_POST["encoded_string"];
  $device_name = $_POST["device_name"];

  /*$image_name = $device_name.'.jpg';*/
  $image_name = "test.jpg";
  $decoded_string = base64_decode($encoded_string);

  $path = 'images/'.$image_name;
  $file = fopen($path, 'wb');
  $is_written = fwrite($file, $decoded_string);
  fclose($file);

  $extracted = shell_exec("python test.py $image_name");
  echo $extracted;

 }

 else {
   echo "Failed :(";
 }

?>

我相信问题是能够运行 python 脚本,但是 python 脚本在 PHP 执行时无法执行 tesseract。

【问题讨论】:

  • 在cli中手动运行python文件是否有效(与php相同的用户)?
  • @CharlotteDunois 是的!
  • 有人能真正帮助解决这个问题吗...

标签: php python tesseract python-tesseract


【解决方案1】:

希望你还在,我找到了解决方案here!在 php 脚本中添加这些行,希望它会起作用:

$path = getenv('PATH'); putenv("PATH=$path:/usr/local/bin");

【讨论】:

  • @0248881 :我知道它可能太旧了,但你喜欢我的回答吗?如果是,请投票,这将非常非常有帮助...谢谢!
  • @charlotte-dunois:您认为我的回答有价值吗?如果是这样,请投票,这将非常有帮助......!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-01
  • 2019-02-26
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 2016-07-28
  • 2012-04-09
相关资源
最近更新 更多