使用Django频道将webRTC视频流发送到服务器 [英] Sending webRTC video stream to server with django channels

查看:132
本文介绍了使用Django频道将webRTC视频流发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建用Django编写的面部检测Web应用程序。该应用程序将以这种方式工作。客户端计算机

  • 然后将每个帧发送到服务器进行面部检测

  • 然后在网页上显示经过处理的帧

  • 我知道我不能使用opencv VideoCapture,因为它只能在服务器端使用。当我在网上阅读时,人们要求我使用javascript(尤其是webRTC)在客户端上开始直播。因此,我找到了这个 教程 ,其中介绍了如何启动网络摄像头在使用javascript的客户端计算机上。



    现在我的问题是如何将客户端计算机上的javascript中的每个帧发送到服务器端的opencv python?



    所有这些都应该实时发生。因此,我无法保存直播视频,然后在保存的视频上运行python代码。



    某些网站要求我使用AJAX将数据发送到服务器端,但是我不知道如何定位JavaScript代码中要发送的每个帧。



    到目前为止,这是我的代码



    使用webRTC的客户端摄像机访问



     <!DOCTYPE html> 
    < html>

    < head>
    < meta charset = utf-8>
    < meta content =填充,帮助,搜索,引擎,而不是 name =关键字>
    < meta content =此页面的内容。 name = description>
    < meta content = Display Webcam Stream name = title>
    < title>显示网络摄像头流< / title>

    < style>
    #container {
    margin:0px auto;
    宽度:500像素;
    高度:375像素;
    边框:10px#333稳定;
    }

    #videoElement {
    width:500px;
    高度:375像素;
    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(出问题了!);
    });
    }

    console.log(video)
    < / script>
    < / body>

    < / html>

    在这段代码中,我如何从摄像头访问每个帧。我尝试使用 console.log 打印视频的内容,但这无济于事。



    DJANGO views.py



      def index(request):
    return render(请求,'cwrtc / index.html',{})

    我正在使用Django频道,因为我发现,要从客户端发送和接收数据,我可能必须使用Web套接字。而且我使用python是因为我计划向应用程序添加更多功能,这些功能将比使用任何其他语言都更容易使用python进行编码。



    是否可以发送视频流



    预先感谢

    解决方案

    是的,您可以在服务器上将视频从javascript发送到python,但是,不能使用Ajax或Web套接字发送帧。



    这是您可以执行的操作。


    1. 使用javascript在客户端创建WebRTC会话

    2. 使用以下方法在服务器端运行webrtc本机代码。

    3. 现在通过交换SDP在客户端和服务器之间创建p2p会话。请注意,您将需要在服务器端使用视频捕获设备,否则将没有视频会话。如果没有,则可以在服务器端使用伪造的视频捕获器。

    4. 然后可以将python代码与服务器上运行的webrtc实例对接。

    让我知道是否需要更多帮助。


    I am trying to create a face detection web application written in django. The app works this way.

    1. The user navigates to the url
    2. The camera starts on the client machine
    3. Each frame is then sent to the server for face detection
    4. Processed frame is then displayed on the web page

    I understood I could not use opencv VideoCapture because, it only works on the server side. When I read online people asked me to use javascript and specifically webRTC to start live stream on the client side. So I found this tutorial which explains how to start webcam on the client machine with javascript.

    Now my question is how to send each frame from javascript on the client machine to opencv python on the server side?

    All this should happen in real time. So I cannot save the live video and then run the python code on the saved video.

    Some sites asked me to use AJAX to send data to the server side but I am not sure how to target each frame that is to be sent in the javascript code.

    This is my code so far

    CLIENT SIDE CAMERA ACCESS WITH 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>
    

    In this piece of code how do I access each frame from the webcam. I tried to print the contents of video with console.log but that did not help.

    DJANGO views.py

    def index(request):
        return render(request, 'cwrtc/index.html', {})
    

    I am using django channels because I figured, to send and receive data from the client side I might have to use web sockets. And I am using python because I plan to add more functionality to the application that will be easier to code with python than any other language.

    Is it possible to send video stream from javascript to python?

    Thanks in advance

    解决方案

    yes, you can send video from javascript to python on your server, however, You can not use Ajax or web socket to send frames.

    This is how you can do it.

    1. Create WebRTC session at client-end using javascript
    2. Run webrtc at your server-end using native code.
    3. Now create p2p session between client and server by exchanging SDPs. Note that you will need video capture device at server end else there won't be video session. If not, you can use fake video capturer at server end.
    4. You can then interface your python code with webrtc instance running on your server.

    Let me know if you need more help.

    这篇关于使用Django频道将webRTC视频流发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆