Skip to content

Commit

Permalink
Make use of new key argument in bisect library.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfmanstout committed Feb 17, 2024
1 parent 60489ad commit 5d2b61a
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions .subtrees/gaze-ocr/gaze_ocr/talon.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ class TalonEyeTracker:
def __init__(self):
# Keep approximately 10 seconds of frames on Tobii 5
self._queue = deque(maxlen=1000)
# TODO: Remove once Talon is upgraded to Python 3.10 and bisect supports key arg.
self._ts_queue = deque(maxlen=1000)
self.is_connected = False
self.connect()

def _on_gaze(self, frame: tobii.GazeFrame):
if not frame or not frame.gaze:
return
self._queue.append(frame)
self._ts_queue.append(frame.ts)

def connect(self):
if self.is_connected:
Expand Down Expand Up @@ -148,14 +145,14 @@ def get_gaze_point_at_timestamp(self, timestamp):
if not self._queue:
print("No gaze history available")
return None
frame_index = bisect.bisect_left(self._ts_queue, timestamp)
frame_index = bisect.bisect_left(self._queue, timestamp, key=lambda f: f.ts)
if frame_index == len(self._queue):
frame_index -= 1
frame = self._queue[frame_index]
if abs(frame.ts - timestamp) > self.STALE_GAZE_THRESHOLD_SECONDS:
print(
"No gaze history available at that time: {}. Range: [{}, {}]".format(
timestamp, self._ts_queue[0], self._ts_queue[-1]
timestamp, self._queue[0].ts, self._queue[-1].ts
)
)
return None
Expand All @@ -165,10 +162,12 @@ def get_gaze_bounds_during_time_range(self, start_timestamp, end_timestamp):
if not self._queue:
print("No gaze history available")
return None
start_index = bisect.bisect_left(self._ts_queue, start_timestamp)
start_index = bisect.bisect_left(
self._queue, start_timestamp, key=lambda f: f.ts
)
if start_index == len(self._queue):
start_index -= 1
end_index = bisect.bisect_left(self._ts_queue, end_timestamp)
end_index = bisect.bisect_left(self._queue, end_timestamp, key=lambda f: f.ts)
if end_index == len(self._queue):
end_index -= 1
left = right = top = bottom = None
Expand Down

0 comments on commit 5d2b61a

Please sign in to comment.