如何从Python脚本传递变量到PHP并在网页上显示它。

huangapple go评论72阅读模式
英文:

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

问题

以下是您要翻译的部分:

我想从Python将我的变量传递到我的PHP这是我的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():
 #处理神经网络
 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("猪肉")
    else:
        print("牛肉")

我想将pred变量发送到我的PHP并添加了完整的Python代码
这是我的PHP程序

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

我尝试使用escapeshellcmd但也不起作用
非常感谢您的答复

请注意,我已经将HTML实体字符(如&quot;&#39;)替换为原始字符,以使代码更容易阅读和理解。如果您需要进一步的帮助,请随时提问。

英文:

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(&quot;D:\projectBram\public\daging.jpg&quot;)
center=center_image(image)
left,top,right,bottom = center
center_cropped = crop(image,left,top,right,bottom)
center_cropped.save(&quot;D:\projectBram\public\storage\pork\daging123.jpg&quot;)
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__ == &quot;__main__&quot;:
pred1=crop_dulu()
if pred1==0:
print(&quot;pork&quot;)
else:
print(&quot;beef&quot;)

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

 &lt;?php 
ob_start();
passthru(&#39;python D:/local-binary-patterns/haha2.py&#39;);
$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

答案1

得分: 1

请查看 https://www.php.net/manual/en/function.passthru.php

passthru 不返回任何内容,因此 $command 将始终为空。

您的Python脚本会将输出发送到stdout,因此您需要一个输出缓冲区:

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

现在,$command 应该包含您的Python脚本打印的所有数据。

英文:

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(&#39;python D:/local-binary-patterns/haha1.py&#39;);
$command = ob_get_clean()

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

huangapple
  • 本文由 发表于 2020年1月6日 15:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定