Created
February 1, 2025 09:57
-
-
Save pushkarsingh019/ffb34205793d43708d5a0b9ab16b5e5d to your computer and use it in GitHub Desktop.
General Purpose Functions to Record and Store Data from Tobii Eyetracker.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def gaze_data_callback(gaze_data): | |
| global trigger | |
| global gaze_data_buffer | |
| global winsize | |
| global r | |
| if len(trigger)==0: | |
| ev = '' | |
| else: | |
| ev = trigger | |
| trigger=[] | |
| # Extract the data we are interested in | |
| t = gaze_data.system_time_stamp / 1000.0 | |
| lx = gaze_data.left_eye.gaze_point.position_on_display_area[0] * winsize[0] | |
| ly = winsize[1] - gaze_data.left_eye.gaze_point.position_on_display_area[1] * winsize[1] | |
| lp = gaze_data.left_eye.pupil.diameter | |
| lv = gaze_data.left_eye.gaze_point.validity | |
| rx = gaze_data.right_eye.gaze_point.position_on_display_area[0] * winsize[0] | |
| ry = winsize[1] - gaze_data.right_eye.gaze_point.position_on_display_area[1] * winsize[1] | |
| rp = gaze_data.right_eye.pupil.diameter | |
| rv = gaze_data.right_eye.gaze_point.validity | |
| # Add gaze data to the buffer | |
| gaze_data_buffer.append((t,lx,ly,lp,lv,rx,ry,rp,rv,ev)) | |
| def write_buffer_to_file(buffer, output_path): | |
| # Make a copy of the buffer and clear it | |
| buffer_copy = buffer[:] | |
| buffer.clear() | |
| # Define column names | |
| columns = ['time', 'L_X', 'L_Y', 'L_P', 'L_V', | |
| 'R_X', 'R_Y', 'R_P', 'R_V', 'Event'] | |
| # Convert buffer to DataFrame | |
| out = pd.DataFrame(buffer_copy, columns=columns) | |
| # Check if the file exists | |
| file_exists = not os.path.isfile(output_path) | |
| # Write the DataFrame to an HDF5 file | |
| out.to_csv(output_path, mode='a', index =False, header = file_exists) | |
| # ROI Function | |
| def get_area_of_interest(screen_resolution, area_of_interest, position_of_interest): | |
| """ | |
| Visualizes the area of interest within the given screen resolution. | |
| Parameters: | |
| screen_resolution (list): A list containing the width and height of the screen resolution [width, height]. | |
| area_of_interest (list): A list containing the width and height of the area of interest [width, height]. | |
| position_of_interest (list): A list containing the x and y coordinates of the position of the area of interest relative to the center [x, y]. | |
| Returns: | |
| list: A list containing the start and end coordinates of the area of interest rectangle [x_start, x_end, y_start, y_end]. | |
| """ | |
| screen_width, screen_height = screen_resolution | |
| aoi_width, aoi_height = area_of_interest | |
| aoi_x, aoi_y = position_of_interest | |
| # Calculate the x and y coordinates of the area of interest | |
| x_start = screen_width // 2 + aoi_x - aoi_width // 2 | |
| x_end = x_start + aoi_width | |
| y_start = screen_height // 2 + aoi_y - aoi_height // 2 | |
| y_end = y_start + aoi_height | |
| # Ensure the area of interest stays within the screen resolution | |
| x_start = max(0, min(x_start, screen_width - aoi_width)) | |
| x_end = max(0, min(x_end, screen_width)) | |
| y_start = max(0, min(y_start, screen_height - aoi_height)) | |
| y_end = max(0, min(y_end, screen_height)) | |
| return [x_start, x_end, y_start, y_end] | |
| # finding and initialising the eyetracker | |
| found_eyetrackers = tr.find_all_eyetrackers() | |
| # We will just use the first one | |
| Eyetracker = found_eyetrackers[0] | |
| # Create an empty list we will append our data to | |
| gaze_data_buffer = [] | |
| # to store the data. | |
| write_buffer_to_file(gaze_data_buffer, "file_path.csv") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment