bramasta vikana
bramasta vikana

Reputation: 304

How to pass variable from python script into php and show it in webpage

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

Upvotes: 0

Views: 257

Answers (1)

simirimia
simirimia

Reputation: 419

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.

Upvotes: 1

Related Questions