Python 通过神经网络从POST请求获取对web.py包装器的响应
在阅读了的前三章之后,我想尝试一个基于画布的web界面,看看它在我自己手写输入时的性能如何。结果是他的分叉示例代码repo。它包括一个方形画布,用户可以画出数字,然后它向网络的web.py包装器发送XHR帖子 我遇到的问题是web.py,尤其是:Python 通过神经网络从POST请求获取对web.py包装器的响应,python,ajax,neural-network,web.py,Python,Ajax,Neural Network,Web.py,在阅读了的前三章之后,我想尝试一个基于画布的web界面,看看它在我自己手写输入时的性能如何。结果是他的分叉示例代码repo。它包括一个方形画布,用户可以画出数字,然后它向网络的web.py包装器发送XHR帖子 我遇到的问题是web.py,尤其是: class recognize: def POST(self, name): # read in posted base64 data, assume PNG, convert to greyscale data
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上注册一个回调,以便从中捕获响应
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());
}