A plugin to allow recalibration via the web API

This adds a plugin with a single endpoint, "recalibrate".
The recalibrate methods are deliberately simple and not aware
of the StreamingCamera because I want to share the maximum
amount of code with non-microscope projects that also want
to flatten the lens shading.

The plugin is microscope-specific, and therefore handles
shutting down the stream and managing the resolution of
the camera, to allow the calibration to run without
restarting the microscope server.

Currently it **DOES NOT SAVE SETTINGS**.

TODO: save the settings after recalibration.

Arguably this isn't a job for this plugin, what we would
need is:
* A method to sync the StreamingCamera's settings with the
  underlying PiCamera object
* A method to save the StreamingCamera's settings to disk.
I think it would be neatest to provide the first of these as
a method of the StreamingCamera class, and call it from the
plugin.  Saving the settings to disk (e.g. to the microscope
rc file, or to somewhere else) should be a separate endpoint
(although most interfaces will probably call it immediately
after recalibration).
This commit is contained in:
Richard Bowman 2019-01-30 09:10:33 +00:00
parent d7e19c40e2
commit 416b74cb4a
3 changed files with 220 additions and 0 deletions

View file

@ -0,0 +1,59 @@
import time
import numpy as np
from openflexure_microscope.plugins import MicroscopePlugin
from openflexure_microscope.utilities import set_properties
from openflexure_microscope.api.v1.views import MicroscopeViewPlugin
from openflexure_microscope.api.utilities import JsonPayload
from flask import request, Response, escape, jsonify
import logging
from .recalibrate_utils import recalibrate_camera
class RecalibrateAPIView(MicroscopeViewPlugin):
def post(self):
payload = JsonPayload(request)
# Figure out the range of z values to use
print("Starting microscope recalibration...")
task = self.microscope.task.start(self.plugin.recalibrate)
# return a handle on the autofocus task
return jsonify(task.state, 202)
class Plugin(MicroscopePlugin):
"""
A set of default plugins
"""
api_views = {
'/recalibrate': RecalibrateAPIView,
}
def recalibrate(self):
"""Reset the camera's settings.
This generates new gains, exposure time, and lens shading
table such that the background is as uniform as possible
with a gray level of 230. It takes a little while to run.
"""
scamera = self.microscope.camera
with scamera.lock:
assert not scamera.state['record_active'], "Can't recalibrate while recording!"
streaming = scamera.state['stream_active']
if streaming:
logging.info("Stopping stream before recalibration")
scamera.stop_stream_recording(resolution=(640,480))
old_resolution = scamera.camera.resolution
try:
scamera.camera.resolution=(640,480)
recalibrate_camera(scamera.camera)
finally:
scamera.camera.resolution=old_resolution
if streaming:
logging.info("Restarting stream after recalibration")
scamera.start_stream_recording()