我正在尝试使用django创建一个人脸检测的Web应用。该应用程序的工作方式如下:
- 用户导航到URL
- 摄像头在客户端计算机上启动
- 然后将每个帧发送到服务器进行人脸检测
- 处理后的帧随即在网页上显示
我了解到不能使用OpenCV VideoCapture,因为它只在服务器端起作用。当我在网上阅读时,有人建议我使用JavaScript,特别是WebRTC,在客户端启动实时流。因此,我找到了这篇教程,它解释了如何使用JavaScript在客户端计算机上启动网络摄像头。
现在我的问题是如何将来自客户端计算机上JavaScript的每个帧发送到服务器端的Python OpenCV?
所有这些都应该实时发生,因此我不能保存实时视频,然后在保存的视频上运行Python代码。
一些网站要求我使用AJAX将数据发送到服务器端,但我不确定如何在JavaScript代码中定位要发送的每个帧。
以下是我迄今为止的代码:
通过WebRTC访问客户端摄像头
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
#container {
margin: 0px auto;
width: 500px;
height: 375px;
border: 10px #333 solid;
}
#videoElement {
width: 500px;
height: 375px;
background-color: #666;
}
</style>
</head>
<body>
<div id="container">
<video autoplay="true" id="videoElement">
</video>
</div>
<script>
var video = document.querySelector("#videoElement");
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({
video: true
})
.then(function(stream) {
video.srcObject = stream;
// myJson = JSON.stringify(stream)
})
.catch(function(err0r) {
console.log("Something went wrong!");
});
}
console.log(video)
</script>
</body>
</html>
在这段代码中,我如何从网络摄像头访问每个帧。我尝试使用
console.log
打印video
的内容,但没有帮助。
DJANGO视图.py
def index(request):
return render(request, 'cwrtc/index.html', {})
我使用django channels是因为我认为,为了从客户端发送和接收数据,我可能必须使用web sockets。而且我使用python,因为我计划添加更多功能到应用程序中,使用python编写比其他语言更容易。
是否可能从javascript发送视频流到python?
提前致谢。