2

I have some understanding of WebRTC. I know it is mainly used in communication of two peers, but I somehow want my python server to receive it's stream and do some mathematical calculations on each frame, and send back the stream to the user.

I have OpenCV model and it works with openCV videoCapture technology, but I wont to integrate it with webRTC and send results back to the user from python server.

My question is, how can i make WebRTC work with python properly. I found out about aiortc, it's a python implementation for webRTC peer, but it has some problems with several user connections and I need to something else.

Is there any other way to integrate openCV model with WebRTC stream?

Rezga
  • 390
  • 1
  • 10
  • 1
    Currently working out solutions too. The literature and examples on Python and WebRTC are rare. aiortc promises but does not deliver. – Prayson W. Daniel Aug 23 '20 at 16:35
  • 1
    Does this answer your question? [WebRTC with python](https://stackoverflow.com/questions/24718111/webrtc-with-python) and here is everything python and webrtc tagged in SO https://stackoverflow.com/search?q=%5Bpython%5D+webrtc – Prayson W. Daniel Aug 23 '20 at 16:42
  • 1
    Thanks for your replies. I have read almost every stack overflow submission but haven't found anything better than **aiortc**, which doesn't work quite well on several `RTP connections` :(. – Rezga Aug 26 '20 at 08:15

1 Answers1

2

If you're looking for faster and most easiest way to stream frames to clients using WebRTC, you can use my VidGear Python Library's WebGear_RTC utilizes WebRTC technology under the hood, and also built upon aiortc - library for Web Real-Time Communication (WebRTC) and Object Real-Time Communication (ORTC).

Requirement: Works with Python 3.6+ only.

# install VidGear
python3 -m pip install vidgear[asyncio]
python3 -m pip install aiortc

 

Then you can use this complete python example which runs video server at address http://<host-machine ip>:8000/ on any browser on the network, in just a few lines of code:

# import necessary libs
import uvicorn, cv2
from vidgear.gears.asyncio import WebGear_RTC

# create your own custom streaming class
class Custom_Stream_Class:
    """
    Custom Streaming using OpenCV
    """

    def __init__(self, source=0):

        # !!! define your own video source here !!!
        self.source = cv2.VideoCapture(source)

        # define running flag
        self.running = True

    def read(self):

        # don't forget this function!!!

        # check if source was initialized or not
        if self.source is None:
            return None
        # check if we're still running
        if self.running:
            # read frame from provided source
            (grabbed, frame) = self.source.read()
            # check if frame is available
            if grabbed:

                # do something with your OpenCV frame here

                # lets convert frame to gray for this example
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                # return our gray frame
                return gray
            else:
                # signal we're not running now
                self.running = False
        # return None-type
        return None

    def stop(self):

        # don't forget this function!!!

        # flag that we're not running
        self.running = False
        # close stream
        if not self.source is None:
            self.source.release()

# assign your Custom Streaming Class with adequate source (for e.g. foo.mp4) 
# to `custom_stream` attribute in options parameter
options = {"custom_stream": Custom_Stream_Class(source="foo.mp4")}

# initialize WebGear_RTC app without any source
web = WebGear_RTC(logging=True, **options)

# run this app on Uvicorn server at address http://localhost:8000/
uvicorn.run(web(), host="localhost", port=8000)

# close app safely
web.shutdown()

Documentation

If still get some error, raise an issue here in its GitHub repo.

abhiTronix
  • 1,248
  • 13
  • 17