samedi 11 juin 2016

PiCamera Flask, start and stop preview


I am creating a little web interface in Flask to control the Raspberry Pi camera using the PiCamera python module. I have a working index page which displays a stream from the camera. However, when I POST stop_preview() via an input button the app fails and I cannot work out what I am doing wrong. Here's some of my code so far.

This is part of my views.py

from flask import redirect, url_for, session, request, 
             render_template, Response
from simplepam import authenticate
from app.camera_pi import Camera
from app import app


@app.route('/', methods=['GET', 'POST'])
@app.route('/index',  methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        if request.form['submit']:
            Camera.StopPreview()
    elif request.method == 'GET':
        return render_template("index.html", title="Home")


def gen(camera):
    """Video streaming generator function."""
    while True:
        frame = camera.get_frame()
        yield (b'--framern'
               b'Content-Type: image/jpegrnrn' + frame + b'rn')


@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')

This is my index.html template.

<!DOCTYPE html>


 <html>
      <head>
      </head>
      <body>
        <img id="video_feed" src="{{ url_for('video_feed') }}">
        <form method="post">
          <p><input type="submit" name="submit" value="StopPreview"></p>
        </form>
      </body>
    </html>

This is the camera_pi.py file (taken from Miguel Grinberg's github repo https://github.com/miguelgrinberg/flask-video-streaming)

# The MIT License (MIT)
#
# Copyright (c) 2014 Miguel Grinberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import time
import io
import threading
import picamera
from app import camera_config


class Camera(object):
    thread = None  # background thread that reads frames from camera
    frame = None  # current frame is stored here by background thread
    last_access = 0  # time of last client access to the camera

    def initialize(self):
        if Camera.thread is None:
            # start background frame thread
            Camera.thread = threading.Thread(target=self._thread)
            Camera.thread.start()

            # wait until frames start to be available
            while self.frame is None:
                time.sleep(0)

    def get_frame(self):
        Camera.last_access = time.time()
        self.initialize()
        return self.frame

    def StopPreview():
        with picamera.PiCamera() as camera:
            camera.stop_preview()

    @classmethod
    def _thread(cls):
        with picamera.PiCamera() as camera:
            # camera setup
            camera.resolution = camera_config.camera_resolution

            # let camera warm up
            camera.start_preview()
            time.sleep(2)

            stream = io.BytesIO()
            for foo in camera.capture_continuous(stream, 'jpeg',
                                                 use_video_port=True):
                # store frame
                stream.seek(0)
                cls.frame = stream.read()

                # reset stream for next frame
                stream.seek(0)
                stream.truncate()

                # if there hasn't been any clients asking for frames in
                # the last 10 seconds stop the thread
                if time.time() - cls.last_access > 10:
                    break
        cls.thread = None

I have added the "def StopPreview()" section and it is being called when I POST a submit button from the index page, but the app just crashes at this point.

Thanks in advance for any help you can provide.


Aucun commentaire:

Enregistrer un commentaire