Swap stream for scan, target_resolution as arg

This commit is contained in:
jaknapper 2025-05-07 19:34:13 +01:00 committed by Julian Stirling
parent f5c1ff2621
commit 9f2a131f1e
4 changed files with 27 additions and 42 deletions

View file

@ -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:

View file

@ -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")

View file

@ -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(

View file

@ -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