PHP无法在Python中执行Pytesseract

时间:2016-08-25 02:04:46

标签: php python python-2.7 python-tesseract

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

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

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

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 :(";
 }

?>

0 个答案:

没有答案