My PHP code:
function start($height, $width) {
# do stuff
return $image;
}
Here my Python code:
import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
result = p.communicate()[0]
return result
page_html = "test entry"
output = php("file.php")
print page_html + output
imageUrl = start(h,w)
In Python I want to use that PHP start function. I don't know how to access start function from Python. Can anyone help me on this?
This is how I do it. It works like a charm.
# shell execute PHP
def php(code):
# open process
p = Popen(['php'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, close_fds=True)
# read output
o = p.communicate(code)[0]
# kill process
try:
os.kill(p.pid, signal.SIGTERM)
except:
pass
# return
return o
To execute a particular file do this:
width = 100
height = 100
code = """<?php
include('/path/to/file.php');
echo start(""" + width + """, """ + height + """);
?>
"""
res = php(code)
Note that for Python3 you need res = php(code.encode()), see the answer below
Small update to previous response:
For python3 code string should be encoded to bytes-like object
php(code.encode())
Related
I'm currently using a Pepper Robot to take photos that I use to detect object from coco_classes with a yolov3 algorithm (https://github.com/qqwweee/keras-yolo3) located on my Windows 10 computer. I made an app that can be used on Pepper Tablet using responsive (html, bootstrap, php, ...), to select the object you want to recognize. I'm struggling to pass the class name value from php to a python file called yolo_video.py, it's unlucky because I really want the robot to point out the recognized object, so I need to interact with this Python file.
One important thing to know is that I use Anaconda to call the script and have GPU acceleration.
I tried every single command to pass a value from PHP to Python, sometimes it works, but the script do not complete is mission when it's the case (It's really strange because called from a command prompt it's working fine). With no input, the code works fine even when called from php.
I try to use exec() command because it returns an array-like value witch I can use to take back the last printed element of the python script (witch is angle information that need to take the robot to point the object).
here is yolo_detect.php :
<?php
$classe = $_GET ["choice"];
exec("C:/Anaconda/Scripts/activate yolo && cd C:/wamp64/www/app/projetba3/ && python yolo_video.py --image $who", $value);
echo "valeur envoye ".$class;
var_dump ($value)
?>
You can see that i try to call the yolo_video.py after activating my yolo environment with the parameter --image (Because i do the recognition on one image taken by the robot). I wonder if that argument can cause problems ?
Yolo_video.py :
import sys
import argparse
from yolo import YOLO, detect_video
from PIL import Image
def detect_img(yolo):
while True:
img = "C:\wamp64\www\App\projetba3\camImage.png"
try:
image = Image.open(img)
except:
print('Open Error! Try again!')
continue
else:
r_image, angle = yolo.detect_image(image,str(classe)) #img passe dans le réseau de neurone
print(angle)
#r_image.show()
r_image.save("C:\wamp64\www\App\projetba3\camerapepper.png")
break;
yolo.close_session()
FLAGS = None
if __name__ == '__main__':
classe = sys.argv[1]
print(sys.argv[1])
# class YOLO defines the default value, so suppress any default here
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
'''
Command line options
'''
parser.add_argument(
'--model', type=str,
help='path to model weight file, default ' + YOLO.get_defaults("model_path")
)
parser.add_argument(
'--anchors', type=str,
help='path to anchor definitions, default ' + YOLO.get_defaults("anchors_path")
)
parser.add_argument(
'--classes', type=str,
help='path to class definitions, default ' + YOLO.get_defaults("classes_path")
)
parser.add_argument(
'--gpu_num', type=int,
help='Number of GPU to use, default ' + str(YOLO.get_defaults("gpu_num"))
)
parser.add_argument(
'--image', default=True, action="store_true",
help='Image detection mode, will ignore all positional arguments'
)
'''
Command line positional arguments -- for video detection mode
'''
parser.add_argument(
"--input", nargs='?', type=str,required=False,default='./path2your_video',
help = "Video input path"
)
parser.add_argument(
"--output", nargs='?', type=str, default="",
help = "[Optional] Video output path"
)
FLAGS = parser.parse_args()
if FLAGS.image:
"""
Image detection mode, disregard any remaining command line arguments
"""
print("Image detection mode")
if "input" in FLAGS:
print(" Ignoring remaining command line arguments: " + FLAGS.input + "," + FLAGS.output)
detect_img(YOLO(**vars(FLAGS)))
elif "input" in FLAGS:
detect_video(YOLO(**vars(FLAGS)), FLAGS.input, FLAGS.output)
else:
print("Must specify at least video_input_path. See usage with --help.")
You can see that i use the simple sys.argv[1] to put the value in classe variable. This value is transmitted to another python file yolo.py inside the function yolo.detect_image(image,str(classe)). I added in the function the calculation for the robot position that i need to have. Here is the function :
def detect_image(self, image, classe):
start = timer()
if self.model_image_size != (None, None):
assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
else:
new_image_size = (image.width - (image.width % 32),
image.height - (image.height % 32))
boxed_image = letterbox_image(image, new_image_size)
image_data = np.array(boxed_image, dtype='float32')
print(image_data.shape)
image_data /= 255.
image_data = np.expand_dims(image_data, 0) # Add batch dimension.
out_boxes, out_scores, out_classes = self.sess.run(
[self.boxes, self.scores, self.classes],
feed_dict={
self.yolo_model.input: image_data,
self.input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
thickness = (image.size[0] + image.size[1]) // 300
angle = (0,0)
for i, c in reversed(list(enumerate(out_classes))):
predicted_class = self.class_names[c]
box = out_boxes[i]
score = out_scores[i]
label = '{} {:.2f}'.format(predicted_class, score)
draw = ImageDraw.Draw(image)
label_size = draw.textsize(label, font)
top, left, bottom, right = box
top = max(0, np.floor(top + 0.5).astype('int32'))
left = max(0, np.floor(left + 0.5).astype('int32'))
bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
print(label, (left, top), (right, bottom))
if str(predicted_class)== classe :
ite =+ 1
if ite == 1 :
centresofa = (left+(right-left)/2,top+(bottom-top)/2)
print (centresofa)
anglehor = 55.2*(centresofa[0]/640)
print (anglehor)
if anglehor > 27.2 :
anglehor = anglehor - 27.2
if anglehor < 27.2 :
anglehor = -(27.2 - anglehor)
else :
anglehor = 0
anglever = 44.3*(centresofa[1]/480)
print(anglever)
if anglever < 22.15 :
anglever = 22.15-anglever
if anglever > 22.15 :
anglever = -(anglever-22.15)
else :
anglever = 0
print ("angle horizontal "+str(anglehor)+"angle vertical "+str(anglever))
angle = (anglehor,anglever)
if top - label_size[1] >= 0:
text_origin = np.array([left, top - label_size[1]])
else:
text_origin = np.array([left, top + 1])
# My kingdom for a good redistributable image drawing library.
for i in range(thickness):
draw.rectangle(
[left + i, top + i, right - i, bottom - i],
outline=self.colors[c])
draw.rectangle(
[tuple(text_origin), tuple(text_origin + label_size)],
fill=self.colors[c])
draw.text(text_origin, label, fill=(0, 0, 0), font=font)
del draw
end = timer()
print(end - start)
return image, angle
I should take the values back in the last element of var_dump($values), instead, it just shows me print(sys.argv[1]) value and nothing else (sign that the script stops just after it started). A more strange fact is that his value is "--image" (the argument of my function). How can i solve this big mess ?
How is this exec() function really working with input arguments ???
Python
python yolo_video.py --image $who
sys.argv[0] = yolo_video.py
sys.argv[1] = --image
sys.argv[2] = $who (classname)
I believe you are passing argv[1] "--image" as the class name, when you should be passing argv[2]
https://www.pythonforbeginners.com/argv/more-fun-with-sys-argv
Also, you are trying to use classe as a global variable within detect_img(). This is not a good way to do this and you would have to add a global keyword to make it work.
Change detect_img(yolo) to detect_img(yolo, classe) and pass the actual classe variable to the function via sys.argv[2]
Line 83: detect_img(YOLO(**vars(FLAGS)), sys.argv[2])
PHP
echo "valeur envoye ".$class;
should be
echo "valeur envoye ".$classe;
I've found that you can run a php file from Python by using this:
import subprocess
proc = subprocess.Popen('php.exe input.php', shell=True, stdout=subprocess.PIPE)
response = proc.stdout.read().decode("utf-8")
print(response)
But is there a way to run php code from a string, not from a file? For example:
<?php
$a = ['a', 'b', 'c'][0];
echo($a);
?>
[EDIT]
Use php -r "code" with subprocess.Popen:
def php(code):
p = subprocess.Popen(["php", "-r", code],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = p.communicate() #returns a tuple (stdoutdata, stderrdata)
if out[1] != b'': raise Exception(out[1].decode('UTF-8'))
return out[0].decode('UTF-8')
code = """ \
$a = ['a', 'b', 'c'][2]; \
echo($a);"""
print(php(code))
[Original Answer]
I found a simple class that allows you to do that.
The code is self-explanatory. The class contains 3 methods:
get_raw(self, code): Given a code block, invoke the code and return the raw result as a string
get(self, code): Given a code block that emits json, invoke the code and interpret the result as a Python value.
get_one(self, code): Given a code block that emits multiple json values (one per line), yield the next value.
The example you wrote would look like this:
php = PHP()
code = """ \
$a = ['a', 'b', 'c'][0]; \
echo($a);"""
print (php.get_raw(code))
You can also add a prefix and postfix to the code with PHP(prefix="",postfix"")
PS.: I modified the original class because popen2 is deprecated. I also made the code compatible with Python 3. You can get it here :
import json
import subprocess
class PHP:
"""This class provides a stupid simple interface to PHP code."""
def __init__(self, prefix="", postfix=""):
"""prefix = optional prefix for all code (usually require statements)
postfix = optional postfix for all code
Semicolons are not added automatically, so you'll need to make sure to put them in!"""
self.prefix = prefix
self.postfix = postfix
def __submit(self, code):
code = self.prefix + code + self.postfix
p = subprocess.Popen(["php","-r",code], shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
return child_stdout
def get_raw(self, code):
"""Given a code block, invoke the code and return the raw result as a string."""
out = self.__submit(code)
return out.read()
def get(self, code):
"""Given a code block that emits json, invoke the code and interpret the result as a Python value."""
out = self.__submit(code)
return json.loads(out.read())
def get_one(self, code):
"""Given a code block that emits multiple json values (one per line), yield the next value."""
out = self.__submit(code)
for line in out:
line = line.strip()
if line:
yield json.loads(line)
Based on Victor Val's answer, here is my own compact version.
import subprocess
def run(code):
p = subprocess.Popen(['php','-r',code], stdout=subprocess.PIPE)
return p.stdout.read().decode('utf-8')
code = """ \
$a = ['a', 'b', 'c'][0]; \
echo($a);"""
print(run(code))
I am working on this project that requires me to upload pictures on PHP, execute the picture on python, fetch the output from python and display it again on PHP.
PHP code:
<?php
$command = shell_exec("python C:/path/to/python/KNNColor.py");
$jadi = json_decode($command);
var_dump($jadi);
?>
Python code:
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'));
So as you can see, There is only one print command. Shell_exec is supposed to return the string of the print command from python. But what I get on the "var_dump" is NULL, and if I echo $jadi, there's also nothing. Be it using print or the print(json) command
The fun thing is, when I try to display a string from this python file that only consists 1 line of code.
Python dummy file:
print("Hello")
The "Hello" string, shows up just fine on my PHP. So, is shell_exec unable to read many codes? or is there anything else that I'm doing wrong?
I finally found the reason behind this. In my python script there are these commands :
Feat_Mom_M = np.load('FeatM_M.npy')
Feat_Mom_I = np.load('FeatM_I.npy')
They load the numpy arrays that I have stored from the training process in KNN and I need to use them again as the references for my image classifying process in python. I separated them because I was afraid if my PHP page would take too long to load. It'd need to process all the training data, before finally classifying the uploaded image.
But then when I execute my python file from PHP, I guess it returns an error after parsing those 2 load commands. I experimented putting the print command below them, and it stopped showing on PHP. Since it's all like this now, there's no other way than taking the worst option, even if it'd cost me long loading time.
I tested this in the console:
php > var_dump(json_decode("Indonesia"))
php > ;
php shell code:1:
NULL
php > var_dump(json_decode('{"Indonesia"}'))
php > ;
php shell code:1:
NULL
php > var_dump(json_decode('{"Indonesia":1}'))
php > ;
php shell code:1:
class stdClass#1 (1) {
public $Indonesia =>
int(1)
}
php > var_dump(json_decode('["Indonesia"]'))
php shell code:1:
array(1) {
[0] =>
string(9) "Indonesia"
}
you have to have it wrapped in {} or [] and it will be read into an object or an array.
After an error you can run this json_last_error() http://php.net/manual/en/function.json-last-error.php and it will give you an error code the one your's returns should be JSON_ERROR_SYNTAX
I am running a Python script from a PHP file. For a very a simple example, it works perfectly. Here is the Python script:
#!/usr/bin/env python
def test():
x = 2 + 1
print x + 4
return x
print test()
And PHP script:
<?php
$command = escapeshellcmd('/usr/bin/python2.7 /home/super/PycharmProjects/img_plus_text/helloworld.py');
$output = shell_exec($command);
echo $output;
echo "Finishing....!";
?>
Unfortunately, for more complicated script, It does not work. Here is the Python script
#!/usr/bin/env python
import numpy as np
import os
import random
import caffe
import cv2
from sklearn.externals import joblib
import create_dataset_final_mlp
import text_2_bow
def create_class_mapping():
with open("text_processing/encoded-classes.txt") as class_file:
lines = class_file.readlines()
dictionary = {}
for item in lines:
item = item.split(" ")
dictionary[item[0]] = item[1].replace("\n", "").replace("#", "")
return dictionary
def get_top5(probability_list):
result = []
probabilities = []
for i in range(0, 5):
top = probability_list.index(max(probability_list))
probabilities.insert(i, probability_list[top])
probability_list[top] = -1
result.insert(i, top)
return result, probabilities
def select_random_file(base_dir):
# Select random category
file_chosen = random.choice(os.listdir(base_dir))
# Select random file in that category
file_chosen = random.choice(os.listdir(base_dir + "/" + file_chosen))
return file_chosen
def get_top5_class_name(mapping, top5_list):
class_name_list = []
for i in range(0, 5):
index = top5_list[i]
class_name = mapping[str(index)]
class_name_list.insert(i, class_name)
return class_name_list
def main(image_to_classify):
dataset_path = "/images-test1"
image_file_chosen = create_dataset_final_mlp.find(image_to_classify, dataset_path)
if image_file_chosen is None:
image_file_chosen = select_random_file("%s" % dataset_path)
identifier = image_file_chosen.split(".")[0]
print "Identifier: " + identifier
dictionary = text_2_bow.create_dictionary("text_processing/dictionary-1000-words.csv")
# load CNN model from disk
classifier = create_dataset_final_mlp.load_model_from_disk("test/deploy.prototxt",
"test/snapshot_iter_1020.caffemodel", '256,256',
"test/converted_mean.npy")
# load the final MLP to classify patterns
print "loading model from disk..."
mlp = joblib.load("trained_model_9097_5000_3000_iter_100.model")
with open("export-ferramenta.csv") as export_file:
for line in export_file:
if identifier in line:
pattern = line
print "Pattern: " + pattern
break
# Show selected image
full_img_path = create_dataset_final_mlp.find(image_file_chosen, dataset_path)
# img = cv2.imread(full_img_path, cv2.IMREAD_COLOR)
# cv2.imshow("Image selected", img)
# cv2.waitKey(0)
# get the CNN features
inputs = [caffe.io.load_image(full_img_path)]
# the second parameter is used to switch for prediction from center crop alone instead of averaging predictions across crops (default).
classifier.predict(inputs, False)
features = classifier.blobs['fc6'].data[0]
extracted_cnn_features = create_dataset_final_mlp.get_cnn_features_as_vector(features)
extracted_cnn_features = extracted_cnn_features[:-1]
extracted_cnn_features = extracted_cnn_features.split(",")
# transform the text using text_2_bow functions
pattern = pattern.split(",")
text_pattern = pattern[1] + pattern[2]
matrix = text_2_bow.get_matrix(text_pattern, dictionary)
reshaped_matrix = np.reshape(matrix, 5000).tolist()
full_features_vector = extracted_cnn_features + reshaped_matrix
full_features_vector = np.asarray(full_features_vector, np.float64)
# use the MLP to classify
prediction = mlp.predict_proba([full_features_vector])
tolist = prediction[0].tolist()
mapping = create_class_mapping()
top5, probabilities = get_top5(tolist)
top_class_name = get_top5_class_name(mapping, top5)
print "Top 5 classes: ", top_class_name
print "Top 5 probs ", probabilities
return top_class_name, probabilities
if __name__ == "__main__":
main("")
PHP Script
<?php
echo exec('sudo /home/super/bin/caffe-nv/python /home/super/PycharmProjects/img_plus_text/demo_website.py 2>&1');
?>
ERROR
sh: 1: /home/super/bin/caffe-nv/python: Permission denied
I have read majority of the questions on the issue however I am stuck here. Please point out the mistake. Thank you.
The problem was with environment variables, I made a shell script as shown below
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:$LD_LIBRARY_PATH
export PYTHONPATH=$PYTHONPATH:/home/super/bin/caffe-nv/python/
python /demo_website2.py $1
Then I run the shell script from PHP using exec
exec("/opt/lampp/htdocs/test/run.sh ". $param, $output);
I am new to python. I have created a gui based app to insert values into database.
I have created a Rest api to handle db operations. How can i append the api URL with json created in python.
app.py
from Tkinter import *
import tkMessageBox
import json
import requests
from urllib import urlopen
top = Tk()
L1 = Label(top, text="Title")
L1.pack( side = TOP)
E1 = Entry(top, bd =5)
E1.pack(side = TOP)
L2 = Label(top, text="Author")
L2.pack( side = TOP)
E2 = Entry(top, bd =5)
E2.pack(side = TOP)
L3 = Label(top, text="Body")
L3.pack( side = TOP)
E3 = Entry(top, bd =5)
E3.pack(side = TOP)
input = E2.get();
def callfunc():
data = {"author": E2.get(),
"body" : E3.get(),
"title" : E1.get()}
data_json = json.dumps(data)
# r = requests.get('http://localhost/spritle/api.php?action=get_uses')
#url = "http://localhost/spritle/api.php?action=insert_list&data_json="
#
url = urlopen("http://localhost/spritle/api.php?action=insert_list&data_json="%data_json).read()
tkMessageBox.showinfo("Result",data_json)
SubmitButton = Button(text="Submit", fg="White", bg="#0094FF",
font=("Grobold", 10), command = callfunc)
SubmitButton.pack()
top.mainloop()
Error:
TypeError: not all arguments converted during string formatting
i AM GETTING error while appending url with data_json ?
There is an error on string formating:
Swap this:
"http://localhost/spritle/api.php?action=insert_list&data_json="%data_json
by this:
"http://localhost/spritle/api.php?action=insert_list&data_json=" + data_json
or:
"http://localhost/spritle/api.php?action=insert_list&data_json={}".format(data_json)
The following statements are equivalents:
"Python with " + "PHP"
"Python with %s" % "PHP"
"Python with {}".format("PHP")
"Python with {lang}".format(lang="PHP")
Also, I don't think sending JSON data like this via URL is a good idea. You should encode the data at least.
You are trying to use % operator to format the string, and you need to put the %s placeholder into the string:
"http://localhost/spritle/api.php?action=insert_list&data_json=%s" % data_json
Or use other methods suggested in another answer.
Regarding the data transfer - you definitely need to use POST request and not GET.
Check this, using urllib2 and this, using requests.