Capture as a Raw dependency to use methods that aren't actions
This commit is contained in:
parent
6cf2f4d8c3
commit
ce33b94aab
2 changed files with 10 additions and 37 deletions
|
|
@ -22,15 +22,12 @@ from labthings_fastapi.dependencies.blocking_portal import BlockingPortal
|
||||||
from labthings_fastapi.decorators import thing_action, thing_property
|
from labthings_fastapi.decorators import thing_action, thing_property
|
||||||
from labthings_fastapi.dependencies.metadata import GetThingStates
|
from labthings_fastapi.dependencies.metadata import GetThingStates
|
||||||
from labthings_fastapi.types.numpy import NDArray
|
from labthings_fastapi.types.numpy import NDArray
|
||||||
from labthings_fastapi.dependencies.thing import direct_thing_client_dependency
|
|
||||||
from labthings_fastapi.dependencies.invocation import InvocationLogger
|
from labthings_fastapi.dependencies.invocation import InvocationLogger
|
||||||
|
|
||||||
from .camera import RawCameraDependency as Camera
|
from .camera import RawCameraDependency as Camera
|
||||||
from .camera import CameraDependency as WrappedCamera
|
from .camera import CameraDependency as WrappedCamera
|
||||||
from .stage import StageDependency as Stage
|
from .stage import StageDependency as Stage
|
||||||
from .capture import CaptureThing
|
from .capture import RawCaptureDependency as CaptureDep
|
||||||
|
|
||||||
CaptureDep = direct_thing_client_dependency(CaptureThing, "/capture/")
|
|
||||||
|
|
||||||
SETTLING_TIME = 0.3
|
SETTLING_TIME = 0.3
|
||||||
BACKLASH_CORRECTION = 250
|
BACKLASH_CORRECTION = 250
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import time
|
|
||||||
import piexif
|
import piexif
|
||||||
import json
|
import json
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
from labthings_fastapi.dependencies.metadata import GetThingStates
|
from labthings_fastapi.dependencies.metadata import GetThingStates
|
||||||
from labthings_fastapi.thing import Thing
|
from labthings_fastapi.thing import Thing
|
||||||
from labthings_fastapi.decorators import thing_action
|
from labthings_fastapi.dependencies.raw_thing import raw_thing_dependency
|
||||||
from .camera import CameraDependency as CamDep
|
from .camera import CameraDependency as CamDep
|
||||||
from labthings_fastapi.dependencies.invocation import (
|
from labthings_fastapi.dependencies.invocation import (
|
||||||
InvocationLogger,
|
InvocationLogger,
|
||||||
|
|
@ -20,35 +20,9 @@ class CaptureThing(Thing):
|
||||||
"""A temporary Thing to handle capturing to disk with associated metadata
|
"""A temporary Thing to handle capturing to disk with associated metadata
|
||||||
Will be moved to the camera Thing or dependency in due course"""
|
Will be moved to the camera Thing or dependency in due course"""
|
||||||
|
|
||||||
@thing_action
|
def _capture_image(
|
||||||
def _capture_and_save(
|
self, cam: CamDep, metadata_getter: GetThingStates
|
||||||
self,
|
) -> tuple[np.ndarray, dict]:
|
||||||
jpeg_path: str,
|
|
||||||
cam: CamDep,
|
|
||||||
logger: InvocationLogger,
|
|
||||||
metadata_getter: GetThingStates,
|
|
||||||
) -> None:
|
|
||||||
"""Capture an image and save it to disk
|
|
||||||
|
|
||||||
This will set the event `acquired` once the image has been acquired, so
|
|
||||||
that the stage may be moved while it's saved.
|
|
||||||
"""
|
|
||||||
capture_start = time.time()
|
|
||||||
image, metadata = self._capture_image(
|
|
||||||
cam,
|
|
||||||
metadata_getter,
|
|
||||||
)
|
|
||||||
acquisition_time = time.time()
|
|
||||||
self._save_capture(jpeg_path, image, metadata, logger)
|
|
||||||
save_time = time.time()
|
|
||||||
acquisition_duration = round(acquisition_time - capture_start, 1)
|
|
||||||
saving_duration = round(save_time - acquisition_time, 1)
|
|
||||||
logger.info(
|
|
||||||
f"Acquired {jpeg_path} in {acquisition_duration}s then {saving_duration}s saving to disk"
|
|
||||||
)
|
|
||||||
|
|
||||||
@thing_action
|
|
||||||
def _capture_image(self, cam, metadata_getter):
|
|
||||||
"""Capture an image in memory and return it with metadata
|
"""Capture an image in memory and return it with metadata
|
||||||
CaptureError raised if the capture fails for any reason
|
CaptureError raised if the capture fails for any reason
|
||||||
returns tuple with numpy array of image data, and dict of metadata
|
returns tuple with numpy array of image data, and dict of metadata
|
||||||
|
|
@ -60,11 +34,10 @@ class CaptureThing(Thing):
|
||||||
raise CaptureError("An error occurred while capturing") from e
|
raise CaptureError("An error occurred while capturing") from e
|
||||||
return image, metadata
|
return image, metadata
|
||||||
|
|
||||||
@thing_action
|
|
||||||
def _save_capture(
|
def _save_capture(
|
||||||
self,
|
self,
|
||||||
jpeg_path: str,
|
jpeg_path: str,
|
||||||
image,
|
image: Image.Image,
|
||||||
metadata: dict,
|
metadata: dict,
|
||||||
logger: InvocationLogger,
|
logger: InvocationLogger,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
@ -88,3 +61,6 @@ class CaptureThing(Thing):
|
||||||
logger.warning(f"Failed to add metadata to {jpeg_path}")
|
logger.warning(f"Failed to add metadata to {jpeg_path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise IOError(f"An error occurred while saving {jpeg_path}") from e
|
raise IOError(f"An error occurred while saving {jpeg_path}") from e
|
||||||
|
|
||||||
|
|
||||||
|
RawCaptureDependency = raw_thing_dependency(CaptureThing)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue