在不刷新页面的情况下将信息从后端(烧瓶)发送到前端

Sent information from backend (flask) to frontend without refreshing the page

  1. 我有一个可以画图的界面。
  2. 我有一个可以识别这张图片的机器学习模型。

我想要什么? 单击“识别”按钮后,我想将机器学习模型的输出发送到外部界面,即文本字段 WITHOUT REFRESHING THE PAGE.

  1. Before pressing the button "Recognize"
  2. After pressing the button "Recognize"

使用下面的代码我可以将模型的输出打印到终端,但我需要将它发送到文本字段。

paint.html

<html>
<head>
<meta charset="utf-8">
<title>Drawing App</title>
<link rel="stylesheet" href="static/style.css" type="text/css" media="screen" title="no title">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
</head>
<body>
  <h2>Phone Number Recognition<img src="https://cdn1.iconfinder.com/data/icons/fatcow/32/painbrush.png" alt="" class="paint"></h2>
<canvas width="1024" height="256" id="mainCanvas"></canvas>
<div class="controls">
    <button id="clear" onclick='clear_canvas()'>Clear</button>
    <button id="recognize" onclick='recognize_number()'>Recognize</button>
    <label id="label1"></label>
    </div>
</div>
<script src="https://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="static/main.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>

style.css

* {
    margin: 0;
    padding: 0;
}

body {
    background: #F5F5F5;
    font-family: sans-serif;
    height: 100%;
    width: 100%;
}

h2 {
  text-align: center;
  margin: 20px;
  font-family: 'Open Sans', sans-serif;
}

.paint {
  padding-top: 2px;
}

/* CANVAS STYLING
===================*/

canvas {
    display: block;
    margin: 40px auto 10px;
    border-radius: 5px;
    border-left: 1px solid #E0E0E0;
    border-right: 1px solid #E0E0E0;
    border-top: 1px solid #E0E0E0;
    box-shadow: 0 4px 0 0 #E0E0E0;
    cursor: url(../img/cursor.png), crosshair;
}

.controls {
    min-height: 60px;
    margin: 0 auto;
    width: 600px;
    border-radius: 5px;
    overflow: hidden;
}
ul {
    list-style:none;
    margin: 0;
    float:  left;
    padding: 10px 0 20px;
    width: 100%;
    text-align: center;
}

/* BUTTON STYLES
==============*/

button {
    background: #68B25B;
    box-shadow: 0 3px 0 0 #6A845F;
    color: #fff;
    outline: none;
    cursor: pointer;
    text-shadow: 0 1px #6A845F;
    display: block;
    font-size: 16px;
    line-height: 40px;
}
label {
    background: #c7ebc1;
    box-shadow: 0 3px 0 0 #6A845F;
    color: rgb(0, 0, 0);
    outline: none;
    cursor: pointer;
    text-shadow: 0 1px #6A845F;
    display: block;
    font-size: 16px;
    line-height: 40px;
}
#clear {
    border: none;
    border-radius: 5px;
    margin: 10px auto;
    padding: 0 20px;
    width: 160px;
    height: 40px;
}
#recognize {
    border: none;
    border-radius: 5px;
    margin: 10px auto;
    padding: 0 20px;
    width: 160px;
    height: 40px;
}
#label1 {
    border: none;
    border-radius: 5px;
    margin: 10px auto;
    padding: 0 20px;
    width: 260px;
    height: 40px;
}

main.js

var colour = $(".selected").css("background-color");
var $canvas = $("canvas");
var context = $canvas[0].getContext("2d");
var lastEvent;
var mouseDown = false;

context.fillStyle = "white";
context.fillRect(0, 0, $canvas[0].width, $canvas[0].height);

// On mouse events on the canvas
$canvas.mousedown(function (e) {
    lastEvent = e;
    mouseDown = true;
}).mousemove(function (e) {
    // Draw lines
    if (mouseDown) {
        context.beginPath();
        context.moveTo(lastEvent.offsetX, lastEvent.offsetY);
        context.lineTo(e.offsetX, e.offsetY);
        context.strokeStyle = colour;
        context.lineWidth = 10;
        context.lineCap = 'round';
        context.stroke();
        lastEvent = e;
    }
}).mouseup(function () {
    mouseDown = false;
}).mouseleave(function () {
    $canvas.mouseup();
});

// Clear the canvas when button is clicked
function clear_canvas() {
    context.fillStyle = "white";
    context.fillRect(0, 0, $canvas[0].width, $canvas[0].height);
}

function recognize_number() {
    var imgURL = $canvas[0].toDataURL('image/jpg');
    $.ajax({
        type: "POST",
        url: "http://172.28.104.162:8080/hook",
        data:{
          imageBase64: imgURL
        }
      }).done(function() {
        console.log('sent');
      });
}

烧瓶

from flask import Flask, render_template, request
import os
import base64
import re

SAVE_PATH = '../data_from_flask'
app = Flask(__name__)

def some_model(img_path):
  return 'some_output'

@app.route("/")
def home():
    return render_template("paint.html")

@app.route('/hook', methods=['GET', 'POST'])
def recognize_image():
    image_b64 = request.values['imageBase64']
    image_data = re.sub('^data:image/.+;base64,', '', image_b64)
    image_path = f"{SAVE_PATH}/flask_{len(os.listdir(SAVE_PATH))}.jpg"
    with open(image_path, "wb") as fh:
        fh.write(base64.decodebytes(bytes(image_data, encoding='UTF-8')))
    
    recognized_number = some_model(img_path=image_path)
    print(recognized_number)
    return render_template('paint.html')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port='8080', debug=False)

您可以通过 id 获取您的元素并将文本设置为您的计算输出。

const labelElement = document.getElementById('label1');

function recognize_number() {
    var imgURL = $canvas[0].toDataURL('image/jpg');
    $.ajax({
        type: "POST",
        url: "http://172.28.104.162:8080/hook",
        data:{
          imageBase64: imgURL
        }
      }).done(function(output) {
        labelElement.innerText = output;
      });
}