diff options
Diffstat (limited to 'src/argaze/utils/tobii_stream_display.py')
-rw-r--r-- | src/argaze/utils/tobii_stream_display.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/argaze/utils/tobii_stream_display.py b/src/argaze/utils/tobii_stream_display.py new file mode 100644 index 0000000..b849357 --- /dev/null +++ b/src/argaze/utils/tobii_stream_display.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python + +import argparse +import os, time + +from argaze import DataStructures, GazeFeatures +from argaze.TobiiGlassesPro2 import * + +import cv2 as cv +import numpy + +def main(): + """ + Capture video camera and gaze data streams and synchronise them. + """ + + # Manage arguments + parser = argparse.ArgumentParser(description=main.__doc__.split('-')[0]) + parser.add_argument('-t', '--tobii_ip', metavar='TOBII_IP', type=str, default='192.168.1.12', help='tobii glasses ip') + + args = parser.parse_args() + + # Create tobii controller + tobii_controller = TobiiController.TobiiController(args.tobii_ip, 'myProject', 'mySelf') + + # Calibrate tobii glasses + tobii_controller.calibrate() + + # Enable tobii data stream + tobii_data_stream = tobii_controller.enable_data_stream() + + # Enable tobii video stream + tobii_video_stream = tobii_controller.enable_video_stream() + + # Start streaming + tobii_controller.start_streaming() + + # Live video stream capture loop + try: + + past_gaze_positions = DataStructures.TimeStampedBuffer() + + while tobii_video_stream.is_alive(): + + video_ts, video_frame = tobii_video_stream.read() + + try: + + # Read data stream + data_stream = tobii_data_stream.read() + + # Store received gaze positions + past_gaze_positions.append(data_stream['GazePosition']) + + # Get last gaze position before video timestamp and remove all former gaze positions + earliest_ts, earliest_gaze_position = past_gaze_positions.pop_first_until(video_ts) + + # Draw gaze position + video_gaze_pixel = (int(earliest_gaze_position.value[0] * video_frame.width), int(earliest_gaze_position.value[1] * video_frame.height)) + cv.circle(video_frame.matrix, video_gaze_pixel, 4, (0, 255, 255), -1) + + # Wait for gaze position + except (AttributeError, ValueError): + continue + + # Close window using 'Esc' key + if cv.waitKey(1) == 27: + break + + cv.imshow(f'Live Tobii Camera', video_frame.matrix) + + # Exit on 'ctrl+C' interruption + except KeyboardInterrupt: + pass + + # Stop frame display + cv.destroyAllWindows() + + # Stop streaming + tobii_controller.stop_streaming() + +if __name__ == '__main__': + + main()
\ No newline at end of file |