简体   繁体   English

使用 django 通道将 webRTC 视频流发送到服务器

[英]Sending webRTC video stream to server with django channels

I am trying to create a face detection web application written in django.我正在尝试创建一个用 django 编写的人脸检测网络应用程序。 The app works this way.该应用程序以这种方式工作。

  1. The user navigates to the url用户导航到 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.我知道我不能使用 opencv VideoCapture,因为它只适用于服务器端。 When I read online people asked me to use javascript and specifically webRTC to start live stream on the client side.当我在网上阅读时,人们要求我使用 javascript,特别是 webRTC 在客户端开始直播。 So I found this tutorial which explains how to start webcam on the client machine with javascript.所以我找到了这个教程,它解释了如何使用 javascript 在客户端机器上启动网络摄像头。

Now my question is how to send each frame from javascript on the client machine to opencv python on the server side?现在我的问题是如何将每个帧从客户端机器上的 javascript 发送到服务器端的 opencv python?

All this should happen in real time.所有这一切都应该实时发生。 So I cannot save the live video and then run the python code on the saved video.所以我无法保存实时视频,然后在保存的视频上运行 python 代码。

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.有些站点要求我使用 AJAX 将数据发送到服务器端,但我不确定如何定位要在 javascript 代码中发送的每个帧。

This is my code so far到目前为止,这是我的代码

CLIENT SIDE CAMERA ACCESS WITH webRTC使用 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.我尝试使用console.log打印video的内容,但这没有帮助。

DJANGO views.py 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.我正在使用 Django 通道,因为我想,要从客户端发送和接收数据,我可能必须使用 Web 套接字。 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.我使用 python 是因为我计划向应用程序添加更多功能,使用 python 编写代码比使用其他任何语言都更容易。

Is it possible to send video stream from javascript to python?是否可以将视频流从 javascript 发送到 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.是的,您可以将视频从 javascript 发送到服务器上的 python,但是,您不能使用 Ajax 或网络套接字发送帧。

This is how you can do it.这就是你如何做到的。

  1. Create WebRTC session at client-end using javascript使用 javascript 在客户端创建 WebRTC 会话
  2. Run webrtc at your server-end using native code.使用本机代码在服务器端运行 webrtc。
  3. Now create p2p session between client and server by exchanging SDPs.现在通过交换 SDP 在客户端和服务器之间创建 p2p 会话。 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.然后,您可以将您的 python 代码与在您的服务器上运行的 webrtc 实例连接起来。

Let me know if you need more help.如果您需要更多帮助,请告诉我。

I don't think this is a good approach for streaming a video via server.我认为这不是通过服务器流式传输视频的好方法。 Why even you want to send video frames to server when webrtc directly offers p2p connection.All you need a socket library to manage the user and connect them.You can pass random generated roo_id to django channel for creating rooms.I will suggest you if you are good at nodejs go for socket.io library as very less resources and documentation available on django channel with webrtc.为什么当 webrtc 直接提供 p2p 连接时你甚至想将视频帧发送到服务器。你需要一个套接字库来管理用户并连接它们。你可以将随机生成的 roo_id 传递给 django 通道以创建房间。如果你,我会建议你擅长 nodejs 去 socket.io 库,因为在带有 webrtc 的 django 频道上可用的资源和文档非常少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM