response motion jpeg - jenhaoyang/backend_blog GitHub Wiki
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)
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')
參考:
https://stackoverflow.com/q/65971081
https://www.pyimagesearch.com/2019/09/02/opencv-stream-video-to-web-browser-html-page/