Issue
I found an example in FLASK to transmit a video camera through the rtsp protocol in the web browser.
I tried to use this same example in the fastapi, but I'm not getting it. The image is frozen.
Example in Flask (Works normally):
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@app.route("/")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate(),
mimetype="multipart/x-mixed-replace; boundary=frame")
I tried to do it that way in FastAPI, but only the first frame appears, getting frozen.
def generate():
# grab global references to the output frame and lock variables
global outputFrame, lock
# loop over frames from the output stream
while True:
# wait until the lock is acquired
with lock:
# check if the output frame is available, otherwise skip
# the iteration of the loop
if outputFrame is None:
continue
# encode the frame in JPEG format
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
# ensure the frame was successfully encoded
if not flag:
continue
# yield the output frame in the byte format
yield b''+bytearray(encodedImage)
@app.get("/")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
# return StreamingResponse(generate())
return StreamingResponse(generate(), media_type="image/jpeg")
does anyone know how to use the same function in fastapi?
In case anyone is interested in the complete code, I took the example here: https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/
Solution
After posting here, I figured out how to fix it.
In the video_feed function, in the media_type parameter, it was just to put it in the same way as in the flask:
@app.get("/")
def video_feed():
# return the response generated along with the specific media
# type (mime type)
# return StreamingResponse(generate())
return StreamingResponse(generate(), media_type="multipart/x-mixed-replace;boundary=frame")
And in the function generate:
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
My complete code:
http://github.com/mpimentel04/rtsp_fastapi
Answered By - marcus vinicius
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.