40 lines
1023 B
Python
40 lines
1023 B
Python
import io
|
|
from flask import Flask, Response
|
|
from picamera2 import Picamera2
|
|
from PIL import Image
|
|
|
|
WIDTH, HEIGHT = 640, 400
|
|
|
|
cam = Picamera2()
|
|
cam.configure(cam.create_video_configuration(
|
|
main={"size": (WIDTH, HEIGHT), "format": "YUV420"},
|
|
controls={"FrameRate": 30}))
|
|
cam.start()
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
def frames():
|
|
while True:
|
|
gray = cam.capture_array("main")[:HEIGHT, :WIDTH] # Y plane = greyscale image
|
|
buf = io.BytesIO()
|
|
Image.fromarray(gray).save(buf, format="JPEG", quality=70)
|
|
yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + buf.getvalue() + b"\r\n"
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return ('<html><body style="margin:0;background:#111">'
|
|
'<img src="/stream" style="width:100%"></body></html>')
|
|
|
|
|
|
@app.route("/stream")
|
|
def stream():
|
|
return Response(frames(), mimetype="multipart/x-mixed-replace; boundary=frame")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
app.run(host="0.0.0.0", port=8000, threaded=True)
|
|
finally:
|
|
cam.stop() |