Capture_array can now use "full" stream
This commit is contained in:
parent
9d1385231d
commit
8bc0573ab1
3 changed files with 9 additions and 60 deletions
|
|
@ -311,25 +311,16 @@ class AutofocusThing(Thing):
|
||||||
stack_dir,
|
stack_dir,
|
||||||
f"{capture_count}.jpeg",
|
f"{capture_count}.jpeg",
|
||||||
)
|
)
|
||||||
metadata = metadata_getter()
|
|
||||||
if capture_method == "blob":
|
if capture_method == "blob":
|
||||||
capture.capture_jpeg(filename=jpeg_path, cam=cam)
|
capture.capture_jpeg(filename=jpeg_path, cam=cam)
|
||||||
elif capture_method == "hires_array":
|
elif capture_method == "hires_array" or capture_method == "array":
|
||||||
# A new way to get the full array
|
stream = 'main' if capture_method == "array" else "full"
|
||||||
img = capture.capture_highres_array()
|
|
||||||
_save_capture(
|
|
||||||
jpeg_path,
|
|
||||||
img,
|
|
||||||
metadata,
|
|
||||||
logger
|
|
||||||
)
|
|
||||||
elif capture_method == "array":
|
|
||||||
# The old capture and save a "main" res image
|
|
||||||
capture._capture_and_save(
|
capture._capture_and_save(
|
||||||
jpeg_path=jpeg_path,
|
jpeg_path=jpeg_path,
|
||||||
cam=cam,
|
cam=cam,
|
||||||
logger=logger,
|
logger=logger,
|
||||||
metadata_getter=metadata_getter,
|
metadata_getter=metadata_getter,
|
||||||
|
stream_name=stream,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Capture method must be one of "array", "blob" or "hires_array"')
|
raise ValueError('Capture method must be one of "array", "blob" or "hires_array"')
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class CameraProtocol(Protocol):
|
||||||
|
|
||||||
def capture_array(
|
def capture_array(
|
||||||
self,
|
self,
|
||||||
stream_name: Literal["main", "lores", "raw"] = "main",
|
stream_name: Literal["main", "lores", "raw", "full"] = "main",
|
||||||
) -> NDArray: ...
|
) -> NDArray: ...
|
||||||
|
|
||||||
def capture_jpeg(
|
def capture_jpeg(
|
||||||
|
|
@ -77,12 +77,6 @@ class CameraProtocol(Protocol):
|
||||||
"""Acquire one image from the preview stream and return its size"""
|
"""Acquire one image from the preview stream and return its size"""
|
||||||
...
|
...
|
||||||
|
|
||||||
def capture_highres_array(
|
|
||||||
self,
|
|
||||||
):
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
class BaseCamera(Thing):
|
class BaseCamera(Thing):
|
||||||
"""A Thing representing a camera
|
"""A Thing representing a camera
|
||||||
|
|
||||||
|
|
@ -171,16 +165,10 @@ class CameraStub(BaseCamera):
|
||||||
@thing_action
|
@thing_action
|
||||||
def capture_array(
|
def capture_array(
|
||||||
self,
|
self,
|
||||||
stream_name: Literal["main", "lores", "raw"] = "main",
|
stream_name: Literal["main", "lores", "raw", "full"] = "main",
|
||||||
) -> NDArray:
|
) -> NDArray:
|
||||||
raise NotImplementedError("Cameras must not inherit from CameraStub")
|
raise NotImplementedError("Cameras must not inherit from CameraStub")
|
||||||
|
|
||||||
@thing_action
|
|
||||||
def capture_highres_array(
|
|
||||||
self,
|
|
||||||
):
|
|
||||||
raise NotImplementedError("Cameras must not inherit from CameraStub")
|
|
||||||
|
|
||||||
@thing_action
|
@thing_action
|
||||||
def capture_jpeg(
|
def capture_jpeg(
|
||||||
self,
|
self,
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ class CaptureThing(Thing):
|
||||||
cam: CamDep,
|
cam: CamDep,
|
||||||
logger: InvocationLogger,
|
logger: InvocationLogger,
|
||||||
metadata_getter: GetThingStates,
|
metadata_getter: GetThingStates,
|
||||||
|
stream: str = "main",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Capture an image and save it to disk
|
"""Capture an image and save it to disk
|
||||||
|
|
||||||
|
|
@ -38,6 +39,7 @@ class CaptureThing(Thing):
|
||||||
image, metadata = self._capture_array(
|
image, metadata = self._capture_array(
|
||||||
cam,
|
cam,
|
||||||
metadata_getter,
|
metadata_getter,
|
||||||
|
stream=stream,
|
||||||
)
|
)
|
||||||
acquisition_time = time.time()
|
acquisition_time = time.time()
|
||||||
self._save_capture(jpeg_path, image, metadata, logger)
|
self._save_capture(jpeg_path, image, metadata, logger)
|
||||||
|
|
@ -55,19 +57,14 @@ class CaptureThing(Thing):
|
||||||
jpeg.save(filename)
|
jpeg.save(filename)
|
||||||
|
|
||||||
@thing_action
|
@thing_action
|
||||||
def capture_highres_array(self, cam: CamDep):
|
def _capture_array(self, cam: CamDep, metadata_getter: GetThingStates, stream: str = 'main'):
|
||||||
"""Return a full resolution stream array"""
|
|
||||||
return cam.capture_highres_array()
|
|
||||||
|
|
||||||
@thing_action
|
|
||||||
def _capture_array(self, cam: CamDep, metadata_getter: GetThingStates):
|
|
||||||
"""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
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
metadata = metadata_getter()
|
metadata = metadata_getter()
|
||||||
image = cam.capture_array(stream_name="main")[..., :3]
|
image = cam.capture_array(stream_name=stream)[..., :3]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise CaptureError("An error occurred while capturing") from e
|
raise CaptureError("An error occurred while capturing") from e
|
||||||
return image, metadata
|
return image, metadata
|
||||||
|
|
@ -99,30 +96,3 @@ 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
|
||||||
|
|
||||||
def _save_capture(
|
|
||||||
jpeg_path: str,
|
|
||||||
image: np.ndarray,
|
|
||||||
metadata: dict,
|
|
||||||
logger: InvocationLogger,
|
|
||||||
) -> None:
|
|
||||||
"""Saving the captured image and metadata to disk
|
|
||||||
logger warning (via InvocationLogger) is raised if metadata is failed to be added
|
|
||||||
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
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
exif_dict = piexif.load(jpeg_path)
|
|
||||||
exif_dict["Exif"][piexif.ExifIFD.UserComment] = json.dumps(
|
|
||||||
metadata
|
|
||||||
).encode("utf-8")
|
|
||||||
piexif.insert(piexif.dump(exif_dict), jpeg_path)
|
|
||||||
except: # noqa: E722
|
|
||||||
# We need to capture any exception as there are many reasons metadata
|
|
||||||
# might not be added. We warn rather than log the error.
|
|
||||||
logger.warning(f"Failed to add metadata to {jpeg_path}")
|
|
||||||
except Exception as e:
|
|
||||||
raise IOError(f"An error occurred while saving {jpeg_path}") from e
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue