【问题标题】:Get response from POST request to web.py wrapper around neural network从 POST 请求中获取对神经网络 web.py 包装器的响应
【发布时间】:2015-11-17 15:43:31
【问题描述】:

在阅读了Michael Nielson's excellent free book on neural networks 的前三章后,我想尝试一个基于画布的 Web 界面,看看它在我自己的手写输入上的表现如何。结果是this branch 在他的分叉示例代码仓库中。它包括一个方形画布,用户可以在其中勾勒出数字,然后将 XHR POST 发送到网络的 web.py 包装器。

我遇到的问题是 web.py,特别是:

class recognize:
    def POST(self, name):
        # read in posted base64 data, assume PNG, convert to greyscale
        data = web.data()
        file = cStringIO.StringIO(urllib.urlopen(data).read())
        img = Image.open(file).convert('L')
        # resize to 28x28
        img.thumbnail((28,28), Image.ANTIALIAS)
        # convert to vector
        vec = np.asarray(img).reshape((28*28,1)).astype(float)
        # feed foward through neural network
        digit = net.recognize(vec)
        print digit
        return digit

最后一行似乎无关紧要,我无法在对我的 javascript 客户端的 HTTP 响应中获得 digit。还有其他方法我应该将digit 放入响应中吗?

【问题讨论】:

    标签: python ajax neural-network web.py


    【解决方案1】:

    python 端很好,你实际上需要在 index.html 中的 XMLHttpRequest 上注册一个回调,以便捕获来自识别的响应

    function exportImage() {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
          if (xhr.readyState == 4 && xhr.status == 200) {
             alert(xhr.responseText) //capture digit here and do something with it
          }
      };
      xhr.open("POST", "//localhost:8080/recognize", true);
      xhr.send(canvas.toDataURL());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2021-12-26
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多