Merge branch 'flatten_lst_route' into 'master'
Added a route to flatten the lens shading table Closes openflexure-microscope-jsclient#62 See merge request openflexure/openflexure-microscope-server!45
This commit is contained in:
commit
093c3d0446
1 changed files with 66 additions and 20 deletions
|
|
@ -7,10 +7,35 @@ from labthings.core.tasks import taskify
|
||||||
|
|
||||||
from flask import abort
|
from flask import abort
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from .recalibrate_utils import recalibrate_camera, auto_expose_and_freeze_settings
|
# Type hinting
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
from .recalibrate_utils import recalibrate_camera, auto_expose_and_freeze_settings, flat_lens_shading_table
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def pause_stream(scamera, resolution: Tuple[int, int] = None):
|
||||||
|
"""This context manager locks a streaming camera, and pauses the stream.
|
||||||
|
|
||||||
|
The stream is re-enabled, with the original resolution, once the with
|
||||||
|
block has finished.
|
||||||
|
"""
|
||||||
|
with scamera.lock:
|
||||||
|
assert not scamera.record_active, "We can't pause the camera's video stream while a recording is in progress."
|
||||||
|
streaming = scamera.stream_active
|
||||||
|
old_resolution = scamera.camera.resolution
|
||||||
|
if streaming:
|
||||||
|
logging.info("Stopping stream in pause_stream context manager")
|
||||||
|
scamera.stop_stream_recording(resolution=resolution)
|
||||||
|
try:
|
||||||
|
yield scamera
|
||||||
|
finally:
|
||||||
|
scamera.camera.resolution = old_resolution
|
||||||
|
if streaming:
|
||||||
|
logging.info("Restarting stream in pause_stream context manager")
|
||||||
|
scamera.start_stream_recording()
|
||||||
|
|
||||||
def recalibrate(microscope):
|
def recalibrate(microscope):
|
||||||
"""Reset the camera's settings.
|
"""Reset the camera's settings.
|
||||||
|
|
@ -19,24 +44,10 @@ def recalibrate(microscope):
|
||||||
table such that the background is as uniform as possible
|
table such that the background is as uniform as possible
|
||||||
with a gray level of 230. It takes a little while to run.
|
with a gray level of 230. It takes a little while to run.
|
||||||
"""
|
"""
|
||||||
scamera = microscope.camera
|
with pause_stream(microscope.camera) as scamera:
|
||||||
with scamera.lock:
|
auto_expose_and_freeze_settings(scamera.camera) # scamera.camera is the PiCamera object
|
||||||
assert not scamera.record_active, "Can't recalibrate while recording!"
|
recalibrate_camera(scamera.camera)
|
||||||
streaming = scamera.stream_active
|
microscope.save_settings()
|
||||||
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)
|
|
||||||
auto_expose_and_freeze_settings(scamera.camera)
|
|
||||||
recalibrate_camera(scamera.camera)
|
|
||||||
finally:
|
|
||||||
scamera.camera.resolution = old_resolution
|
|
||||||
microscope.save_settings()
|
|
||||||
if streaming:
|
|
||||||
logging.info("Restarting stream after recalibration")
|
|
||||||
scamera.start_stream_recording()
|
|
||||||
|
|
||||||
|
|
||||||
@ThingAction
|
@ThingAction
|
||||||
|
|
@ -52,9 +63,42 @@ class RecalibrateView(View):
|
||||||
|
|
||||||
return taskify(recalibrate)(microscope)
|
return taskify(recalibrate)(microscope)
|
||||||
|
|
||||||
|
@ThingAction
|
||||||
|
class FlattenLSTView(View):
|
||||||
|
def post(self):
|
||||||
|
microscope = find_component("org.openflexure.microscope")
|
||||||
|
|
||||||
|
if not microscope:
|
||||||
|
abort(503, "No microscope connected. Unable to flatten the lens shading table.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with pause_stream(microscope.camera) as scamera:
|
||||||
|
flat_lst = flat_lens_shading_table(scamera.camera)
|
||||||
|
scamera.camera.lens_shading_table = flat_lst
|
||||||
|
microscope.save_settings()
|
||||||
|
except:
|
||||||
|
logging.exception("Error flattening the lens shading table.")
|
||||||
|
abort(503, "Couldn't flatten the lens shading table - do you have the forked PiCamera library installed?")
|
||||||
|
|
||||||
|
@ThingAction
|
||||||
|
class DeleteLSTView(View):
|
||||||
|
def post(self):
|
||||||
|
microscope = find_component("org.openflexure.microscope")
|
||||||
|
|
||||||
|
if not microscope:
|
||||||
|
abort(503, "No microscope connected. Unable to flatten the lens shading table.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with pause_stream(microscope.camera) as scamera:
|
||||||
|
scamera.camera.lens_shading_table = None
|
||||||
|
microscope.save_settings()
|
||||||
|
except:
|
||||||
|
logging.exception("Error deleting the lens shading table.")
|
||||||
|
abort(503, "Couldn't flatten the lens shading table - do you have the forked PiCamera library installed?")
|
||||||
|
|
||||||
|
|
||||||
lst_extension_v2 = BaseExtension(
|
lst_extension_v2 = BaseExtension(
|
||||||
"org.openflexure.calibration.picamera", version="2.0.0-beta.1"
|
"org.openflexure.calibration.picamera", version="2.0.0-beta.1", description="Routines to perform flat-field correction on the camera."
|
||||||
)
|
)
|
||||||
|
|
||||||
lst_extension_v2.add_method(
|
lst_extension_v2.add_method(
|
||||||
|
|
@ -62,3 +106,5 @@ lst_extension_v2.add_method(
|
||||||
)
|
)
|
||||||
|
|
||||||
lst_extension_v2.add_view(RecalibrateView, "/recalibrate")
|
lst_extension_v2.add_view(RecalibrateView, "/recalibrate")
|
||||||
|
lst_extension_v2.add_view(FlattenLSTView, "/flatten_lens_shading_table")
|
||||||
|
lst_extension_v2.add_view(DeleteLSTView, "/delete_lens_shading_table")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue