简体   繁体   English

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

[英]how to execute scientific python scripts on server

I'm working on my project in which i'm trying to classify image. 我正在尝试对图像进行分类的项目。 for this i'm sending the image from the android device to the server for classification. 为此,我将图像从android设备发送到服务器进行分类。 On the server there is a php script that accepts the image and stores it in the uploads folder on the local server.below is the code that saves the image on server. 在服务器上,有一个php脚本接受图像并将其存储在本地服务器的uploads文件夹中。以下是将图像保存在服务器上的代码。

upload.php 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 peach_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()

The problem is the walnut_predict.py doesn't execute when triggered from the upload.php file. 问题是从upload.php文件触发时,walnut_predict.py无法执行。

Try providing the full path to python and the script 尝试提供python和脚本的完整路径

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

Also set the script with the execution permission(+x). 还要设置脚本的执行许可权(+ x)​​。

Add this to the first line of the python script 将此添加到python脚本的第一行

#!/usr/bin/env python

Finally If you are running PHP via Apache, make sure the Apache user(www-data) has permission to access & execute the script. 最后,如果您通过Apache运行PHP,请确保Apache用户(www-data)有权访问和执行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM