Implementing a WebSocket to perform object detection on a stream of images
One of the main benefits of WebSockets, as we saw in Chapter 8, Defining WebSockets for Two-Way Interactive Communication in FastAPI, is that it opens a full-duplex communication channel between the client and the server. Once the connection is established, messages can be passed quickly without having to go through all the steps of the HTTP protocol. Therefore, it’s much more suited to sending a lot of data in real time.
The point here will be to implement a WebSocket endpoint that is able to both accept image data and run object detection on it. The main challenge here will be to handle a phenomenon known as backpressure. Put simply, we’ll receive more images from the browser than the server is able to handle because of the time needed to run the detection algorithm. Thus, we’ll have to work with a queue (or buffer) of limited size and drop some images along the way to handle the stream...