【发布时间】:2018-12-05 18:28:05
【问题描述】:
我正在做这个项目,需要我在 PHP 上上传图片,在 python 上执行图片,从 python 获取输出并在 PHP 上再次显示。
PHP 代码:
<?php
$command = shell_exec("python C:/path/to/python/KNNColor.py");
$jadi = json_decode($command);
var_dump($jadi);
?>
Python 代码:
from PIL import Image
import os
import glob
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color
from scipy.stats import skew
#data train untuk warna
Feat_Mom_M = np.load('FeatM_M.npy')
Feat_Mom_I = np.load('FeatM_I.npy')
Malay_Col_Train = Feat_Mom_M
Indo_Col_Train = Feat_Mom_I
#Data warna
All_Train_Col = np.concatenate((Malay_Col_Train, Indo_Col_Train))
Y_Indo_Col = [0] * len(Indo_Col_Train)
Y_Malay_Col = [1] * len(Malay_Col_Train)
Y_Col_Train = np.concatenate((Y_Malay_Col, Y_Indo_Col))
Train_Col = list(zip(All_Train_Col, Y_Col_Train))
from collections import Counter
from math import sqrt
import warnings
#Fungsi KNN
def k_nearest_neighbors(data, predict, k):
if len(data) >= k:
warnings.warn('K is set to a value less than total voting groups!')
distances = []
for group in data:
for features in data[group]:
euclidean_dist = np.sqrt(np.sum((np.array(features) - np.array(predict))**2 ))
distances.append([euclidean_dist, group])
votes = [i[1] for i in sorted(distances)[:k]]
vote_result = Counter(votes).most_common(1)[0][0]
return vote_result
image_list = []
image_list_pixel = []
image_list_lab = []
L = []
A = []
B = []
for filename in glob.glob('C:/path/to/pic/uploaded/batik.jpg'):
im=Image.open(filename)
image_list.append(im)
im_pix = np.array(im)
image_list_pixel.append(im_pix)
#ubah RGB ke LAB
im_lab = color.rgb2lab(im_pix)
#Pisah channel L,A,B
l_channel, a_channel, b_channel = cv2.split(im_lab)
L.append(l_channel)
A.append(a_channel)
B.append(b_channel)
image_list_lab.append(im_lab)
<The rest is processing these arrays into color moment vector, it's too long, so I'm skipping it to the ending>
Feat_Mom = np.array(Color_Moment)
Train_Set_Col = {0:[], 1:[]}
for i in Train_Col:
Train_Set_Col[i[-1]].append(i[:-1])
new_feat_col = Feat_Mom
hasilcol = k_nearest_neighbors(Train_Set_Col, new_feat_col, 9)
import json
if hasilcol == 0:
#print("Indonesia")
print (json.dumps('Indonesia'));
else:
#print("Malaysia")
print (json.dumps('Malaysia'));
如您所见,只有一个打印命令。 Shell_exec 应该从 python 返回打印命令的字符串。但是我在“var_dump”上得到的是NULL,如果我回显$jadi,也什么都没有。无论是使用 print 还是 print(json) 命令
有趣的是,当我尝试从这个 python 文件中显示一个仅包含 1 行代码的字符串时。
Python 虚拟文件:
print("Hello")
“Hello”字符串在我的 PHP 上显示得很好。那么,shell_exec 是不是无法读取很多代码呢?还是我做错了什么?
【问题讨论】:
-
我会在 $command 上做一个 var_dump 因为 php 的 json_decode 在错误时返回 null 所以 python 可能会输出你所期望的但 json_decode 出错
-
改用
exec(),并捕获退出代码。这是第 1 步。import语句可能由于缺少环境变量而失败。 -
@Brice 我做了,它仍然显示字符串(0)“”,这意味着空
-
@miken32 我使用了 exec(),它返回了 string(0) :(。导入语句失败可能是原因,但即使是这样,我仍然不知道如何解决它
-
@DerpfaceOnigiri 退出代码 0 应该意味着程序正确执行。尽管从 exec() 函数运行 python 似乎确实有问题。你能在 php 脚本之外成功运行这个 python 脚本吗?
标签: php python shell-exec