Python Script from PHP - php

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);

Related

Raspberry Pi 4 python code not run from php

I have a python code on RaspberryPi 4. If I run It from terminal, It works correctly, but if I want to run It from php code, It not works. What is the problem with It? Why run it correctly in terminal, but not run from php? Thanks.
The python code part 1:
#!/usr/bin/python3
import os
import glob
import time
import board
import Adafruit_DHT
import urllib.request
from urllib.request import urlopen
from datetime import datetime
import socket
import re
import smtplib
from email.message import EmailMessage
#email
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = "xyz#gmail.com"
msg['from'] = user
password = "abcdef"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(user, password)
server.send_message(msg)
server.quit()
# DECLARATIONS
now = datetime.now() # Get time right now
timestamp = now.strftime("%Y-%m-%d-%H:%M:%S") # Format the timestamp
os.system('modprobe w1-gpio') # Adding 1-wire module to the kernel
os.system('modprobe w1-therm') # Adding 1-wire therm module
base_dir = '/sys/bus/w1/devices/' # Setting the base_dir
device_folder = glob.glob(base_dir + '28*')[0] # Check if a Dallas DS18B20 is connected
device_file = device_folder + '/w1_slave' # This file holds the temperature
#sensor_type = 11 #DHT sensor type 11/22
#sensor_pin = 7 #DHT sensor pin in GPIO
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 22 # = GPIO22; az alap a GPIO4
# Initial the dht device, with data pin connected to:
#dhtDevice = adafruit_dht.DHT11(board.D23)
The python code part 2:
# Function to read raw temperature from the DS18b20
# It opens the device_file (/w1_slave), and reads the content, then close the file
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
# Function to read the temperature and format it to Celsius and Fahrenheit
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equal_pos = lines[1].find('t=')
if equal_pos != -1:
temp_string = lines[1][equal_pos+2:]
temp_c = float(temp_string) / 1000.0
global celsius
celsius = temp_c
temp_f = temp_c *9.0 / 5.0 + 32.0
return temp_c, temp_f
# Function that reads the humidity and temperature from the DHT11
# It only saves and use the humidity
#humidity = 75
def read_humidity():
global humidity
#global temperature
#humidity, temperature = Adafruit_DHT.read_retry(sensor_type, sensor_pin)
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
time.sleep(2)
while True:
if humidity is not None and temperature is not None:
#print ('Data from sensor is OK. Humidity= {0:0.1f} %'.format(humidity))
print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
if humidity > 100:
read_humidity()
else:
break
else:
print ('Error getting data from DHT.')
# Function to get the local hostname
# It is used for identify the "senor" in the mySQL database
def get_host_name():
global local_hostname
local_hostname = socket.gethostname()
# Function that get the external IP-adress
def get_external_ip_address():
global external_ip
url = "http://checkip.dyndns.org" # This site return one line of text.
my_request = urlopen(url).read() # Read the URL
res = re.findall(b'\d{1,3}', my_request) # Search and findall integers in my_request
my_ip_list = list(map(int, res)) # Clean up the list
my_ip = str(my_ip_list)[1:-1] # Remove the square brackets
temp_ip = my_ip.replace(",", ".") # Replace comma with periods
external_ip = temp_ip.replace(" ", "") # Replace <space> with none-space
print ("External IP: " +external_ip) # Print the External IP address as xxx.xxx.xxx.xxx
# Function that actualle sends data and adds it to the database
def send_data():
print (timestamp) # For debug purpose
print (celsius) # For debug purpose
print (humidity) # For debug purpose
print (local_hostname) # For debug purpose
output = "http://example.com/rpi/add_temp.php?temp="+str(celsius) \
+"&humi="+str(humidity)+"&time="+str(timestamp)+"&sensor="+str(local_hostname)+"&ip=" \
+str(external_ip) # This is the string that is called by the urlopen
print (output) # For debug purpose
html = urlopen(output).read() # Actually performing the call
print (html) # For debug purpose
def main():
read_temp()
read_humidity()
get_host_name()
get_external_ip_address()
send_data()
#dhtDevice.exit()
#GPIO.cleanup()
email_alert("Hey", "Hello World", "xyz#gmail.com")
#email_alert("Hey", "Hello World", "1234567891#vtext.com")
if __name__ == "__main__":
main()
The php code:
<?php
// define the GPIO pins for the motor ouptput (Note: PiFace pins start at 200)
$output = shell_exec('/var/www/html/ds18b20_dht_mysql_v01.py 2>&1');
echo "<pre>$output</pre>";
?>
The error message what I get:
Traceback (most recent call last):
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 138, in
main()
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 128, in main
read_humidity()
File "/var/www/html/ds18b20_dht_mysql_v01.py", line 82, in read_humidity
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 94, in read_retry
humidity, temperature = read(sensor, pin, platform)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/common.py", line 81, in read
return platform.read(sensor, pin)
File "/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
raise RuntimeError('Error accessing GPIO.')
RuntimeError: Error accessing GPIO.
I can see that the problem is the DHT11 temp sensor code. But I dont understand why work it in terminal and why not works it from php.
In your method read_humidity() is a call to itself. Is causes a circular reference...
I tried to run this .py file directly from Android. But it does not work.
.py file location is /var/www/html/ on apache server
String command = "python3 http://192.168.1.49:8081/ds18b20_dht.py";
try {
Process p = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}

How to pass input value to Python file from PHP?

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;

Shell_exec returns NULL in PHP

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

Calling Python script from PHP does not pan out

I am trying to extract major keywords from a text or sentence using python. I am using python's RAKE module. The following python code works well in the console. But, when I am trying to call the python script from PHP, the script does not parse any new text or sentence which I stored in a php variable, and instead outputs the old text/sentence even though I commented out it within the python script and replaces it with sys.argv[1] argument. In various ways, within PHP I tried to solve this problem using PHP's exec and passthru commands without any luck and so I finally decided to post my problem here.
PHP script
$var1 = 'The extra sleep will help your body wash out stress hormones.';
Technique(1)
$output = exec("python rake_tutorial.py .$var1");
Technique(2)
$output = exec('python rake_tutorial.py ' .$var1, $result);
Technique(3)
$command = 'python rake_tutorial.py ' . $var1;
$output = passthru($command);
Technique(4)
$output = exec("python rake_tutorial.py $var1", $result);
echo '<pre>' . print_r($result, true);
Here is my Python code
__author__ = 'a_medelyan'
import rake
import operator
import sys
# EXAMPLE ONE - SIMPLE
stoppath = "SmartStoplist.txt"
# EXAMPLE TWO - BEHIND THE SCENES (from https://github.com/aneesha/RAKE/rake.py)
# 1. initialize RAKE by providing a path to a stopwords file
rake_object = rake.Rake(stoppath)
# text = "What you use depends on your baby's age and physical development."
# 1. Split text into sentences
sentenceList = rake.split_sentences(text)
# generate candidate keywords
stopwordpattern = rake.build_stop_word_regex(stoppath)
phraseList = rake.generate_candidate_keywords(sentenceList, stopwordpattern)
print "Phrases:", phraseList
# calculate individual word scores
wordscores = rake.calculate_word_scores(phraseList)
# generate candidate keyword scores
keywordcandidates = rake.generate_candidate_keyword_scores(phraseList, wordscores)
for candidate in keywordcandidates.keys():
print "Candidate: ", candidate, ", score: ", keywordcandidates.get(candidate)
# sort candidates by score to determine top-scoring keywords
sortedKeywords = sorted(keywordcandidates.iteritems(), key=operator.itemgetter(1), reverse=True)
totalKeywords = len(sortedKeywords)
# for example, you could just take the top third as the final keywords
for keyword in sortedKeywords[0:(totalKeywords / 3)]:
print "Keyword: ", keyword[0], ", score: ", keyword[1]
print rake_object.run(sys.argv[1])
sys.stdout.flush()
# print rake_object.run(text)
Looks like your exec command is going wrong.Try the following command:
$var1 = 'The extra sleep will help your body wash out stress hormones.';
$output = exec("python rake.py '".$var1."'");
Hope this helps!
Just thought I'd mention that there is a RAKE implementation in php https://github.com/artofzen/RAKE-PHP

call php function from python

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())

Categories