我正在使用 Django 并嘗試使用 XMLHttpRequest 將網絡攝像頭數據發(fā)送到后臺 (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次后就停止發(fā)送反饋。我的意思是,control4 函數打印 3 次(我認為它應該一直工作到相機關閉為止)。誰能知道問題出在哪里嗎?
1 回答

慕沐林林
TA貢獻2016條經驗 獲得超9個贊
你必須尋找request.FILES圖像
def control4(request):
print(request.FILES['image'])
print(request.POST.get('threshold'))
return render(request, 'local.html')
添加回答
舉報
0/150
提交
取消