Created
September 17, 2021 13:22
-
-
Save usr-ein/b3bc6aa48a01d86a82beae24f821166b to your computer and use it in GitHub Desktop.
Video stream from JPEG - Create an HTTP video stream from a file that periodically gets overwritten by another process
This file contains 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
#!/usr/bin/env python3 | |
import io | |
import os | |
from sys import argv | |
from time import sleep | |
from flask import Flask, Response, render_template | |
from PIL import Image | |
app = Flask(__name__) | |
def gen(): | |
im_path = argv[1] | |
while True: | |
sleep(1) | |
if not os.path.isfile(im_path): | |
continue | |
bytestream = io.BytesIO() | |
Image.open(im_path).save(bytestream, "JPEG") | |
frame = bytestream.getvalue() | |
yield ( | |
b"--frame\r\n" | |
+ b"Content-Type: image/jpeg\r\n\r\n" | |
+ frame | |
+ b"\r\n\r\n" | |
) | |
@app.route("/video_feed") | |
def video_feed(): | |
return Response( | |
gen(), mimetype="multipart/x-mixed-replace; boundary=frame" | |
) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment