Code cleanup

This commit is contained in:
Joel Collins 2020-10-14 14:56:29 +00:00
parent 2bfb988460
commit 994e83dbeb
46 changed files with 261 additions and 318 deletions

View file

@ -7,7 +7,7 @@ from typing import Tuple
from flask import abort
from labthings import find_component
from labthings.extensions import BaseExtension
from labthings.views import ActionView, View
from labthings.views import ActionView
from .recalibrate_utils import (
auto_expose_and_freeze_settings,
@ -78,17 +78,10 @@ class FlattenLSTView(ActionView):
"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?",
)
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()
class DeleteLSTView(ActionView):
@ -101,16 +94,9 @@ class DeleteLSTView(ActionView):
"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?",
)
with pause_stream(microscope.camera) as scamera:
scamera.camera.lens_shading_table = None
microscope.save_settings()
lst_extension_v2 = BaseExtension(

View file

@ -24,13 +24,15 @@ def flat_lens_shading_table(camera):
raise ImportError(
"This program requires the forked picamera library with lens shading support"
)
return np.zeros(camera._lens_shading_table_shape(), dtype=np.uint8) + 32
return (
np.zeros(camera._lens_shading_table_shape(), dtype=np.uint8) + 32
) # pylint: disable=W0212
def adjust_exposure_to_setpoint(camera, setpoint):
"""Adjust the camera's exposure time until the maximum pixel value is <setpoint>."""
print("Adjusting shutter speed to hit setpoint {}".format(setpoint), end="")
for i in range(3):
for _ in range(3):
print(".", end="")
camera.shutter_speed = int(
camera.shutter_speed * setpoint / np.max(rgb_image(camera))
@ -50,9 +52,9 @@ def auto_expose_and_freeze_settings(camera):
camera.awb_mode = "auto"
camera.exposure_mode = "auto"
camera.iso = (
0
) # This is important, if it's on a fixed ISO, gain might not set properly.
for i in range(6):
0 # This is important, if it's on a fixed ISO, gain might not set properly.
)
for _ in range(6):
print(".", end="")
time.sleep(0.5)
logging.info("done")
@ -188,10 +190,10 @@ def recalibrate_camera(camera):
if __name__ == "__main__":
with PiCamera() as camera:
camera.start_preview()
with PiCamera() as main_camera:
main_camera.start_preview()
time.sleep(3)
logging.info("Recalibrating...")
recalibrate_camera(camera)
recalibrate_camera(main_camera)
logging.info("Done.")
time.sleep(2)