0

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.

L4ur3nt
  • 98
  • 6
  • Thanks for your suggestion, I already use Streming Response and I have no error message when running this code with uvicorn ... I'll try to put the app online to demonstrate better what issue I'm having. – L4ur3nt Sep 08 '22 at 18:02
  • I added the API on AWS : http://18.231.43.242:8000/docs – L4ur3nt Sep 08 '22 at 19:08
  • Please have a look at [this answer](https://stackoverflow.com/a/70626324/17865804) in the above link. – Chris Sep 09 '22 at 06:05
  • @Chris thank you, could you please give me more details ? Are you talking about the Request and the templates ? How should I integrate it in my code above ? – L4ur3nt Sep 09 '22 at 13:01

0 Answers0