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

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

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

问题

以下是您要翻译的部分:

  1. 我想从Python将我的变量传递到我的PHP这是我的Python代码
  2. from pyimagesearch.localbinarypatterns import LocalBinaryPatterns
  3. from imutils import paths
  4. import argparse
  5. import cv2
  6. from keras.models import model_from_json
  7. import os
  8. import PIL.Image
  9. import numpy as np
  10. from numpy import loadtxt
  11. from keras.models import Sequential
  12. from keras.layers import Dense
  13. from keras.optimizers import SGD
  14. from keras.optimizers import adam
  15. import matplotlib.pyplot as plt
  16. from keras.layers import Dropout
  17. from keras.layers import Dropout
  18. from keras import regularizers
  19. from sklearn.model_selection import train_test_split
  20. import random
  21. def main():
  22. #处理神经网络
  23. return prediction[0]
  24. def crop_dulu():
  25. image = PIL.Image.open("D:\projectBram\public\daging.jpg")
  26. center = center_image(image)
  27. left, top, right, bottom = center
  28. center_cropped = crop(image, left, top, right, bottom)
  29. center_cropped.save("D:\projectBram\public\storage\pork\daging123.jpg")
  30. pred = main()
  31. return pred
  32. def center_image(image):
  33. width, height = image.size
  34. left = width / 4
  35. top = height / 4
  36. right = 3 * width / 4
  37. bottom = 3 * height / 4
  38. return ((left, top, right, bottom))
  39. def crop(image, left, top, right, bottom):
  40. cropped = image.crop((left, top, right, bottom))
  41. return cropped
  42. if __name__ == "__main__":
  43. pred1 = crop_dulu()
  44. if pred1 == 0:
  45. print("猪肉")
  46. else:
  47. print("牛肉")
  48. 我想将pred变量发送到我的PHP并添加了完整的Python代码
  49. 这是我的PHP程序
  50. <?php
  51. ob_start();
  52. passthru('python D:/local-binary-patterns/haha2.py');
  53. $command = ob_get_clean();
  54. echo $command;
  55. 我尝试使用escapeshellcmd但也不起作用
  56. 非常感谢您的答复

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

英文:

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

  1. from pyimagesearch.localbinarypatterns import LocalBinaryPatterns
  2. from imutils import paths
  3. import argparse
  4. import cv2
  5. from keras.models import model_from_json
  6. import os
  7. import PIL.Image
  8. import numpy as np
  9. from numpy import loadtxt
  10. from keras.models import Sequential
  11. from keras.layers import Dense
  12. from keras.optimizers import SGD
  13. from keras.optimizers import adam
  14. import matplotlib.pyplot as plt
  15. from keras.layers import Dropout
  16. from keras.layers import Dropout
  17. from keras import regularizers
  18. from sklearn.model_selection import train_test_split
  19. import random
  20. def main():
  21. #processANN
  22. return prediction[0]
  23. def crop_dulu():
  24. image = PIL.Image.open(&quot;D:\projectBram\public\daging.jpg&quot;)
  25. center=center_image(image)
  26. left,top,right,bottom = center
  27. center_cropped = crop(image,left,top,right,bottom)
  28. center_cropped.save(&quot;D:\projectBram\public\storage\pork\daging123.jpg&quot;)
  29. pred=main()
  30. return pred
  31. def center_image(image):
  32. width, height = image.size
  33. left= width /4
  34. top = height /4
  35. right = 3 * width / 4
  36. bottom = 3 * height / 4
  37. return ((left,top,right,bottom))
  38. def crop(image,left,top,right,bottom):
  39. cropped= image.crop((left,top,right,bottom))
  40. return cropped
  41. if __name__ == &quot;__main__&quot;:
  42. pred1=crop_dulu()
  43. if pred1==0:
  44. print(&quot;pork&quot;)
  45. else:
  46. 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 :

  1. &lt;?php
  2. ob_start();
  3. passthru(&#39;python D:/local-binary-patterns/haha2.py&#39;);
  4. $command = ob_get_clean();
  5. 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,因此您需要一个输出缓冲区:

  1. ob_start()
  2. passthru('python D:/local-binary-patterns/haha1.py');
  3. $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:

  1. ob_start()
  2. passthru(&#39;python D:/local-binary-patterns/haha1.py&#39;);
  3. $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:

确定