英文:
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实体字符(如"
和'
)替换为原始字符,以使代码更容易阅读和理解。如果您需要进一步的帮助,请随时提问。
英文:
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
答案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('python D:/local-binary-patterns/haha1.py');
$command = ob_get_clean()
Now $command should contain all data printed by you python script.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论