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

4
  • 1
    Looking at php docs, will the backtick operator help? I dont know how safe it is to use though. Commented Jan 6, 2020 at 7:55
  • Does this answer your question: stackoverflow.com/questions/28780729/… Commented Jan 6, 2020 at 8:11
  • no it doesnt ive tried to do it but idoens work Commented Jan 6, 2020 at 8:39
  • For the sake of good coding habbits: omit the closing ?>. For reasons check here: stackoverflow.com/a/4499749/8247892 Commented Jan 6, 2020 at 11:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

it doesnt work, i ve tried your code but it doesnt work
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?
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
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.
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
|

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.