From a7b298889a2184f96d5b4f89f17b57e80165f119 Mon Sep 17 00:00:00 2001 From: Julian Stirling Date: Sat, 18 Jul 2026 13:38:26 -0300 Subject: [PATCH] Re-calculate time offset from CPU time to real time on each frame --- .../things/camera/picamera.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/openflexure_microscope_server/things/camera/picamera.py b/src/openflexure_microscope_server/things/camera/picamera.py index f3f0ff1d..b8de8442 100644 --- a/src/openflexure_microscope_server/things/camera/picamera.py +++ b/src/openflexure_microscope_server/things/camera/picamera.py @@ -86,8 +86,6 @@ class PicameraStreamOutput(Output): Output.__init__(self) self.stream = stream self.encoder = encoder - # The difference between unix time and the CPU time - self.cpu_dt = time.time() - time.monotonic() def outputframe( self, @@ -101,10 +99,13 @@ class PicameraStreamOutput(Output): if timestamp is None or self.encoder.firsttimestamp is None: frame_timestamp = None else: + # The difference between unix time and the CPU time. Do not cache this + # number as it can change with clock slew or other time adjustments. + cpu_dt = time.time() - time.monotonic() # Reconstruct full CPU time of the frame in us timestamp += self.encoder.firsttimestamp # Convert to datetime - frame_timestamp = datetime.fromtimestamp(self.cpu_dt + timestamp / 1e6) + frame_timestamp = datetime.fromtimestamp(cpu_dt + timestamp / 1e6) self.stream.add_frame(frame, frame_timestamp)