diff --git a/src/openflexure_microscope_server/things/camera/__init__.py b/src/openflexure_microscope_server/things/camera/__init__.py index 648264fd..e93fb938 100644 --- a/src/openflexure_microscope_server/things/camera/__init__.py +++ b/src/openflexure_microscope_server/things/camera/__init__.py @@ -16,7 +16,7 @@ from abc import ABC, abstractmethod from copy import deepcopy from datetime import datetime from types import TracebackType -from typing import Any, Literal, Mapping, Optional, Self +from typing import Any, Literal, Mapping, Optional, Self, Union, overload import numpy as np import piexif @@ -223,7 +223,7 @@ class MJPEGStreamWithTimestamp(lt.outputs.MJPEGStream): rather than take the time it is added into the ring buffer. """ - def add_frame(self, frame: bytes, timestamp: Optional[datetime]) -> None: + def add_frame(self, frame: bytes, timestamp: Optional[datetime] = None) -> None: """Add a JPEG to the MJPEG stream. Modify the standard function to have an option to send in the capture time. @@ -250,12 +250,23 @@ class MJPEGStreamWithTimestamp(lt.outputs.MJPEGStream): self.notify_new_frame, entry.index ) + class MJPEGStreamDescriptorWithTimestamp(lt.outputs.MJPEGStreamDescriptor): """Subclass the Labthings descriptor to return our own class.""" + @overload + def __get__(self, obj: Literal[None], type: type | None = None) -> Self: ... + + @overload def __get__( - self, obj: Optional[lt.Thing], type: type[lt.Thing] | None = None # noqa: A002 - ) -> MJPEGStreamWithTimestamp | Self: + self, obj: lt.Thing, type: type | None = None + ) -> MJPEGStreamWithTimestamp: ... + + def __get__( + self, + obj: Optional[lt.Thing], + type: type[lt.Thing] | None = None, # noqa: A002 + ) -> Union[MJPEGStreamWithTimestamp | Self]: """Return our own MJPEG Stream with timestamp.""" if obj is None: return self diff --git a/src/openflexure_microscope_server/things/camera/picamera.py b/src/openflexure_microscope_server/things/camera/picamera.py index fd00fc88..f3f0ff1d 100644 --- a/src/openflexure_microscope_server/things/camera/picamera.py +++ b/src/openflexure_microscope_server/things/camera/picamera.py @@ -101,10 +101,10 @@ class PicameraStreamOutput(Output): if timestamp is None or self.encoder.firsttimestamp is None: frame_timestamp = None else: - # Reconstruct full CPU time of the frame in ns + # Reconstruct full CPU time of the frame in us timestamp += self.encoder.firsttimestamp # Convert to datetime - frame_timestamp = datetime.fromtimestamp(self.cpu_dt + timestamp / 1e9) + frame_timestamp = datetime.fromtimestamp(self.cpu_dt + timestamp / 1e6) self.stream.add_frame(frame, frame_timestamp)