Consolidate functionality into BaseCamera from Capture Thing and StreamingPicamera2

This commit is contained in:
Julian Stirling 2025-05-21 22:09:01 +01:00
parent b5606984ae
commit 4368dc3d90
9 changed files with 202 additions and 192 deletions

View file

@ -18,7 +18,6 @@ import glob
from fastapi import Depends
import numpy as np
from pydantic import BaseModel
from PIL import Image
from labthings_fastapi.thing import Thing
from labthings_fastapi.dependencies.blocking_portal import BlockingPortal
@ -31,11 +30,8 @@ from labthings_fastapi.dependencies.invocation import InvocationLogger
from .camera import RawCameraDependency as Camera
from .camera import CameraDependency as WrappedCamera
from .stage import StageDependency as Stage
from .capture import CaptureThing
CaptureDep = direct_thing_client_dependency(CaptureThing, "/capture/")
STACK_OVERSHOOT = 200
SETTLING_TIME = 0.3
@ -288,7 +284,6 @@ class AutofocusThing(Thing):
stage: Stage,
logger: InvocationLogger,
metadata_getter: GetThingStates,
capture: CaptureDep,
images_dir: str,
stack_dir: str,
capture_resolution: tuple[int, int],
@ -307,29 +302,19 @@ class AutofocusThing(Thing):
for capture_count in range(images_to_capture):
time.sleep(SETTLING_TIME)
jpeg_path = os.path.join(
stack_dir,
f"{capture_count}.jpeg",
)
jpeg_path = os.path.join(stack_dir, f"{capture_count}.jpeg")
start_time = time.time()
image, metadata = capture._capture_image(
cam=cam,
metadata_getter=metadata_getter,
logger=logger,
)
cam.capture_to_memory(logger=logger, metadata_getter=logger)
captured_time = time.time()
if capture_count + 1 < images_to_capture:
stage.move_relative(z=stack_dz)
moved_time = time.time()
if image.size != capture_resolution:
image = image.resize(capture_resolution, Image.BOX)
downsampled_time = time.time()
capture._save_capture(
cam.save_from_memory(
jpeg_path=jpeg_path,
image=image,
metadata=metadata,
logger=logger,
save_resolution=capture_resolution,
)
saved_time = time.time()
time_remaining = SETTLING_TIME - (saved_time - start_time)
@ -337,10 +322,9 @@ class AutofocusThing(Thing):
time.sleep(time_remaining)
logger.info(f"Settled for an extra {round(time_remaining, 3)} seconds")
logger.debug(f"Capturing took {round(captured_time - start_time, 2)} s")
logger.debug(f"Resizing took {round(downsampled_time - moved_time, 2)} s")
logger.debug(f"Saving took {round(saved_time - downsampled_time, 2)} s")
logger.debug(f"Saving took {round(saved_time - moved_time, 2)} s")
logger.debug(
f"Effective settling time was {round(saved_time - moved_time, 2)} s"
f"Effective settling time was {round(time.time() - moved_time, 2)} s"
)
self.copy_sharpest_image_from_stack(images_dir, stack_dir, logger)