From 397f6ef19f446894609cd18c5a01314416d23aab Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Wed, 15 Jan 2020 11:53:24 +0000 Subject: [PATCH] Added ram-capture method --- .../api/v2/views/actions/__init__.py | 5 ++ .../api/v2/views/actions/camera.py | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/openflexure_microscope/api/v2/views/actions/__init__.py b/openflexure_microscope/api/v2/views/actions/__init__.py index 076548a5..6640a07b 100644 --- a/openflexure_microscope/api/v2/views/actions/__init__.py +++ b/openflexure_microscope/api/v2/views/actions/__init__.py @@ -15,6 +15,11 @@ _actions = { "view_class": camera.CaptureAPI, "conditions": True, }, + "ramCapture": { + "rule": "/camera/ram-capture/", + "view_class": camera.RAMCaptureAPI, + "conditions": True, + }, "previewStart": { "rule": "/camera/preview/start", "view_class": camera.GPUPreviewStartAPI, diff --git a/openflexure_microscope/api/v2/views/actions/camera.py b/openflexure_microscope/api/v2/views/actions/camera.py index 917a72eb..24a29729 100644 --- a/openflexure_microscope/api/v2/views/actions/camera.py +++ b/openflexure_microscope/api/v2/views/actions/camera.py @@ -16,6 +16,7 @@ from openflexure_microscope.utilities import filter_dict from openflexure_microscope.api.v2.views.captures import capture_schema import logging +import io from flask import jsonify, request, abort, url_for, redirect, send_file @@ -85,6 +86,58 @@ class CaptureAPI(View): return output +@ThingAction +class RAMCaptureAPI(View): + """ + Create a non-persistant image capture. + """ + + @use_args( + { + "use_video_port": fields.Boolean(missing=False), + "bayer": fields.Boolean( + missing=False, description="Return with raw bayer data" + ), + "resize": fields.Dict( + missing=None, example={"width": 640, "height": 480} + ), # TODO: Validate keys + } + ) + @doc_response(200, mimetype="image/jpeg") + def post(self, args): + """ + Create a non-persistant image capture. + """ + microscope = find_component("org.openflexure.microscope") + + resize = args.get("resize", None) + if resize: + if ("width" in resize) and ("height" in resize): + resize = ( + int(resize["width"]), + int(resize["height"]), + ) # Convert dict to tuple + else: + abort(404) + + # Open a BytesIO stream to be destroyed once request has returned + with microscope.camera.lock, io.BytesIO() as stream: + + microscope.camera.capture( + stream, + use_video_port=args.get("use_video_port"), + resize=resize, + bayer=args.get("bayer"), + ) + + stream.seek(0) + + return send_file( + io.BytesIO(stream.getbuffer()), + mimetype="image/jpeg" + ) + + @ThingAction class GPUPreviewStartAPI(View): """