【发布时间】: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