vendredi 31 janvier 2020

Upload image and text data to flask server through ajax

I want to upload image and text data to my flask server. How can we send it. My HTML Code

<form id="upload-file" method="post" enctype="multipart/form-data">
                            <button type="button" class="btn btn-success" >
                                <label for="imageUpload" style="font-size: 1rem; line-height: 1.5; font-weight: normal; margin-top: 9px;" >
                                    Choose Photo...
                                </label>
                            </button>
                            <input type="file" id="imageUpload" name="image" accept=".png, .jpg, .jpeg">
                            <input type="text" id="imageUpload" name="i1" >
                            <input type="text" id="imageUpload" name="i2" >
                        </form>

JavaScript and ajax Code

$('#btn-predict').click(function () {
        var form_data = new FormData(document.getElementById('upload-file'));
        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:5000/predict',
            data: form_data,
            contentType: false,
            cache: false,
            processData: false,
            async: true,
            success: function (data) {
                console.log('Success1');
            },
        });
    });

My Flask Server Code

@app.route('/predict', methods=['GET', 'POST'])
def upload():
    if request.method == "POST":

        if request.files:
            output={}
            image = request.files["image"]
            print(request.files)
            basepath = os.path.dirname(__file__)
            file_path = os.path.join(
            basepath, 'uploads', secure_filename(image.filename))
            image.save(file_path)
            print(file_path)
            # Make prediction
            model = load_model(model_path)
            model.load_weights(model_weight)
            preds = model_predict(file_path, model)

            return str(preds)

    return render_template('AboutUs.html')

I know how, who to send image but in above case I want to send text data with image.




Aucun commentaire:

Enregistrer un commentaire