I need to show a video stream on a react component with a Python+Django backend.
CURRENT SITUATION:
I'm using opencv library to get the stream:
import cv2
[...]
def read_data():
cap = cv2.VideoCapture("rtsp://user:password/bla bla bla")
while(cap.isOpened()):
ret, frame = cap.read()
retval, buffer = cv2.imencode('.jpg', frame)
image_result_base64 = base64.b64encode(buffer)
return HttpResponse(image_result_base64, 200)
On React, where this.state.response_data is the content responseText of the backend call:
render ()
{
const img_data ="data:image/png;base64," + this.state.response_data;
return <img id="overView" class="overView" alt="Stream" src={ img_data} width="90%" />
}
This approach works and on frontend I see the frame in my component.
Unfortunally to create some sort of stream, I need to call backend at least 10 times every second to have 10fps. This is heavy to backend.
I read about StreamingHttpResponse but I don't know if fits my needs.