0

I want to pass my variable from python to my php heres my code from python :

from pyimagesearch.localbinarypatterns import LocalBinaryPatterns
from imutils import paths
import argparse
import cv2
from keras.models import model_from_json
import os
import PIL.Image
import numpy as np
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from keras.optimizers import adam
import matplotlib.pyplot as plt
from keras.layers import Dropout
from keras.layers import Dropout
from keras import regularizers
from sklearn.model_selection import train_test_split
import random
def main():
 #processANN
 return prediction[0]

def crop_dulu():
    image = PIL.Image.open("D:\projectBram\public\daging.jpg")

    center=center_image(image)
    left,top,right,bottom = center
    center_cropped = crop(image,left,top,right,bottom)
    center_cropped.save("D:\projectBram\public\storage\pork\daging123.jpg")
    pred=main()
    return pred



def center_image(image):
    width, height = image.size
    left= width /4
    top = height /4
    right = 3 * width / 4
    bottom = 3 * height / 4
    return ((left,top,right,bottom))

def crop(image,left,top,right,bottom):
    cropped= image.crop((left,top,right,bottom))
    return cropped

if __name__ == "__main__":
    pred1=crop_dulu()
    if pred1==0:
        print("pork")
    else:
        print("beef")

i want to send pred variable to my php and i add my full python code And here's my php program :

 <?php 
ob_start();
passthru('python D:/local-binary-patterns/haha2.py');
$command = ob_get_clean();
echo $command;

I've tried to use escapeshellcmd and it didn't work either thank you so much for your answer

bramasta vikana
  • 274
  • 1
  • 12

1 Answers1

1

Check https://www.php.net/manual/en/function.passthru.php:

passthru does not return anything, so $command will always be empty.

your python script outputs something, to stdout, so you need an output buffer:

ob_start()
passthru('python D:/local-binary-patterns/haha1.py');
$command = ob_get_clean()

Now $command should contain all data printed by you python script.

simirimia
  • 409
  • 2
  • 9
  • it doesnt work, i ve tried your code but it doesnt work – bramasta vikana Jan 06 '20 at 10:41
  • You made sure that the python script works? If you run it from console it prints out the result you expect? How do you execute the PHP code? Console or Web Server? If Server, did you made sure that the server process is allowed and able to execute the python script? Does that user has the python command in his path? – simirimia Jan 06 '20 at 10:54
  • i ran it from console and it work, and i exe my php file through browser, how coud i know my server allowed to execute python script?, btw my python script is run but it doesnt return a value to php – bramasta vikana Jan 06 '20 at 11:06
  • With user rights management I can't help you since you obviously run Windows and I do not know much about that. I just tried it with little script on my machine, it should work that way. The python example you've shown in your question is that the actual code? What my code does is take the data the python script prints to stdout (meaning collection all the stuff the print() statements in python produce) and assigning it to $command. It does not collect return values. This is something different. – simirimia Jan 06 '20 at 11:23
  • its just a piece for my python code, my python code is to recognise meat image and output of it is 1 or 0 i want to pass this into my php and send itu to android – bramasta vikana Jan 06 '20 at 13:58
  • Could you please add the exact line with passthru of you PHP script and the complete if __name__ == '__main__' block of your python script? – simirimia Jan 06 '20 at 17:17
  • your code is work when i delete my ANN function, i try to print a simple variable and it work , but why it doesnt work when i add my ANN function – bramasta vikana Jan 07 '20 at 02:51