我正在使用 Django 并尝试使用 XMLHttpRequest 将网络摄像头数据发送到后台 (view.py) 以处理每个帧。我点击此链接,大多数人都为我的问题提出了类似的方法。
$(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 次(我认为它应该一直工作到相机关闭为止)。
谁能知道问题出在哪里吗?
慕沐林林
相关分类