From 9f2a131f1ed0d534451165b5d2e7f3c25caa67e9 Mon Sep 17 00:00:00 2001 From: jaknapper Date: Wed, 7 May 2025 19:34:13 +0100 Subject: [PATCH] Swap stream for scan, target_resolution as arg --- .../things/autofocus.py | 24 +++++++------------ .../things/camera/__init__.py | 23 +++++------------- .../things/capture.py | 18 ++++++++------ .../things/smart_scan.py | 4 ++-- 4 files changed, 27 insertions(+), 42 deletions(-) diff --git a/src/openflexure_microscope_server/things/autofocus.py b/src/openflexure_microscope_server/things/autofocus.py index b4825e08..f332278f 100644 --- a/src/openflexure_microscope_server/things/autofocus.py +++ b/src/openflexure_microscope_server/things/autofocus.py @@ -294,7 +294,7 @@ class AutofocusThing(Thing): capture: CaptureDep, images_dir: str, stack_dir: str, - capture_method: str = "array", + target_resolution: tuple[int, int], ) -> None: """Run a z stack, saving all images to stack_dir and copying the central image to stack_dir""" @@ -311,21 +311,13 @@ class AutofocusThing(Thing): stack_dir, f"{capture_count}.jpeg", ) - if capture_method == "blob": - capture.capture_jpeg(filename=jpeg_path, cam=cam) - elif capture_method == "hires_array" or capture_method == "array": - stream = "main" if capture_method == "array" else "full" - capture._capture_and_save( - jpeg_path=jpeg_path, - cam=cam, - logger=logger, - metadata_getter=metadata_getter, - stream_name=stream, - ) - else: - raise ValueError( - 'Capture method must be one of "array", "blob" or "hires_array"' - ) + capture._capture_and_save( + jpeg_path=jpeg_path, + cam=cam, + logger=logger, + metadata_getter=metadata_getter, + target_resolution=target_resolution, + ) # If the stack isn't complete yet, move if capture_count + 1 < images_to_capture: diff --git a/src/openflexure_microscope_server/things/camera/__init__.py b/src/openflexure_microscope_server/things/camera/__init__.py index ac471031..6c18765a 100644 --- a/src/openflexure_microscope_server/things/camera/__init__.py +++ b/src/openflexure_microscope_server/things/camera/__init__.py @@ -79,14 +79,9 @@ class CameraProtocol(Protocol): """Acquire one image from the preview stream and return its size""" ... - def start_highres_streaming(self) -> None: - """Start (or stop and restart) the camera in a configuration for low - res streaming and instant high res captures""" - ... - - def start_streaming(self) -> None: - """Start (or stop and restart) the camera in the default streaming - configuration which balances stream and capture quality""" + def start_streaming(self, main_resolution) -> None: + """Start (or stop and restart) the camera with the given resolution + for the main stream""" ... @@ -194,15 +189,9 @@ class CameraStub(BaseCamera): raise NotImplementedError("Cameras must not inherit from CameraStub") @thing_action - def start_highres_streaming(self): - """Start (or stop and restart) the camera in a configuration for low - res streaming and instant high res captures""" - raise NotImplementedError("Cameras must not inherit from CameraStub") - - @thing_action - def start_streaming(self) -> None: - """Start (or stop and restart) the camera in the default streaming - configuration which balances stream and capture quality""" + def start_streaming(self, main_resolution) -> None: + """Start (or stop and restart) the camera with the given resolution + for the main stream""" raise NotImplementedError("Cameras must not inherit from CameraStub") diff --git a/src/openflexure_microscope_server/things/capture.py b/src/openflexure_microscope_server/things/capture.py index ebcd7312..e353ecba 100644 --- a/src/openflexure_microscope_server/things/capture.py +++ b/src/openflexure_microscope_server/things/capture.py @@ -28,7 +28,7 @@ class CaptureThing(Thing): cam: CamDep, logger: InvocationLogger, metadata_getter: GetThingStates, - stream_name: str = "main", + target_resolution: tuple[int, int], ) -> None: """Capture an image and save it to disk @@ -39,7 +39,7 @@ class CaptureThing(Thing): image, metadata = self._capture_array( cam, metadata_getter, - stream=stream_name, + target_resolution=target_resolution, logger=logger, ) acquisition_time = time.time() @@ -81,7 +81,7 @@ class CaptureThing(Thing): cam: CamDep, metadata_getter: GetThingStates, logger: InvocationLogger, - stream: str = "main", + target_resolution: tuple[int, int] = (1640, 1232), ): """Capture an image in memory and return it with metadata CaptureError raised if the capture fails for any reason @@ -90,7 +90,13 @@ class CaptureThing(Thing): for capture_attempts in range(5): try: metadata = metadata_getter() - image = cam.capture_array(stream_name=stream, wait=5)[..., :3] + image = Image.fromarray( + cam.capture_array(stream_name="main", wait=5)[..., :3].astype( + "uint8" + ), + "RGB", + ) + image = image.resize(target_resolution, Image.LANCZOS) return image, metadata except TimeoutError: logger.warning( @@ -110,9 +116,7 @@ class CaptureThing(Thing): IOError is raised if the file cannot be saved nothing is returned on success""" try: - Image.fromarray(image.astype("uint8"), "RGB").save( - jpeg_path, quality=95, subsampling=0 - ) + image.save(jpeg_path, quality=95, subsampling=0) try: exif_dict = piexif.load(jpeg_path) exif_dict["Exif"][piexif.ExifIFD.UserComment] = json.dumps( diff --git a/src/openflexure_microscope_server/things/smart_scan.py b/src/openflexure_microscope_server/things/smart_scan.py index f82fbf43..af7f6569 100644 --- a/src/openflexure_microscope_server/things/smart_scan.py +++ b/src/openflexure_microscope_server/things/smart_scan.py @@ -182,7 +182,7 @@ class SmartScanThing(Thing): self._scan_images_taken = 0 self._stitch_resize = 1 - self._cam.start_highres_streaming() + self._cam.start_streaming(main_resolution=(3280, 2464)) # Don't set self._scan_data dictionary. This is done at the start of _run_scan @@ -692,7 +692,7 @@ class SmartScanThing(Thing): self._autofocus.run_z_stack( images_dir=self._ongoing_scan_images_dir, stack_dir=site_folder, - capture_method="blob", + target_resolution=(1640, 1232), ) # increment capure counter as thread has completed