如何在服务器上执行科学python脚本

时间:2018-01-30 08:20:06

标签: php python neural-network scientific-computing

我正在制作我正在尝试对图片进行分类的项目。为此,我将图像从Android设备发送到服务器进行分类。在服务器上有一个php脚本,它接受图像并将其存储在本地服务器上的uploads文件夹中。下面是将图像保存在服务器上的代码。

upload.php

<?php

                // Path to move uploaded files
                $target_path = dirname(__FILE__).'/uploads/';

                if (isset($_FILES['image']['name'])) {
                    $target_path = $target_path . basename($_FILES['image']['name']);

                    try {
                        // Throws exception incase file is not being moved
                        if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
                            // make error flag true
                            echo json_encode(array('status'=>'fail', 'message'=>'could not move file'));
                        }

            //echo $output;
            $output = null; 
            exec('python walnut_predict.py' ,$output, $return); 
             echo json_encode(array('status'=>'success', 'message'=>$output));

                        // File successfully uploaded
                       // echo json_encode(array('status'=>'success', 'message'=>$output));
                    } catch (Exception $e) {
                        // Exception occurred. Make error flag true
                        echo json_encode(array('status'=>'fail', 'message'=>$e->getMessage()));
                    }
                } else {
                    // File parameter is missing
                    echo json_encode(array('status'=>'fail', 'message'=>'Not received any file'));
                }

            /*
            $output = null; 
            exec('python walnut_predict.py' ,$output, $return); 
             echo json_encode(array('status'=>'fail', 'message'=>$output));
            //print_r($output); 
            //print_r($return) 

            */
            ?>

walnut_predict.py

    from flask import Flask
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
        from keras.models import load_model
        from keras.models import Sequential
        import cv2
        import demjson
        import numpy as np
        from Tkinter import Tk
        from tkFileDialog import askopenfilename
        from keras.preprocessing.image import ImageDataGenerator,                                
    array_to_img, img_to_array, load_img
        model = Sequential()

        model = load_model('first_try_walnut.h5')
        model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

        img1 = cv2.imread("black.jpg")
        img1 = cv2.resize(img1, (150, 150))
        img1 = np.reshape(img1, [1, 150, 150, 3])
        classes1 = model.predict_classes(img1)

        if classes1 == 1:
            data = [{'op': 'Black Walnut'}]
            json = demjson.encode(data)
            return json
        else:
            data = [{'op': 'English Walnut'}]
            json = demjson.encode(data)
    return json


    if __name__ == '__main__':
       app.run()

问题是walnut_predict.py在从upload.php文件触发时不会执行。

1 个答案:

答案 0 :(得分:0)

尝试提供python和脚本的完整路径

exec('/full_path/python /full_path_script/walnut_predict.py' ,$output, $return); 

还使用执行权限(+ x)​​设置脚本。

将其添加到python脚本的第一行

#!/usr/bin/env python

最后 如果您通过Apache运行PHP,请确保Apache用户(www-data)有权访问&amp;执行脚本。