Skip to content

Instantly share code, notes, and snippets.

@mrbemani
Created July 20, 2020 08:14
Show Gist options
  • Save mrbemani/e9b6bfb41a56ff591554b5dc8b516380 to your computer and use it in GitHub Desktop.
Save mrbemani/e9b6bfb41a56ff591554b5dc8b516380 to your computer and use it in GitHub Desktop.
use flask to push image sequence as mjpeg stream.
# 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