etho-python 1.2.6__py3-none-any.whl → 1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,34 +12,7 @@ from typing import Optional, Dict, Any
12
12
  import tables
13
13
 
14
14
 
15
- try:
16
- import cv2
17
-
18
- cv2_import_error = None
19
- except ImportError as cv2_import_error:
20
- pass
21
-
22
- try:
23
- import av
24
-
25
- av_import_error = None
26
- except ImportError as av_import_error:
27
- pass
28
-
29
- try:
30
- import ffmpegcv
31
-
32
- ffmpegcv_import_error = None
33
- except ImportError as ffmpegcv_import_error:
34
- pass
35
-
36
- try:
37
- from vidgear.gears import WriteGear
38
-
39
- vidgear_import_error = None
40
- except ImportError as vidgear_import_error:
41
- pass
42
-
15
+ # Import video libraries inside their callbacks to avoid mixing FFmpeg builds on macOS.
43
16
  try:
44
17
  from qtpy import QtWidgets
45
18
  import pyqtgraph as pg
@@ -91,13 +64,13 @@ class ImageDisplayCV2(ImageCallback):
91
64
  def __init__(self, data_source, poll_timeout=0.01, **kwargs):
92
65
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
93
66
 
94
- if cv2_import_error is not None:
95
- logger.exception("Could not import cv2. Aborting!", exc_info=cv2_import_error)
96
- raise cv2_import_error
67
+ import cv2
68
+
69
+ self.cv2 = cv2
97
70
 
98
71
  logger.info("setting up disp")
99
- cv2.namedWindow("display")
100
- cv2.resizeWindow("display", self.frame_width, self.frame_height)
72
+ self.cv2.namedWindow("display")
73
+ self.cv2.resizeWindow("display", self.frame_width, self.frame_height)
101
74
 
102
75
  @classmethod
103
76
  def make_concurrent(cls, comms="pipe", **kwargs):
@@ -109,12 +82,12 @@ class ImageDisplayCV2(ImageCallback):
109
82
  else:
110
83
  image, timestamp = data
111
84
 
112
- cv2.imshow("display", image)
113
- cv2.waitKey(1)
85
+ self.cv2.imshow("display", image)
86
+ self.cv2.waitKey(1)
114
87
 
115
88
  def _cleanup(self):
116
89
  logger.info("closing display")
117
- cv2.destroyWindow("display")
90
+ self.cv2.destroyWindow("display")
118
91
  super()._cleanup()
119
92
 
120
93
 
@@ -124,8 +97,9 @@ class ImageDisplayPQG(ImageCallback):
124
97
  FRIENDLY_NAME = "disp_fast"
125
98
  TIMESTAMPS_ONLY = False
126
99
 
127
- def __init__(self, data_source, *, poll_timeout=0.01, **kwargs):
100
+ def __init__(self, data_source, *, poll_timeout=0.01, row_major_bgr=False, **kwargs):
128
101
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
102
+ self.row_major_bgr = row_major_bgr
129
103
 
130
104
  if pyqtgraph_import_error is not None:
131
105
  logger.exception("Could not import pyqtgraph. Aborting!", exc_info=pyqtgraph_import_error)
@@ -139,7 +113,10 @@ class ImageDisplayPQG(ImageCallback):
139
113
  # set up window and subplots
140
114
  self.app = QtWidgets.QApplication([])
141
115
  self.win = RawImageWidget(scaled=True)
142
- self.win.resize(self.frame_width, self.frame_height)
116
+ if self.row_major_bgr:
117
+ self.win.resize(self.frame_height, self.frame_width)
118
+ else:
119
+ self.win.resize(self.frame_width, self.frame_height)
143
120
  self.win.show()
144
121
  self.app.processEvents()
145
122
 
@@ -153,6 +130,10 @@ class ImageDisplayPQG(ImageCallback):
153
130
  else:
154
131
  image, timestamp = data
155
132
 
133
+ if self.row_major_bgr:
134
+ if pg.getConfigOption("imageAxisOrder") == "col-major":
135
+ image = image.swapaxes(0, 1)
136
+ image = np.ascontiguousarray(image[..., ::-1])
156
137
  self.win.setImage(image)
157
138
  self.app.processEvents()
158
139
 
@@ -175,8 +156,6 @@ class ImageWriterCV2(ImageCallback):
175
156
  - openh264 (?) form the cisco github page
176
157
  - opencv: `mamba install opencv -c conda-forge`
177
158
 
178
- Raises:
179
- cv2_import_error: If cv2 (opencv) could not be imported.
180
159
 
181
160
  Args:
182
161
  ImageCallback (_type_): _description_
@@ -189,9 +168,7 @@ class ImageWriterCV2(ImageCallback):
189
168
  def __init__(self, data_source, *, poll_timeout=0.01, **kwargs):
190
169
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
191
170
 
192
- if cv2_import_error is not None:
193
- logger.exception("Could not import cv2. Aborting!", exc_info=cv2_import_error)
194
- raise cv2_import_error
171
+ import cv2
195
172
 
196
173
  self.vw = cv2.VideoWriter()
197
174
  self.vw.open(
@@ -228,9 +205,9 @@ class ImageWriterPyAV(ImageCallback):
228
205
  def __init__(self, data_source, *, poll_timeout=0.01, **kwargs):
229
206
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
230
207
 
231
- if av_import_error is not None:
232
- logger.exception("Could not import PyAV. Aborting!", exc_info=av_import_error)
233
- raise av_import_error
208
+ import av
209
+
210
+ self.av = av
234
211
 
235
212
  self.container = None
236
213
 
@@ -241,14 +218,14 @@ class ImageWriterPyAV(ImageCallback):
241
218
  image, timestamp = data
242
219
 
243
220
  if self.container is None:
244
- self.container = av.open(self.file_name + self.SUFFIX, "w")
221
+ self.container = self.av.open(self.file_name + self.SUFFIX, "w")
245
222
  self.stream = self.container.add_stream("libx264", rate=Fraction(str(self.frame_rate)))
246
223
  self.stream.width = image.shape[1]
247
224
  self.stream.height = image.shape[0]
248
225
  self.stream.pix_fmt = "yuv420p"
249
226
 
250
227
  frame_format = "gray" if image.ndim == 2 else "bgr24"
251
- frame = av.VideoFrame.from_ndarray(image, format=frame_format)
228
+ frame = self.av.VideoFrame.from_ndarray(image, format=frame_format)
252
229
  for packet in self.stream.encode(frame):
253
230
  self.container.mux(packet)
254
231
 
@@ -271,8 +248,6 @@ class ImageWriterFCV(ImageCallback):
271
248
  - ffmpeg>=6 (?): `mamba install ffmpeg>=6 -c conda-forge
272
249
  - ffmpegcv: `pip install ffmpegcv`
273
250
 
274
- Raises:
275
- ffmpegcv_import_error: If ffmpegcv could not be imported.
276
251
 
277
252
  Args:
278
253
  ImageCallback (_type_): _description_
@@ -285,9 +260,7 @@ class ImageWriterFCV(ImageCallback):
285
260
  def __init__(self, data_source, *, poll_timeout=0.01, **kwargs):
286
261
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
287
262
 
288
- if ffmpegcv_import_error is not None:
289
- logger.exception("Could not import ffmpegcv. Aborting!", exc_info=ffmpegcv_import_error)
290
- raise ffmpegcv_import_error
263
+ import ffmpegcv
291
264
 
292
265
  # self.vw = ffmpegcv.VideoWriter()
293
266
  self.vw = ffmpegcv.VideoWriter(
@@ -345,10 +318,9 @@ class ImageWriterCVR(ImageCallback):
345
318
  ):
346
319
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
347
320
 
348
- if vidgear_import_error is not None:
349
- logger.exception("No!", exc_info=vidgear_import_error)
350
- self._cleanup()
351
- raise vidgear_import_error
321
+ from vidgear.gears import WriteGear
322
+
323
+ self.WriteGear = WriteGear
352
324
 
353
325
  self.video_count = 0
354
326
  self.frame_count = 0
@@ -358,9 +330,9 @@ class ImageWriterCVR(ImageCallback):
358
330
  if ffmpeg_params is not None:
359
331
  self.output_params.update(ffmpeg_params)
360
332
  try:
361
- self.vw = WriteGear(output_filename=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
333
+ self.vw = self.WriteGear(output_filename=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
362
334
  except:
363
- self.vw = WriteGear(output=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
335
+ self.vw = self.WriteGear(output=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
364
336
 
365
337
  def _loop(self, data):
366
338
  if hasattr(self.data_source, "WHOAMI") and self.data_source.WHOAMI == "array":
@@ -376,9 +348,9 @@ class ImageWriterCVR(ImageCallback):
376
348
  self.frame_count = 0
377
349
  self.video_count += 1
378
350
  try:
379
- self.vw = WriteGear(output_filename=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
351
+ self.vw = self.WriteGear(output_filename=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
380
352
  except:
381
- self.vw = WriteGear(output=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
353
+ self.vw = self.WriteGear(output=self.file_name + f"_{self.video_count:06d}" + self.SUFFIX, **self.output_params)
382
354
 
383
355
  def _cleanup(self):
384
356
  try:
@@ -410,8 +382,6 @@ class ImageWriterVidGear(ImageCallback):
410
382
  -crf: 16
411
383
  ```
412
384
 
413
- Raises:
414
- vidgear_import_error: If VidGear could not be imported.
415
385
  """
416
386
 
417
387
  SUFFIX: str = ".avi"
@@ -421,10 +391,7 @@ class ImageWriterVidGear(ImageCallback):
421
391
  def __init__(self, data_source, *, poll_timeout=0.01, ffmpeg_params: Optional[Dict[str, Any]] = None, **kwargs):
422
392
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
423
393
 
424
- if vidgear_import_error is not None:
425
- logger.exception("Failed to import VidGear. Closing!", exc_info=vidgear_import_error)
426
- self._cleanup()
427
- # raise vidgear_import_error
394
+ from vidgear.gears import WriteGear
428
395
 
429
396
  self.output_params = {"-input_framerate": self.frame_rate, "-r": self.frame_rate}
430
397
  if ffmpeg_params is not None:
@@ -477,6 +444,9 @@ class ImageWriterVPF(ImageCallback):
477
444
  logging.exception("Could not import PyNvCodec. Quitting.", exc_info=e)
478
445
  self._cleanup()
479
446
 
447
+ import cv2
448
+
449
+ self.cv2 = cv2
480
450
  gpuID = 0
481
451
  self.encFile = open(self.file_name + self.SUFFIX, "wb")
482
452
  # self.nvEnc = nvc.PyNvEncoder({'rc':'vbr_hq','profile': 'high', 'cq': '10', 'codec': 'h264', 'bf':'3',
@@ -501,7 +471,7 @@ class ImageWriterVPF(ImageCallback):
501
471
 
502
472
  def _loop(self, data):
503
473
  image, timestamp = data
504
- rawFrameYUV420 = cv2.cvtColor(image, cv2.COLOR_RGB2YUV_I420) # convert to YUV420 - nvenc can't handle RGB inputs
474
+ rawFrameYUV420 = self.cv2.cvtColor(image, self.cv2.COLOR_RGB2YUV_I420) # convert to YUV420 - nvenc can't handle RGB inputs
505
475
  rawSurfaceYUV420 = self.nvUpl.UploadSingleFrame(rawFrameYUV420) # upload YUV420 frame to GPU
506
476
  if rawSurfaceYUV420.Empty():
507
477
  return # break
@@ -703,9 +673,13 @@ class ImageDisplayCenterBackCV2(ImageCallback):
703
673
  def __init__(self, data_source, poll_timeout=0.01, center_x=0, center_y=0, **kwargs):
704
674
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
705
675
 
676
+ import cv2
677
+
678
+ self.cv2 = cv2
679
+
706
680
  logger.info("setting up disp")
707
- cv2.namedWindow("display")
708
- cv2.resizeWindow("display", self.frame_width, self.frame_height)
681
+ self.cv2.namedWindow("display")
682
+ self.cv2.resizeWindow("display", self.frame_width, self.frame_height)
709
683
 
710
684
  if center_x != 0:
711
685
  self.center_x = center_x
@@ -728,22 +702,22 @@ class ImageDisplayCenterBackCV2(ImageCallback):
728
702
  else:
729
703
  image, timestamp = data
730
704
 
731
- image = cv2.rectangle(
705
+ image = self.cv2.rectangle(
732
706
  image,
733
707
  (self.frame_height - 95, self.frame_width),
734
708
  (95, self.frame_width - 45),
735
709
  color=self.color,
736
710
  thickness=self.thickness,
737
711
  ) # ball region
738
- image = cv2.line(image, (60, self.center_y), (self.frame_height - 60, self.center_y), self.color, self.thickness) # y-axis cross
739
- image = cv2.line(image, (self.center_x, 60), (self.center_x, self.frame_width - 60), self.color, self.thickness) # x-axis cross
712
+ image = self.cv2.line(image, (60, self.center_y), (self.frame_height - 60, self.center_y), self.color, self.thickness) # y-axis cross
713
+ image = self.cv2.line(image, (self.center_x, 60), (self.center_x, self.frame_width - 60), self.color, self.thickness) # x-axis cross
740
714
  # image = cv2.rectangle(image, (self.center_x-40,self.center_y-35), (self.center_x+20,self.center_y+35), color=self.color, thickness=self.thickness) # fly
741
- cv2.imshow("display", image)
742
- cv2.waitKey(1)
715
+ self.cv2.imshow("display", image)
716
+ self.cv2.waitKey(1)
743
717
 
744
718
  def _cleanup(self):
745
719
  logger.info("closing display")
746
- cv2.destroyWindow("display")
720
+ self.cv2.destroyWindow("display")
747
721
  super()._cleanup()
748
722
 
749
723
 
@@ -756,9 +730,13 @@ class ImageDisplayCenterTopCV2(ImageCallback):
756
730
  def __init__(self, data_source, poll_timeout=0.01, circ_center_x=0, circ_center_y=0, circ_r=0, **kwargs):
757
731
  super().__init__(data_source=data_source, poll_timeout=poll_timeout, **kwargs)
758
732
 
733
+ import cv2
734
+
735
+ self.cv2 = cv2
736
+
759
737
  logger.info("setting up disp")
760
- cv2.namedWindow("display")
761
- cv2.resizeWindow("display", self.frame_width, self.frame_height)
738
+ self.cv2.namedWindow("display")
739
+ self.cv2.resizeWindow("display", self.frame_width, self.frame_height)
762
740
 
763
741
  # properties of drawn circle
764
742
  if circ_center_x != 0:
@@ -809,18 +787,18 @@ class ImageDisplayCenterTopCV2(ImageCallback):
809
787
  else:
810
788
  image, timestamp = data
811
789
 
812
- image = cv2.circle(image, (self.circ_center_x, self.circ_center_y), self.circ_r, self.circ_color, self.circ_thickness)
813
- image = cv2.line(image, (self.circ_center_x, 0), (self.circ_center_x, self.frame_width), self.circ_color, self.circ_thickness)
814
- image = cv2.line(image, (0, self.circ_center_y), (self.frame_height, self.circ_center_y), self.circ_color, self.circ_thickness)
815
- image = cv2.rectangle(image, self.flyhead_topleft, self.flyhead_bottomright, color=self.circ_color, thickness=self.circ_thickness)
816
- image = cv2.rectangle(image, self.flybody_topleft, self.flybody_bottomright, color=self.circ_color, thickness=self.circ_thickness)
817
- image = cv2.rectangle(image, self.needle_topleft, self.needle_bottomright, color=self.needle_color, thickness=self.circ_thickness)
818
- cv2.imshow("display", image)
819
- cv2.waitKey(1)
790
+ image = self.cv2.circle(image, (self.circ_center_x, self.circ_center_y), self.circ_r, self.circ_color, self.circ_thickness)
791
+ image = self.cv2.line(image, (self.circ_center_x, 0), (self.circ_center_x, self.frame_width), self.circ_color, self.circ_thickness)
792
+ image = self.cv2.line(image, (0, self.circ_center_y), (self.frame_height, self.circ_center_y), self.circ_color, self.circ_thickness)
793
+ image = self.cv2.rectangle(image, self.flyhead_topleft, self.flyhead_bottomright, color=self.circ_color, thickness=self.circ_thickness)
794
+ image = self.cv2.rectangle(image, self.flybody_topleft, self.flybody_bottomright, color=self.circ_color, thickness=self.circ_thickness)
795
+ image = self.cv2.rectangle(image, self.needle_topleft, self.needle_bottomright, color=self.needle_color, thickness=self.circ_thickness)
796
+ self.cv2.imshow("display", image)
797
+ self.cv2.waitKey(1)
820
798
 
821
799
  def _cleanup(self):
822
800
  logger.info("closing display")
823
- cv2.destroyWindow("display")
801
+ self.cv2.destroyWindow("display")
824
802
  super()._cleanup()
825
803
 
826
804
 
@@ -4,6 +4,7 @@ from .ximea import Ximea
4
4
  from .basler import Basler
5
5
  from .hamamatsu import Hamamatsu
6
6
  from .dummy import Dummy
7
+ from .webcam import Webcam, list_webcams
7
8
 
8
9
  make = {
9
10
  "Spinnaker_OLD": Spinnaker_OLD,
@@ -12,4 +13,5 @@ make = {
12
13
  "Basler": Basler,
13
14
  "Hamamatsu": Hamamatsu,
14
15
  "Dummy": Dummy,
16
+ "Webcam": Webcam,
15
17
  }
@@ -9,6 +9,10 @@ def gray2rgb(image: np.ndarray) -> np.ndarray:
9
9
 
10
10
 
11
11
  class BaseCam:
12
+ # Physical backends can list (protocol identifier, display name) pairs
13
+ # before a camera is opened.
14
+ enumerate_devices = None
15
+
12
16
  def __init__(self, serialnumber: int):
13
17
  """[summary]
14
18
 
@@ -8,13 +8,20 @@ try:
8
8
  import pypylon.pylon as py
9
9
 
10
10
  pylon_error = None
11
- except ImportError as pylon_error:
12
- pass
11
+ except Exception as exc:
12
+ pylon_error = exc
13
13
 
14
14
 
15
15
  class Basler(BaseCam):
16
16
  NAME = "BAS"
17
17
 
18
+ @staticmethod
19
+ def enumerate_devices():
20
+ if pylon_error is not None:
21
+ raise pylon_error
22
+
23
+ return [(device.GetSerialNumber(), device.GetModelName()) for device in py.TlFactory.GetInstance().EnumerateDevices()]
24
+
18
25
  def __init__(self, serialnumber):
19
26
  if pylon_error is not None:
20
27
  raise pylon_error
@@ -2,7 +2,7 @@ import numpy as np
2
2
  from typing import Tuple, Optional
3
3
  import time
4
4
  from .base import BaseCam
5
- import cv2
5
+ from PIL import Image, ImageDraw
6
6
 
7
7
 
8
8
  class Dummy(BaseCam):
@@ -39,18 +39,10 @@ class Dummy(BaseCam):
39
39
  image_timestamp = system_timestamp - self._t0
40
40
 
41
41
  image = np.random.randint(0, 64, size=(self._roi[2], self._roi[3], 3), dtype=np.uint8)
42
- image = cv2.putText(
43
- image,
44
- f"{image_timestamp: 1.3f} s",
45
- org=(20, 100),
46
- fontFace=cv2.FONT_HERSHEY_SIMPLEX,
47
- fontScale=1,
48
- color=(128, 64, 96),
49
- thickness=2,
50
- lineType=2,
51
- )
52
-
53
- return image, image_timestamp, system_timestamp
42
+ preview = Image.fromarray(image)
43
+ ImageDraw.Draw(preview).text((20, 85), f"{image_timestamp: 1.3f} s", fill=(128, 64, 96))
44
+
45
+ return np.array(preview), image_timestamp, system_timestamp
54
46
 
55
47
  def _estimate_timestamp_offset(self) -> float:
56
48
  """[summary]
@@ -6,7 +6,7 @@ from .base import BaseCam
6
6
  try:
7
7
  from pylablib.devices import DCAM
8
8
  hamamatsu_error = None
9
- except ImportError as e:
9
+ except Exception as e:
10
10
  hamamatsu_error = e
11
11
 
12
12
 
@@ -14,6 +14,13 @@ class Hamamatsu(BaseCam):
14
14
 
15
15
  NAME = "HAM"
16
16
 
17
+ @staticmethod
18
+ def enumerate_devices():
19
+ if hamamatsu_error is not None:
20
+ raise hamamatsu_error
21
+
22
+ return [(index, "Hamamatsu DCAM camera") for index in range(DCAM.get_cameras_number())]
23
+
17
24
  def __init__(self, serialnumber):
18
25
  if hamamatsu_error is not None:
19
26
  raise hamamatsu_error
@@ -8,13 +8,39 @@ try:
8
8
  import PySpin
9
9
 
10
10
  pyspin_error = None
11
- except ImportError as pyspin_error:
12
- pass
11
+ except Exception as exc:
12
+ pyspin_error = exc
13
13
 
14
14
 
15
15
  class Spinnaker(BaseCam):
16
16
  NAME = "SPN"
17
17
 
18
+ @staticmethod
19
+ def enumerate_devices():
20
+ if pyspin_error is not None:
21
+ raise pyspin_error
22
+
23
+ devices = []
24
+ system = PySpin.System.GetInstance()
25
+ try:
26
+ cameras = system.GetCameras()
27
+ try:
28
+ for index in range(cameras.GetSize()):
29
+ camera = cameras.GetByIndex(index)
30
+ try:
31
+ nodes = camera.GetTLDeviceNodeMap()
32
+ serial = PySpin.CStringPtr(nodes.GetNode("DeviceSerialNumber")).GetValue()
33
+ model = PySpin.CStringPtr(nodes.GetNode("DeviceModelName"))
34
+ name = model.GetValue() if PySpin.IsReadable(model) else "FLIR camera"
35
+ devices.append((serial, name))
36
+ finally:
37
+ del camera
38
+ finally:
39
+ cameras.Clear()
40
+ finally:
41
+ system.ReleaseInstance()
42
+ return devices
43
+
18
44
  def __init__(self, serialnumber):
19
45
  if pyspin_error is not None:
20
46
  raise pyspin_error
@@ -8,8 +8,8 @@ try:
8
8
  import PySpin
9
9
 
10
10
  pyspin_error = None
11
- except ImportError as pyspin_error:
12
- pass
11
+ except Exception as exc:
12
+ pyspin_error = exc
13
13
 
14
14
 
15
15
  class Spinnaker_OLD(BaseCam):
@@ -34,7 +34,7 @@ class Spinnaker_OLD(BaseCam):
34
34
  self.c.ChunkModeActive.SetValue(True)
35
35
  self.c.ChunkSelector.SetValue(PySpin.ChunkSelector_Timestamp)
36
36
  self.c.ChunkEnable.SetValue(True)
37
- self.timestamp_offset = 0 # self._estimate_timestamp_offset()
37
+ self.timestamp_offset = self._estimate_timestamp_offset()
38
38
 
39
39
  # set pixel format
40
40
  self.c.PixelFormat.SetValue(PySpin.PixelFormat_Mono8)
@@ -240,19 +240,17 @@ class Spinnaker_OLD(BaseCam):
240
240
  return value
241
241
 
242
242
  def _estimate_timestamp_offset(self, timestamp_offset_iterations=10):
243
- """Gets offset between system time and timestamps."""
244
- # This method is required because the timestamp stored in the camera is relative to when it was powered on, so an
245
- # offset needs to be applied to get it into epoch time; from tests I’ve done, this appears to be accurate to ~1e-3
246
- # seconds.
243
+ """Estimate the system-minus-camera offset using legacy timestamp nodes."""
244
+ seconds_per_tick = self.getattr("TimestampIncrement") / 1e9
247
245
  timestamp_offsets = []
248
- for i in range(timestamp_offset_iterations):
249
- # Latch timestamp. This basically “freezes” the current camera timer into a variable that can be read with
250
- self.c.TimestampLatch.Execute()
251
- system_time = time.time()
252
- # Compute timestamp offset in seconds; note that timestamp latch value is in nanoseconds
253
- timestamp_offset = system_time - self.c.TimestampLatchValue.GetValue() / 1e9
254
- timestamp_offsets.append(timestamp_offset)
255
- # Return the median value
246
+ for _ in range(timestamp_offset_iterations):
247
+ # Bracket the latch to account for the command's round-trip latency.
248
+ system_before = time.time()
249
+ self.getattr("TimestampLatch")()
250
+ system_after = time.time()
251
+ camera_time = self.getattr("Timestamp") * seconds_per_tick
252
+ system_time = system_before + (system_after - system_before) / 2
253
+ timestamp_offsets.append(system_time - camera_time)
256
254
  return np.median(timestamp_offsets)
257
255
 
258
256
  def enable_gpio_strobe(self):