0

I am using Postman to send a base64 image to the PHP file on my Apache web-server. The image is always sent successfully. The PHP script executes a python script to extract text from the image (Using Pytesseract/Tesseract-OCR) and sends the output back to PHP. (Using Windows 10, if that matters)

The first two print statements are always returned in Postman but the third and fourth print statements do not return. The last print statement returns only when the pytesseract line is commented out.

When I run the python script by itself, all print statements return successfully.

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

?>

I believe the problem is able to run the python script but the python script isn't able to execute tesseract when it has been executed by PHP.

3
  • Does it work if you run the python file manually in cli (as the same user as php)? Commented Aug 29, 2016 at 4:40
  • @CharlotteDunois Yes! Commented Aug 31, 2016 at 5:22
  • can someone actually help with this issue or not... Commented Sep 1, 2016 at 11:07

1 Answer 1

2

Hopefully you're still on it, i found the solution here ! add theses lines in the php script, and hopefully it will work:

$path = getenv('PATH'); putenv("PATH=$path:/usr/local/bin");
Sign up to request clarification or add additional context in comments.

2 Comments

@0248881 : i know it might be too old, but did you like my answer ? if yes, please upvote, that would be very, very helpful...Thanks !
@charlotte-dunois : do you consider my answer valuable ? If so, please upvote, that would be very helpful...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.