2016-08-29 36 views
0

我正在使用Postman将base64映像发送到我的Apache Web服务器上的PHP文件。图像始终成功发送。 PHP脚本执行python脚本以从图像中提取文本(使用Pytesseract/Tesseract-OCR)并将输出发送回PHP。 (使用Windows 10,如果有的话)PHP无法通过shell_exec()执行Python中的Pytesseract()

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

当我自己运行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。

+0

请问,如果你在命令行手动运行Python文件(如相同的用户PHP)的工作? –

+0

@CharlotteDunois是的! – 0248881

+0

有人可以真正帮助解决这个问题吗? – 0248881

回答

0

希望你仍然在上面,我找到了解决方案here!添加论文线路中的PHP脚本,并希望这将工作:

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

@ 0248881:我知道这可能太旧了,但是你喜欢我的回答吗?如果是的话,请upvote,这将是非常非常有益的...谢谢! – Ryan

+0

@ charlotte-dunois:你认为我的回答有价值吗?如果是这样,请upvote,这将是非常有益的...! – Ryan