我正在使用 Django 并尝试使用 XMLHttpRequest 将网络摄像头数据发送到后台 (view.py) 以处理每个帧。我关注this link大多数人都针对我的问题提出了类似的方法。
$(document).ready(function(){
$('#trigger_button').click(function(){
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => {
document.getElementById("myVideo").srcObject = stream;
})
.catch(err => {
alert('navigator.getUserMedia error: ', err)
});
drawCanvas.width = v.videoWidth;
drawCanvas.height = v.videoHeight;
imageCanvas.width = uploadWidth;
imageCanvas.height = uploadWidth * (v.videoHeight / v.videoWidth);
drawCtx.lineWidth = 4;
drawCtx.strokeStyle = "cyan";
drawCtx.font = "20px Verdana";
drawCtx.fillStyle = "cyan";
imageCanvas.getContext("2d").drawImage(v, 0, 0, v.videoWidth, v.videoHeight, 0, 0, uploadWidth, uploadWidth * (v.videoHeight / v.videoWidth));
imageCanvas.toBlob(postFile, 'image/jpeg');
});
});
function postFile(file) {
var formdata = new FormData();
formdata.append("image", file);
formdata.append("threshold", scoreThreshold);
var xhr = new XMLHttpRequest();
xhr.open('POST', apiServer, true);
xhr.onload = function () {
if (this.status === 200) {
var objects = JSON.parse(this.response);
drawBoxes(objects);
imageCtx.drawImage(v, 0, 0, v.videoWidth, v.videoHeight, 0, 0, uploadWidth, uploadWidth * (v.videoHeight / v.videoWidth));
imageCanvas.toBlob(postFile, 'image/jpeg');
}
else {
alert(this.status);
}
};
xhr.send(formdata);
}
然后,我尝试访问 view.py 中请求中的数据,如下所示:
def control4(request):
print(request.POST.get('image'))
print(request.POST.get('threshold'))
return render(request, 'local.html')
但是,虽然request.Post.get('threshold')返回一个值,但request.POST.get('image')返回None。另外,该方法重复3次后就停止发送反馈。我的意思是,control4 函数打印 3 次(我认为它应该一直工作到相机关闭为止)。
有人知道问题出在哪里吗?
谢谢你,最好的。
最佳答案
您必须在 request.FILES
中查找图像
def control4(request):
print(request.FILES['image'])
print(request.POST.get('threshold'))
return render(request, 'local.html')
关于python - XmlHttpRequest 请求元素为 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64272668/