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

时间:2015-11-17 15:43:31

标签: python ajax neural-network web.py

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

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

<configuration>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />

最后一行似乎并不重要,我无法在我的javascript客户端的HTTP响应中获得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 。还有其他方法我应该将digit放入响应中吗?

1 个答案:

答案 0 :(得分:2)

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