I try get rtmp (or rtsp) stream, perform model inference and then output the stream from an API using fastapi. I find some guidance from this thread but the endpoint is processing endlessly without giving an error message.
You can see the API with 3 working endpoints here : http://18.231.43.242:8000/docs
I tested the stream_detect endpoint with the following rtmp stream rtmp://62.113.210.250/medienasa-live/rbw_high without success. I am not sure if the API is streaming something and I am not able to retrieve it or if it is not streaming at all.
Here is the code for the endpoint :
@app.post("/stream_detect")
def stream_detect(link: str = Form()):
capture = cv2.VideoCapture(link)
def detect(stream):
while True:
err, frame = stream.read()
# size = (640*frame.shape[0]/frame.shape[1],640)
# resized_frame = cv2.resize(frame.astype(np.uint8), size)
detection = model(frame)
detection.render()
(flag, encodedimage) = cv2.imencode(".jpg", detection.imgs[0])
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedimage) + b'\r\n')
return StreamingResponse(detect(capture),media_type="multipart/x-mixed-replace;boundary=frame" )
I did research the web for more than a week now without finding the right way to do it, any help is welcome.