GithubHelp home page GithubHelp logo

Comments (2)

askpatrickw avatar askpatrickw commented on June 26, 2024 1

Sounds good, for future searchers, this mostly works...

#!/usr/bin/env python
from importlib import import_module
import os
from flask import Flask, render_template, Response

# import camera driver
# Valid values for Camera Library are:
# pi for Picamera
# pi2 for Picamera2
# opencv for opencv
if os.environ.get("CAMERA"):
    Camera = import_module("camera_" + os.environ["CAMERA"]).Camera
else:
    # Camera Emulator with 1.jpg, 2.jpg, 3.jpg
    from camera import Camera

app = Flask(__name__)

@app.route("/")
def index():
    """Video streaming home page."""
    return render_template("index.html")


def gen(camera):
    """Video streaming generator function."""
    yield b"--frame\r\n"
    while True:
        frame = camera.get_frame()
        yield b"Content-Type: image/jpeg\r\n\r\n" + frame + b"\r\n--frame\r\n"


@app.route("/video_feed")
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(
        gen(Camera()), mimetype="multipart/x-mixed-replace; boundary=frame"
    )


@app.route("/thumbnail")
def get_thumbnail():
    """ After the camera stops, this no longer works.
    """
    frame = Camera().get_frame()
    return Response(frame, mimetype="image/jpeg")


if __name__ == "__main__":
    app.run(host="0.0.0.0", threaded=True)

It moves the loading of the correct libraries to the else and adds a thumbnail route.

It does seem to have some sort of timing issue when adding the MJPEG config in Home Assistant. Sometimes the thumbnail doesn't validate. If I figure out a better solution, I'll post another comment on this closed issue. I think I fixed that.

from flask-video-streaming.

miguelgrinberg avatar miguelgrinberg commented on June 26, 2024

I don't see this as part of this project. The idea of this repository is to demonstrate how to implement the video streaming aspect, and the intention is for this code to be a companion to my blog articles on this topic. Adding other features would make more sense on a dedicated project, which you may want to based off of this code, if you like.

from flask-video-streaming.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.