임을영
임을영

Reputation: 21

video streaming using flask with raspi-camera

Sorry for my English skill. I want to make a web page which streams video and runs several functions. I am using python and flask server. But, there are some problems which I can't solve alone. I have a source code. It's almost perfect.

source code.

import time
from flask import Flask, render_template, Response
from camera import Camera

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

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

@app.route('/video_feed')
def video_feed():
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/test')
def test():
    return time.time() 

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True

and template

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

In the source code, the function named gen() is using 'yield'. so, It will never end. It take all resource. I want to run another function like 'test' function while video is streaming.

Upvotes: 2

Views: 2436

Answers (2)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

Video streaming requires a permanent connection with the client. If you are doing this using the Flask development web server, then you can't handle any other requests, because that server by default handles a single connection at a time.

If you want to handle more than one connection simultaneously you have a couple of options. The simplest (but not very robust or efficient) is to run the development server in threaded mode (just add threaded=True inside the app.run() call). This will spawn new threads for incoming requests. A more production-ready solution is to switch to a different web server. For example, with gunicorn you can control how many worker processes are started. For even more flexibility a server such as gevent can handle really large number of concurrent client requests.

Upvotes: 1

mitghi
mitghi

Reputation: 919

You can control behavior of generators like this:

import time

start_time = time.time()

def first():
    while True:
        yield 'frame'

def second():
    while True:
        yield time.time()

init = [first(),second()]

def handler():
    #handling logic 
    global start_time
    while True:
        try:
            res = map(lambda x: x.next(),init)
            if res[1]  - start_time > 10: break 
            print res[1] - start_time
        except StopIteration:
            break

handler()

This code will yield difference between time since script started and imaginary frame which can be any data.

Upvotes: 0

Related Questions