Advertisement
Guest User

picamera+flask

a guest
Mar 12th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. from flask import Flask, render_template, Response
  2.  
  3. # emulated camera
  4. from camera import Camera
  5. import cherrypy
  6. # Raspberry Pi camera module (requires picamera package)
  7. # from camera_pi import Camera
  8.  
  9. app = Flask(__name__)
  10.  
  11.  
  12. @app.route('/')
  13. def index():
  14.     """Video streaming home page."""
  15.     return render_template('index.html')
  16.  
  17.  
  18. def gen(camera):
  19.     """Video streaming generator function."""
  20.     while True:
  21.         frame = camera.get_frame()
  22.         yield (b'--frame\r\n'
  23.                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
  24.  
  25.  
  26. @app.route('/video_feed')
  27. def video_feed():
  28.     """Video streaming route. Put this in the src attribute of an img tag."""
  29.     return Response(gen(Camera()),
  30.                     mimetype='multipart/x-mixed-replace; boundary=frame')
  31.  
  32.  
  33. def run_server():
  34.     cherrypy.tree.graft(app, '/')
  35.     cherrypy.config.update({
  36.         'log.screen': True,
  37.         'engine.autoreload_on': True,
  38.         'server.socket_port': 5000,
  39.         'server.socket_host': '10.0.2.114'
  40.     })
  41.     try:
  42.         cherrypy.engine.start()
  43.         cherrypy.engine.block()
  44.     except KeyboardInterrupt:
  45.         cherrypy.engine.stop()
  46.  
  47.  
  48. if __name__ == '__main__':
  49.     run_server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement