从 POST 请求到 web.py 神经网络包装器的响应
Get response from POST request to web.py wrapper around neural network
阅读 Michael Nielson's excellent free book on neural networks, I wanted to try out a canvas based web interface to see how well it would perform on my own hand-written input. The result is this branch 在他的分叉示例代码库中的前三章。它包括一个方块 canvas,用户可以用它勾勒出数字,然后它对网络的 web.py 包装器执行 XHR POST。
我遇到的问题是 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 方面很好,您实际上需要在 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());
}
阅读 Michael Nielson's excellent free book on neural networks, I wanted to try out a canvas based web interface to see how well it would perform on my own hand-written input. The result is this branch 在他的分叉示例代码库中的前三章。它包括一个方块 canvas,用户可以用它勾勒出数字,然后它对网络的 web.py 包装器执行 XHR POST。
我遇到的问题是 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 方面很好,您实际上需要在 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());
}