Sending webRTC video stream to server with django channels

11,424

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.

Share:
11,424
Sashaank
Author by

Sashaank

A Computer Science Graduate with excellent programming skills in python. I have worked with multiple machine learning libraries, implementing image classification and natural language processing. I have participated in hackathons to provide solutions for signature authentication and waste classification using artificial intelligence. I am well versed in web development and graphic design and have created products for various organisations.

Updated on June 17, 2022

Comments

  • Sashaank
    Sashaank almost 2 years

    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

  • Sashaank
    Sashaank over 5 years
    hey! thanks for the reply. I am a newbie and do not necessarily know how to run native webrtc code on the server side. can you point me to some tutorials on how to do that and also how to interface the python code with webrtc instance. I looked online and could only find on how to run native code for android
  • Sashaank
    Sashaank over 5 years
    How do I implement webrtc with python on the server end? I looked at aiortc but that does not work in windows 10.
  • mesibo
    mesibo over 5 years
    I suggest you pickup Linux for running your server and interface python using python-C module interface. Visit webrtc.org to fetch native code and instructions on how to build it. I will be happy to help for any specific instructions.
  • Hari
    Hari over 3 years
    @Sashaank, I'm trying to do the same thing, were you able to implement it?