我遇到了一个奇怪的情况,我不知道是什么问题
我想使用ajax在使用php的服务器(Raspberry Pi 3)上发出请求
php脚本处理请求,使用shell_exec运行python脚本,并将shell_exec的结果返回给客户端。 python脚本执行Tensorflow Lite模型的预测并打印结果
问题是我的shell_exec()没有执行,php返回一个状态为200且response=0的响应
我认为这是服务器上的超时,因为python脚本需要10秒来打印结果,但将python代码更改为“sleep(180)”只会使ajax请求等待3分钟
重新更改python脚本
所以,我认为这是关于python脚本的
运行
php server.php
在raspberry上,我在exepct时得到了python脚本的输出
所以,可能是关于运行php脚本(www数据等)的apache用户。我将所有文件的权限更改为777,但错误仍然存在
ajax.html
<!DOCTYPE html>
<html lang="it">
<head>
<title>Ajax Example</title>
<meta charset="utf-8">
<script>
function reply() {
$.ajax({
url: 'risposta.php',
type: 'POST',
contentType: false,
processData: false,
async: true,
timeout: 0,
success: function(response){
if(response != 0){
document.getElementById("result").innerHTML = response;
}else{
alert('Error');
}
},
});
}
</script>
</head>
<body>
<form id="formesempio">
<input type="text" id="nome">
<input type="button" value="Play" onclick="javascript:reply()">
</form>
<p>Result: <span id="result"></span></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</body>
</html>
server.php
<?php
$command = "python3 /var/www/html/DBR/predict.py --fn /var/www/html/img.jpg";
$out = shell_exec($command);
echo $out;
?>
预测.py
from tflite_runtime.interpreter import Interpreter
from PIL import Image
import numpy as np
import time
import argparse
import pandas as pd
from DBR_utils import print_human_readable_dog_deploy, get_dog_from_predictions
# Variabili globali
model_path = "DBR/DBR.tflite"
label_path = "DBR/IT_breeds.txt"
def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def classify_image(interpreter, image, top_k=1):
set_input_tensor(interpreter, image)
interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = interpreter.get_tensor(output_details['index'])
return output
# -- Parser --
parser = argparse.ArgumentParser(description='DBR')
parser.add_argument('--fn', type=str, required=True, help='filename')
args = parser.parse_args()
interpreter = Interpreter(model_path)
#print("Model Loaded Successfully.")
interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
#print("Image Shape (", width, ",", height, ")")
# Load an image to be classified.
image = Image.open(args.fn).convert('RGB').resize((width, height))
# Classify the image.
time1 = time.time()
output = classify_image(interpreter, image)
time2 = time.time()
classification_time = np.round(time2-time1, 3)
#print("Classificaiton Time =", classification_time, "seconds.")
labels = []
with open(label_path, 'r') as f:
tmp = f.readlines()
for l in tmp:
labels.append(l.rstrip())
labels = pd.Series(labels)
dog = get_dog_from_predictions(np.squeeze(output), labels, args.fn)
res = print_human_readable_dog_deploy(dog)
print(res)
目前没有回答
相关问题 更多 >
编程相关推荐