python - 从 POST 请求获取对神经网络 web.py 包装器的响应

标签 python ajax neural-network web.py

读完Michael Nielson's excellent free book on neural networks的前三章后,我想尝试一个基于 Canvas 的 Web 界面,看看它在我自己的手写输入上的表现如何。结果是this branch在他的 fork 示例代码仓库中。它包括一个方形 Canvas ,用户可以在其中绘制数字,然后将 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 响应中获取数字。我是否应该通过其他方式将数字放入响应中?

最佳答案

Python 方面没问题,您实际上需要在 index.html 中的 XMLHttpRequest 上注册回调,以便捕获 recognize 的响应

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());
}

关于python - 从 POST 请求获取对神经网络 web.py 包装器的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33761087/

相关文章:

neural-network - 我是否必须使用神经网络预处理测试数据?

python - 复制嵌套列表并用连续数字填充

python - JAX:jit 函数的时间随着函数访问的内存而超线性增长

python - 字符串中特定字母的数量

python - 如何从数据框列中每个单词末尾删除特定的字母组合?

jquery - AJAX post 请求后将数据返回给客户端?

jquery - IE7 无法使用 jQuery .load 命令加载页面

machine-learning - 重量衰减值增加显示最差性能

javascript - 如何/是否重写异步 :false AJAX function?

python - 如何在pytorch中手动计算整个数据集的误差?