platypush/platypush/backend/http/app/routes/plugins/camera/__init__.py

88 lines
2.2 KiB
Python
Raw Normal View History

2019-12-29 16:28:07 +01:00
from flask import Response, Blueprint
from platypush.plugins.camera import CameraPlugin
2019-12-29 16:28:07 +01:00
from platypush import Config
from platypush.backend.http.app import template_folder
from platypush.backend.http.app.utils import authenticate, send_request
camera = Blueprint('camera', __name__, template_folder=template_folder)
# Declare routes list
__routes__ = [
camera,
]
def get_device_id(device_id=None):
if device_id is None:
2019-12-29 16:28:07 +01:00
device_id = int(send_request(action='camera.get_default_device_id').output)
return device_id
2019-12-29 16:28:07 +01:00
def get_camera(device_id=None):
device_id = get_device_id(device_id)
2019-12-29 16:28:07 +01:00
camera_conf = Config.get('camera') or {}
camera_conf['device_id'] = device_id
return CameraPlugin(**camera_conf)
2019-12-29 16:28:07 +01:00
def get_frame(device_id=None):
cam = get_camera(device_id)
with cam:
frame = None
2019-12-29 16:28:07 +01:00
for _ in range(cam.warmup_frames):
output = cam.get_stream()
2019-12-29 16:28:07 +01:00
with output.ready:
output.ready.wait()
frame = output.frame
2019-12-29 16:28:07 +01:00
return frame
def video_feed(device_id=None):
2019-12-29 16:28:07 +01:00
cam = get_camera(device_id)
2019-12-29 16:28:07 +01:00
with cam:
while True:
2019-12-29 16:28:07 +01:00
output = cam.get_stream()
with output.ready:
output.ready.wait()
frame = output.frame
2019-12-29 16:28:07 +01:00
if frame and len(frame):
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@camera.route('/camera/<device_id>/frame', methods=['GET'])
@authenticate()
def get_camera_frame(device_id):
2019-12-29 16:28:07 +01:00
frame = get_frame(device_id)
return Response(frame, mimetype='image/jpeg')
@camera.route('/camera/frame', methods=['GET'])
@authenticate()
def get_default_camera_frame():
2019-12-29 16:28:07 +01:00
frame = get_frame()
return Response(frame, mimetype='image/jpeg')
2019-12-29 16:28:07 +01:00
@camera.route('/camera/<device_id>/stream', methods=['GET'])
@authenticate()
2019-12-29 16:28:07 +01:00
def get_stream_feed(device_id):
return Response(video_feed(device_id),
mimetype='multipart/x-mixed-replace; boundary=frame')
2019-12-29 16:28:07 +01:00
@camera.route('/camera/stream', methods=['GET'])
@authenticate()
2019-12-29 16:28:07 +01:00
def get_default_stream_feed():
return Response(video_feed(),
mimetype='multipart/x-mixed-replace; boundary=frame')
# vim:sw=4:ts=4:et: