Created
July 20, 2020 08:14
-
-
Save mrbemani/e9b6bfb41a56ff591554b5dc8b516380 to your computer and use it in GitHub Desktop.
use flask to push image sequence as mjpeg stream.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # learnt | |
| # import cv2 | |
| # import flask and initialize app ... | |
| # define global pushed_frame and frame_is_ready variable | |
| def generateStreamFrame(): | |
| # grab global references to the output frame | |
| global pushed_frame, frame_is_ready | |
| # loop over frames from the output stream | |
| while True: | |
| # check if the output frame is available, otherwise skip | |
| # the iteration of the loop | |
| outputFrame = None | |
| if frame_is_ready: | |
| outputFrame = pushed_frame.copy() | |
| 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'--mjpegframeboundary\r\n' b'Content-Type: image/jpeg\r\n\r\n' + | |
| bytearray(encodedImage) + b'\r\n') | |
| @webapp.route("/mjpeg") | |
| def video_feed(): | |
| # return the response generated along with the specific media | |
| # type (mime type) | |
| return Response(generateStreamFrame(), | |
| mimetype="multipart/x-mixed-replace; boundary=mjpegframeboundary") | |
| # run flask app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment