Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

unrotated gaze lasers working nicely

Jon1238bf01ba8d
diff --git a/jon_scratch/pupil_calibration_pipeline/data_classes/freemocap_session_data_class.py b/jon_scratch/pupil_calibration_pipeline/data_classes/freemocap_session_data_class.py
index ecfd0c62..703763e6 100644
--- a/jon_scratch/pupil_calibration_pipeline/data_classes/freemocap_session_data_class.py
+++ b/jon_scratch/pupil_calibration_pipeline/data_classes/freemocap_session_data_class.py
@@ -3,7 +3,7 @@ from typing import List
import numpy as np
-from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilData
+from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilLabsDataClass
from jon_scratch.pupil_calibration_pipeline.data_classes.rotation_data_class import RotationDataClass
@@ -12,10 +12,12 @@ class FreemocapSessionDataClass:
session_id: str = None
timestamps: np.ndarray = None
mediapipe_skel_fr_mar_dim: np.ndarray = None
- right_eye_data: PupilData = None
- left_eye_data: PupilData = None
+ right_eye_pupil_labs_data: PupilLabsDataClass = None
+ left_eye_pupil_labs_data: PupilLabsDataClass = None
head_rotation_data: RotationDataClass = None
- right_eye_socket_rotation_matricies: List = None
- left_eye_socket_rotation_matricies: List = None
+ right_eye_socket_rotation_data: RotationDataClass = None
+ left_eye_socket_rotation_data: RotationDataClass = None
+ right_gaze_vector_endpoint_fr_xyz: np.ndarray = None
+ left_gaze_vector_endpoint_fr_xyz: np.ndarray = None
diff --git a/jon_scratch/pupil_calibration_pipeline/data_classes/pupil_dataclass_and_handler.py b/jon_scratch/pupil_calibration_pipeline/data_classes/pupil_dataclass_and_handler.py
index d2aec820..43c0c35a 100644
--- a/jon_scratch/pupil_calibration_pipeline/data_classes/pupil_dataclass_and_handler.py
+++ b/jon_scratch/pupil_calibration_pipeline/data_classes/pupil_dataclass_and_handler.py
@@ -7,7 +7,7 @@ import pandas as pd
@dataclass
-class PupilData:
+class PupilLabsDataClass:
def __init__(self,
timestamps=None,
theta=None,
@@ -52,14 +52,14 @@ class PupilDataHandler:
pupil_center_normal_y = np.array(pupil_dataframe['circle_3d_normal_y'])
pupil_center_normal_z = np.array(pupil_dataframe['circle_3d_normal_z'])
- self.pupil_data = PupilData(timestamps=np.array(pupil_dataframe['pupil_timestamp']),
- theta=np.array(pupil_dataframe['theta']),
- phi=np.array(pupil_dataframe['phi']),
- pupil_center_normal_x=pupil_center_normal_x,
- pupil_center_normal_y=pupil_center_normal_y,
- pupil_center_normal_z=pupil_center_normal_z,
- eye_d=pupil_dataframe['eye_id'],
- method=pupil_dataframe['method'])
+ self.pupil_data = PupilLabsDataClass(timestamps=np.array(pupil_dataframe['pupil_timestamp']),
+ theta=np.array(pupil_dataframe['theta']),
+ phi=np.array(pupil_dataframe['phi']),
+ pupil_center_normal_x=pupil_center_normal_x,
+ pupil_center_normal_y=pupil_center_normal_y,
+ pupil_center_normal_z=pupil_center_normal_z,
+ eye_d=pupil_dataframe['eye_id'],
+ method=pupil_dataframe['method'])
def convert_to_unix_timestamps(self, pupil_recording_info_json: dict):
self.pupil_data.timestamps = self.pupil_data.timestamps - pupil_recording_info_json['start_time_synced_s'] + \
@@ -80,7 +80,7 @@ class PupilDataHandler:
this_eye_logical_indicies = np.logical_and(self.pupil_data.eye_d == eye_d,
self.pupil_data.method != '2d c++')
- return PupilData(
+ return PupilLabsDataClass(
timestamps=self.pupil_data.timestamps[this_eye_logical_indicies],
theta=self.pupil_data.theta[this_eye_logical_indicies],
phi=self.pupil_data.phi[this_eye_logical_indicies],
diff --git a/jon_scratch/pupil_calibration_pipeline/data_classes/rotation_data_class.py b/jon_scratch/pupil_calibration_pipeline/data_classes/rotation_data_class.py
index 16030066..421f3019 100644
--- a/jon_scratch/pupil_calibration_pipeline/data_classes/rotation_data_class.py
+++ b/jon_scratch/pupil_calibration_pipeline/data_classes/rotation_data_class.py
@@ -7,4 +7,7 @@ import numpy as np
@dataclass
class RotationDataClass:
rotation_matricies: List[np.ndarray]
- local_origin_fr_xyz: np.ndarray = np.array([0, 0, 0])
\ No newline at end of file
+ local_origin_fr_xyz: np.ndarray = np.array([0, 0, 0])
+ x_hat_norm_fr_xyz: np.ndarray = np.array([1, 0, 0])
+ y_hat_norm_fr_xyz: np.ndarray = np.array([0, 1, 0])
+ z_hat_norm_fr_xyz: np.ndarray = np.array([0, 0, 1])
\ No newline at end of file
diff --git a/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_calibration_pipeline_orchestrator.py b/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_calibration_pipeline_orchestrator.py
index 3ec93ebb..bb32dce0 100644
--- a/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_calibration_pipeline_orchestrator.py
+++ b/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_calibration_pipeline_orchestrator.py
@@ -1,10 +1,11 @@
+import copy
import logging
from pathlib import Path
from typing import Union
import numpy as np
from rich import inspect
from jon_scratch.pupil_calibration_pipeline.data_classes.freemocap_session_data_class import FreemocapSessionDataClass
-from jon_scratch.pupil_calibration_pipeline.head_rotation_calculator import RotationMatrixCalculator
+from jon_scratch.pupil_calibration_pipeline.rotation_matrix_calculator import RotationMatrixCalculator
from jon_scratch.pupil_calibration_pipeline.pupil_freemocap_synchronizer import PupilFreemocapSynchronizer
from jon_scratch.pupil_calibration_pipeline.qt_gl_laser_skeleton_visualizer import QtGlLaserSkeletonVisualizer
from jon_scratch.pupil_calibration_pipeline.session_data_loader import SessionDataLoader
@@ -17,7 +18,6 @@ logger.setLevel(logging.INFO)
class PupilFreemocapCalibrationPipelineOrchestrator:
session_data_loader: SessionDataLoader = None
raw_session_data = FreemocapSessionDataClass()
- synchronized_session_data: FreemocapSessionDataClass = None
vor_frame_start: int = None,
vor_frame_end: int = None
@@ -53,15 +53,15 @@ class PupilFreemocapCalibrationPipelineOrchestrator:
# load pupil data
####
pupil_data_handler = self.session_data_loader.load_pupil_data()
- self.raw_session_data.right_eye_data = pupil_data_handler.get_eye_data('right')
- self.raw_session_data.left_eye_data = pupil_data_handler.get_eye_data('left')
+ self.raw_session_data.right_eye_pupil_labs_data = pupil_data_handler.get_eye_data('right')
+ self.raw_session_data.left_eye_pupil_labs_data = pupil_data_handler.get_eye_data('left')
####
- # Synchronize pupil data with freemocap data - results in self.synchronized_session_data (each stream has exactly the same number of frames)
+ # Synchronize pupil data with freemocap data - results in synchronized_session_data (each stream has exactly the same number of frames)
####
- self.synchronized_session_data = PupilFreemocapSynchronizer(self.raw_session_data).synchronize(debug=False,
- vor_frame_start=self.vor_frame_start,
- vor_frame_end=self.vor_frame_end)
+ synchronized_session_data = PupilFreemocapSynchronizer(self.raw_session_data).synchronize(debug=False,
+ vor_frame_start=self.vor_frame_start,
+ vor_frame_end=self.vor_frame_end)
logger.info(
'synchronization complete - I should add a test to make sure everything has the same number of frames')
@@ -70,49 +70,54 @@ class PupilFreemocapCalibrationPipelineOrchestrator:
####
rotation_matrix_calculator = RotationMatrixCalculator(self.raw_session_data.mediapipe_skel_fr_mar_dim)
- self.synchronized_session_data.head_rotation_data = rotation_matrix_calculator.calculate_head_rotation_matricies(
+ synchronized_session_data.head_rotation_data = rotation_matrix_calculator.calculate_head_rotation_matricies(
debug=False)
- self.synchronized_session_data.right_eye_socket_rotation_data= rotation_matrix_calculator.calculate_eye_rotation_matricies(
+ synchronized_session_data.right_eye_socket_rotation_data = rotation_matrix_calculator.calculate_eye_rotation_matricies(
+ eye='right',
debug=False,
- eye='right')
+ )
- self.synchronized_session_data.left_eye_socket_rotation_data = rotation_matrix_calculator.calculate_eye_rotation_matricies(
+ synchronized_session_data.left_eye_socket_rotation_data = rotation_matrix_calculator.calculate_eye_rotation_matricies(
+ 'left',
debug=False,
- eye='left')
+ )
logger.info(
- f'len(self.synchronized_session_data.head_rotation_data.head_rotation_matricies): {len(self.synchronized_session_data.head_rotation_data.rotation_matricies)}')
+ f'len(synchronized_session_data.head_rotation_data.head_rotation_matricies): {len(synchronized_session_data.head_rotation_data.rotation_matricies)}')
####
# Perform Vestibular-Ocular-Reflex based calibration (see methods from (Matthis et al, 2018 and 2022) for deetos)
####
- vor_calibrator = VorCalibrator(self.synchronized_session_data, vor_start_frame=self.vor_frame_start,
+ vor_calibrator = VorCalibrator(synchronized_session_data.mediapipe_skel_fr_mar_dim.copy(),
+ vor_start_frame=self.vor_frame_start,
vor_end_frame=self.vor_frame_end)
right_index_fingertip_idx = 41 # pretty sure this is right?
- fixation_point_fr_xyz = self.synchronized_session_data.mediapipe_skel_fr_mar_dim[
- self.vor_frame_start:self.vor_frame_end, right_index_fingertip_idx, :3]
- vor_calibrator.calibrate(fixation_point_fr_xyz)
+ fixation_point_fr_xyz = synchronized_session_data.mediapipe_skel_fr_mar_dim[
+ self.vor_frame_start:self.vor_frame_end,
+ right_index_fingertip_idx,
+ :]
+ # right eye
+ synchronized_session_data.right_gaze_vector_endpoint_fr_xyz = vor_calibrator.calibrate(
+ synchronized_session_data.right_eye_pupil_labs_data,
+ synchronized_session_data.right_eye_socket_rotation_data,
+ fixation_point_fr_xyz)
+ # left eye
+ synchronized_session_data.left_gaze_vector_endpoint_fr_xyz = vor_calibrator.calibrate(
+ synchronized_session_data.left_eye_pupil_labs_data,
+ synchronized_session_data.left_eye_socket_rotation_data,
+ fixation_point_fr_xyz)
####
# Play laser skeleton animation (as both a cool thing and a debug tool)
####
- qt_gl_laser_skeleton = QtGlLaserSkeletonVisualizer(session_data=self.synchronized_session_data,
+ qt_gl_laser_skeleton = QtGlLaserSkeletonVisualizer(session_data=synchronized_session_data,
move_data_to_origin=True,
start_frame=self.vor_frame_start,
end_frame=self.vor_frame_end)
qt_gl_laser_skeleton.start_animation()
- ############################################
- ############################################
- ### helper methods
- ############################################
- def save_head_rotation_matricies(self):
- save_path = self.session_path / 'DataArrays' / 'mediaPipeSkel_3d_head_rotation_matricies_fr_row_col.npy'
- logger.info(f'saving head rotation matricies to: {save_path}')
- np.save(str(save_path), self.raw_session_data.head_rotation_matricies)
-
if __name__ == '__main__':
session_id = 'sesh_2022-02-15_11_54_28_pupil_maybe'
@@ -122,7 +127,8 @@ if __name__ == '__main__':
vor_frame_start_in = 1200
vor_frame_end_in = 1500
- orchestrator = PupilFreemocapCalibrationPipelineOrchestrator(this_session_path, vor_frame_start=vor_frame_start_in,
- vor_frame_end=vor_frame_end_in)
- orchestrator.run()
- print('done')
+ pupil_freemocap_calibration_pipeline_orchestrator = PupilFreemocapCalibrationPipelineOrchestrator(this_session_path,
+ vor_frame_start=vor_frame_start_in,
+ vor_frame_end=vor_frame_end_in)
+ pupil_freemocap_calibration_pipeline_orchestrator.run()
+ print('done :D ')
diff --git a/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_synchronizer.py b/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_synchronizer.py
index b4bef7a6..3f3ff5cf 100644
--- a/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_synchronizer.py
+++ b/jon_scratch/pupil_calibration_pipeline/pupil_freemocap_synchronizer.py
@@ -5,7 +5,7 @@ from matplotlib import pyplot as plt
import matplotlib
from jon_scratch.pupil_calibration_pipeline.data_classes.freemocap_session_data_class import FreemocapSessionDataClass
-from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilData
+from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilLabsDataClass
matplotlib.use('qt5agg')
logger = logging.getLogger(__name__)
@@ -29,8 +29,8 @@ class PupilFreemocapSynchronizer:
"""
# find start and end frames shared by all datastreams
freemocap_timestamps = self.raw_session_data.timestamps
- right_eye_timestamps = self.raw_session_data.right_eye_data.timestamps
- left_eye_timestamps = self.raw_session_data.left_eye_data.timestamps
+ right_eye_timestamps = self.raw_session_data.right_eye_pupil_labs_data.timestamps
+ left_eye_timestamps = self.raw_session_data.left_eye_pupil_labs_data.timestamps
start_time_unix = np.max((freemocap_timestamps[0], right_eye_timestamps[0], left_eye_timestamps[0]))
end_time_unix = np.min((freemocap_timestamps[-1], right_eye_timestamps[-1], left_eye_timestamps[-1]))
@@ -81,9 +81,9 @@ class PupilFreemocapSynchronizer:
self.resample_eye_data()
self.normalize_eye_data()
- self.synchronized_timestamps = self.raw_session_data.timestamps[freemocap_start_frame:freemocap_end_frame+1]
+ self.synchronized_timestamps = self.raw_session_data.timestamps[freemocap_start_frame:freemocap_end_frame + 1]
- self.synchronized_timestamps = self.synchronized_timestamps - self.synchronized_timestamps[0]
+ self.synchronized_timestamps = self.synchronized_timestamps - self.synchronized_timestamps[0]
assert (self.synchronized_timestamps.shape[0] == self.right_eye_theta.shape[0])
assert (self.synchronized_timestamps.shape[0] == self.left_eye_theta.shape[0])
@@ -91,64 +91,64 @@ class PupilFreemocapSynchronizer:
if debug:
self.show_debug_plots(vor_frame_start, vor_frame_end)
- synchronized_right_eye_data = PupilData(timestamps=self.synchronized_timestamps,
- theta=self.right_eye_theta,
- phi=self.right_eye_phi,
- pupil_center_normal_x=self.right_eye_pupil_center_normal_x,
- pupil_center_normal_y=self.right_eye_pupil_center_normal_y,
- pupil_center_normal_z=self.right_eye_pupil_center_normal_z,
- eye_d=0)
- synchronized_left_eye_data = PupilData(timestamps=self.synchronized_timestamps,
- theta=self.left_eye_theta,
- phi=self.left_eye_phi,
- pupil_center_normal_x=self.left_eye_pupil_center_normal_x,
- pupil_center_normal_y=self.left_eye_pupil_center_normal_y,
- pupil_center_normal_z=self.left_eye_pupil_center_normal_z,
- eye_d=1)
+ synchronized_right_eye_data = PupilLabsDataClass(timestamps=self.synchronized_timestamps,
+ theta=self.right_eye_theta,
+ phi=self.right_eye_phi,
+ pupil_center_normal_x=self.right_eye_pupil_center_normal_x,
+ pupil_center_normal_y=self.right_eye_pupil_center_normal_y,
+ pupil_center_normal_z=self.right_eye_pupil_center_normal_z,
+ eye_d=0)
+ synchronized_left_eye_data = PupilLabsDataClass(timestamps=self.synchronized_timestamps,
+ theta=self.left_eye_theta,
+ phi=self.left_eye_phi,
+ pupil_center_normal_x=self.left_eye_pupil_center_normal_x,
+ pupil_center_normal_y=self.left_eye_pupil_center_normal_y,
+ pupil_center_normal_z=self.left_eye_pupil_center_normal_z,
+ eye_d=1)
synchronized_session_data = FreemocapSessionDataClass(timestamps=self.synchronized_timestamps,
- mediapipe_skel_fr_mar_dim=self.raw_session_data.mediapipe_skel_fr_mar_dim[
- freemocap_start_frame:freemocap_end_frame,
- :, :],
- right_eye_data=synchronized_right_eye_data,
- left_eye_data=synchronized_left_eye_data)
+ mediapipe_skel_fr_mar_dim=self.raw_session_data.mediapipe_skel_fr_mar_dim[
+ freemocap_start_frame:freemocap_end_frame,
+ :, :],
+ right_eye_pupil_labs_data=synchronized_right_eye_data,
+ left_eye_pupil_labs_data=synchronized_left_eye_data)
return synchronized_session_data
-
def clip_eye_data(self):
- self.right_eye_timestamps_clipped = self.raw_session_data.right_eye_data.timestamps[self.right_eye_start_frame:self.right_eye_end_frame]
+ self.right_eye_timestamps_clipped = self.raw_session_data.right_eye_pupil_labs_data.timestamps[
+ self.right_eye_start_frame:self.right_eye_end_frame]
- self.right_eye_pupil_center_normal_x_clipped = self.raw_session_data.right_eye_data.pupil_center_normal_x[
+ self.right_eye_pupil_center_normal_x_clipped = self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_x[
self.right_eye_start_frame:self.right_eye_end_frame]
- self.right_eye_pupil_center_normal_y_clipped = self.raw_session_data.right_eye_data.pupil_center_normal_y[
+ self.right_eye_pupil_center_normal_y_clipped = self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_y[
self.right_eye_start_frame:self.right_eye_end_frame]
- self.right_eye_pupil_center_normal_z_clipped = self.raw_session_data.right_eye_data.pupil_center_normal_z[
+ self.right_eye_pupil_center_normal_z_clipped = self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_z[
self.right_eye_start_frame:self.right_eye_end_frame]
- self.right_eye_theta_clipped = self.raw_session_data.right_eye_data.theta[
+ self.right_eye_theta_clipped = self.raw_session_data.right_eye_pupil_labs_data.theta[
self.right_eye_start_frame:self.right_eye_end_frame]
- self.right_eye_phi_clipped = self.raw_session_data.right_eye_data.phi[
+ self.right_eye_phi_clipped = self.raw_session_data.right_eye_pupil_labs_data.phi[
self.right_eye_start_frame:self.right_eye_end_frame]
- self.left_eye_timestamps_clipped = self.raw_session_data.left_eye_data.timestamps[
- self.left_eye_start_frame:self.left_eye_end_frame]
+ self.left_eye_timestamps_clipped = self.raw_session_data.left_eye_pupil_labs_data.timestamps[
+ self.left_eye_start_frame:self.left_eye_end_frame]
- self.left_eye_pupil_center_normal_x_clipped = self.raw_session_data.left_eye_data.pupil_center_normal_x[
+ self.left_eye_pupil_center_normal_x_clipped = self.raw_session_data.left_eye_pupil_labs_data.pupil_center_normal_x[
self.left_eye_start_frame:self.left_eye_end_frame]
- self.left_eye_pupil_center_normal_y_clipped = self.raw_session_data.left_eye_data.pupil_center_normal_y[
+ self.left_eye_pupil_center_normal_y_clipped = self.raw_session_data.left_eye_pupil_labs_data.pupil_center_normal_y[
self.left_eye_start_frame:self.left_eye_end_frame]
- self.left_eye_pupil_center_normal_z_clipped = self.raw_session_data.left_eye_data.pupil_center_normal_z[
+ self.left_eye_pupil_center_normal_z_clipped = self.raw_session_data.left_eye_pupil_labs_data.pupil_center_normal_z[
self.left_eye_start_frame:self.left_eye_end_frame]
- self.left_eye_theta_clipped = self.raw_session_data.left_eye_data.theta[
+ self.left_eye_theta_clipped = self.raw_session_data.left_eye_pupil_labs_data.theta[
self.left_eye_start_frame:self.left_eye_end_frame]
- self.left_eye_phi_clipped = self.raw_session_data.left_eye_data.phi[
+ self.left_eye_phi_clipped = self.raw_session_data.left_eye_pupil_labs_data.phi[
self.left_eye_start_frame:self.left_eye_end_frame]
def resample_eye_data(self):
@@ -179,13 +179,19 @@ class PupilFreemocapSynchronizer:
self.left_eye_phi_clipped)
def normalize_eye_data(self):
- self.right_eye_pupil_center_normal_x = self.right_eye_pupil_center_normal_x / np.linalg.norm(self.right_eye_pupil_center_normal_x)
- self.right_eye_pupil_center_normal_y = self.right_eye_pupil_center_normal_y / np.linalg.norm(self.right_eye_pupil_center_normal_y)
- self.right_eye_pupil_center_normal_z = self.right_eye_pupil_center_normal_z / np.linalg.norm(self.right_eye_pupil_center_normal_z)
-
- self.left_eye_pupil_center_normal_x = self.left_eye_pupil_center_normal_x / np.linalg.norm(self.left_eye_pupil_center_normal_x)
- self.left_eye_pupil_center_normal_y = self.left_eye_pupil_center_normal_y / np.linalg.norm(self.left_eye_pupil_center_normal_y)
- self.left_eye_pupil_center_normal_z = self.left_eye_pupil_center_normal_z / np.linalg.norm(self.left_eye_pupil_center_normal_z)
+ self.right_eye_pupil_center_normal_x = self.right_eye_pupil_center_normal_x / np.linalg.norm(
+ self.right_eye_pupil_center_normal_x)
+ self.right_eye_pupil_center_normal_y = self.right_eye_pupil_center_normal_y / np.linalg.norm(
+ self.right_eye_pupil_center_normal_y)
+ self.right_eye_pupil_center_normal_z = self.right_eye_pupil_center_normal_z / np.linalg.norm(
+ self.right_eye_pupil_center_normal_z)
+
+ self.left_eye_pupil_center_normal_x = self.left_eye_pupil_center_normal_x / np.linalg.norm(
+ self.left_eye_pupil_center_normal_x)
+ self.left_eye_pupil_center_normal_y = self.left_eye_pupil_center_normal_y / np.linalg.norm(
+ self.left_eye_pupil_center_normal_y)
+ self.left_eye_pupil_center_normal_z = self.left_eye_pupil_center_normal_z / np.linalg.norm(
+ self.left_eye_pupil_center_normal_z)
def show_debug_plots(self, vor_frame_start, vor_frame_end):
@@ -195,32 +201,41 @@ class PupilFreemocapSynchronizer:
fig = plt.figure(num=653412, figsize=(10, 20))
fig.suptitle('Raw data')
ax1 = fig.add_subplot(411)
- ax1.plot(self.raw_session_data.right_eye_data.timestamps, self.raw_session_data.right_eye_data.pupil_center_normal_x, '.-', label='right_eye_pupil_center_normal_x')
- ax1.plot(self.raw_session_data.right_eye_data.timestamps, self.raw_session_data.right_eye_data.pupil_center_normal_y, '.-', label='right_eye_pupil_center_normal_y')
- ax1.plot(self.raw_session_data.right_eye_data.timestamps, self.raw_session_data.right_eye_data.pupil_center_normal_z, '.-', label='right_eye_pupil_center_normal_z')
+ ax1.plot(self.raw_session_data.right_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_x, '.-',
+ label='right_eye_pupil_center_normal_x')
+ ax1.plot(self.raw_session_data.right_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_y, '.-',
+ label='right_eye_pupil_center_normal_y')
+ ax1.plot(self.raw_session_data.right_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.right_eye_pupil_labs_data.pupil_center_normal_z, '.-',
+ label='right_eye_pupil_center_normal_z')
ax1.legend(loc='upper left')
ax2 = fig.add_subplot(412)
- ax2.plot(self.raw_session_data.right_eye_data.timestamps,
- self.raw_session_data.right_eye_data.theta, '.-',
+ ax2.plot(self.raw_session_data.right_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.right_eye_pupil_labs_data.theta, '.-',
label='right_eye_theta')
- ax2.plot(self.raw_session_data.right_eye_data.timestamps,
- self.raw_session_data.right_eye_data.phi, '.-',
+ ax2.plot(self.raw_session_data.right_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.right_eye_pupil_labs_data.phi, '.-',
label='right_eye_phi')
ax2.legend(loc='upper left')
ax3 = fig.add_subplot(413)
- ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_x, '.-', label='left_eye_pupil_center_normal_x')
- ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_y, '.-', label='left_eye_pupil_center_normal_y')
- ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_z, '.-', label='left_eye_pupil_center_normal_z')
+ ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_x, '.-',
+ label='left_eye_pupil_center_normal_x')
+ ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_y, '.-',
+ label='left_eye_pupil_center_normal_y')
+ ax3.plot(self.raw_session_data.timestamps, self.left_eye_pupil_center_normal_z, '.-',
+ label='left_eye_pupil_center_normal_z')
ax3.legend(loc='upper left')
ax4 = fig.add_subplot(414)
- ax4.plot(self.raw_session_data.left_eye_data.timestamps,
- self.raw_session_data.left_eye_data.theta, '.-',
+ ax4.plot(self.raw_session_data.left_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.left_eye_pupil_labs_data.theta, '.-',
label='left_eye_theta')
- ax4.plot(self.raw_session_data.left_eye_data.timestamps,
- self.raw_session_data.left_eye_data.phi, '.-',
+ ax4.plot(self.raw_session_data.left_eye_pupil_labs_data.timestamps,
+ self.raw_session_data.left_eye_pupil_labs_data.phi, '.-',
label='left_eye_phi')
ax4.legend(loc='upper left')
@@ -228,13 +243,18 @@ class PupilFreemocapSynchronizer:
# Plot synchronized data
###########################
-
fig = plt.figure(num=65341, figsize=(10, 20))
fig.suptitle('Synchronized data')
ax1 = fig.add_subplot(411)
- ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.right_eye_pupil_center_normal_x[vor_frame_start:vor_frame_end], '.-', label='right_eye_pupil_center_normal_x')
- ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.right_eye_pupil_center_normal_y[vor_frame_start:vor_frame_end], '.-', label='right_eye_pupil_center_normal_y')
- ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.right_eye_pupil_center_normal_z[vor_frame_start:vor_frame_end], '.-', label='right_eye_pupil_center_normal_z')
+ ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.right_eye_pupil_center_normal_x[vor_frame_start:vor_frame_end], '.-',
+ label='right_eye_pupil_center_normal_x')
+ ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.right_eye_pupil_center_normal_y[vor_frame_start:vor_frame_end], '.-',
+ label='right_eye_pupil_center_normal_y')
+ ax1.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.right_eye_pupil_center_normal_z[vor_frame_start:vor_frame_end], '.-',
+ label='right_eye_pupil_center_normal_z')
ax1.legend(loc='upper left')
ax2 = fig.add_subplot(412)
@@ -247,9 +267,15 @@ class PupilFreemocapSynchronizer:
ax2.legend(loc='upper left')
ax3 = fig.add_subplot(413)
- ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.left_eye_pupil_center_normal_x[vor_frame_start:vor_frame_end], '.-', label='left_eye_pupil_center_normal_x')
- ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.left_eye_pupil_center_normal_y[vor_frame_start:vor_frame_end], '.-', label='left_eye_pupil_center_normal_y')
- ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end], self.left_eye_pupil_center_normal_z[vor_frame_start:vor_frame_end], '.-', label='left_eye_pupil_center_normal_z')
+ ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.left_eye_pupil_center_normal_x[vor_frame_start:vor_frame_end], '.-',
+ label='left_eye_pupil_center_normal_x')
+ ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.left_eye_pupil_center_normal_y[vor_frame_start:vor_frame_end], '.-',
+ label='left_eye_pupil_center_normal_y')
+ ax3.plot(self.synchronized_timestamps[vor_frame_start:vor_frame_end],
+ self.left_eye_pupil_center_normal_z[vor_frame_start:vor_frame_end], '.-',
+ label='left_eye_pupil_center_normal_z')
ax3.legend(loc='upper left')
ax4 = fig.add_subplot(414)
diff --git a/jon_scratch/pupil_calibration_pipeline/qt_gl_laser_skeleton_visualizer.py b/jon_scratch/pupil_calibration_pipeline/qt_gl_laser_skeleton_visualizer.py
index 4c304e93..7b35f0be 100644
--- a/jon_scratch/pupil_calibration_pipeline/qt_gl_laser_skeleton_visualizer.py
+++ b/jon_scratch/pupil_calibration_pipeline/qt_gl_laser_skeleton_visualizer.py
@@ -56,6 +56,10 @@ class QtGlLaserSkeletonVisualizer():
self.initialize_skel_dottos()
self.initialize_skel_lines()
self.initialize_head_axes()
+ self.initialize_eye_socket_axes()
+ if self.session_data.right_gaze_vector_endpoint_fr_xyz is not None:
+ self.initialize_gaze_lasers()
+ self.initialize_gaze_laser_tails(tail_length=30)
def create_app_window(self):
self.app = pg.mkQApp("Laser Skeleton")
@@ -108,8 +112,92 @@ class QtGlLaserSkeletonVisualizer():
self.skeleton_connections_list.append(this_skel_line)
self.gl_view_widget.addItem(this_skel_line)
+ def initialize_gaze_lasers(self):
+ # right eye
+ this_frame_right_gaze_origin = self.session_data.right_eye_socket_rotation_data.local_origin_fr_xyz[0, :]
+ this_frame_right_gaze_endpoint = self.session_data.right_gaze_vector_endpoint_fr_xyz[0, :]
+ this_frame_right_gaze_laser = np.array([this_frame_right_gaze_origin, this_frame_right_gaze_endpoint])
+
+ self.right_gaze_vector_line_item = gl.GLLinePlotItem(pos=this_frame_right_gaze_laser,
+ color=(1, 0, 1, 1),
+ width=3)
+
+ self.gl_view_widget.addItem(self.right_gaze_vector_line_item)
+
+ # left eye
+ this_frame_left_gaze_origin = self.session_data.left_eye_socket_rotation_data.local_origin_fr_xyz[0, :]
+ this_frame_left_gaze_endpoint = self.session_data.left_gaze_vector_endpoint_fr_xyz[0, :]
+ this_frame_left_gaze_laser = np.array([this_frame_left_gaze_origin, this_frame_left_gaze_endpoint])
+
+ self.left_gaze_vector_line_item = gl.GLLinePlotItem(pos=this_frame_left_gaze_laser,
+ color=(0, 1, 1, 1),
+ width=3)
+
+ self.gl_view_widget.addItem(self.left_gaze_vector_line_item)
+
+ def initialize_gaze_laser_tails(self, tail_length):
+ self.tail_length = tail_length
+ dummy_tail = np.empty((self.tail_length, 3))
+ dummy_tail[:] = np.nan
+ self.right_gaze_laser_tail_line_item = gl.GLLinePlotItem(pos=dummy_tail, color=(1, 0, 1, .5), width=2)
+ self.left_gaze_laser_tail_line_item = gl.GLLinePlotItem(pos=dummy_tail, color=(0, 1, 1, .5), width=2)
+
+ self.gl_view_widget.addItem(self.right_gaze_laser_tail_line_item)
+ self.gl_view_widget.addItem(self.left_gaze_laser_tail_line_item)
+
+ def initialize_eye_socket_axes(self):
+ self.eye_axes_scale = 1e2 / 2
+
+ # right eye
+ self.right_eye_socket_axes_x_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='x',
+ scale=self.eye_axes_scale,
+ color=(1, 0, 0, 1),
+ width=3)
+ self.right_eye_socket_axes_y_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='y',
+ scale=self.eye_axes_scale,
+ color=(0, 1, 0, 1),
+ width=3)
+ self.right_eye_socket_axes_z_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='z',
+ scale=self.eye_axes_scale,
+ color=(0, 0, 1, 1),
+ width=3)
+
+ self.gl_view_widget.addItem(self.right_eye_socket_axes_x_vector_line_item)
+ self.gl_view_widget.addItem(self.right_eye_socket_axes_y_vector_line_item)
+ self.gl_view_widget.addItem(self.right_eye_socket_axes_z_vector_line_item)
+
+ # left eye
+ self.left_eye_socket_axes_x_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='x',
+ scale=self.eye_axes_scale,
+ color=(1, 0, 0, 1),
+ width=3)
+ self.left_eye_socket_axes_y_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='y',
+ scale=self.eye_axes_scale,
+ color=(0, 1, 0, 1),
+ width=3)
+ self.left_eye_socket_axes_z_vector_line_item = self.create_axis_vector_line_item(0,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='z',
+ scale=self.eye_axes_scale,
+ color=(0, 0, 1, 1),
+ width=3)
+
+ self.gl_view_widget.addItem(self.left_eye_socket_axes_x_vector_line_item)
+ self.gl_view_widget.addItem(self.left_eye_socket_axes_y_vector_line_item)
+ self.gl_view_widget.addItem(self.left_eye_socket_axes_z_vector_line_item)
+
def initialize_head_axes(self):
- self.head_axes_scale = 1e3
+ self.head_axes_scale = 1e2
first_frame_head_center_xyz = self.session_data.head_rotation_data.local_origin_fr_xyz[0, :]
@@ -177,8 +265,9 @@ class QtGlLaserSkeletonVisualizer():
raise ValueError('something weird about the `dimension` argument')
this_rot_mat = rotation_data.rotation_matricies[frame_number]
- this_axis_vector_endpoint_xyz = (this_rot_mat[dimension, :] * scale) + rotation_data.local_origin_fr_xyz[frame_number,:]
- this_axis_vector_origin_xyz = rotation_data.local_origin_fr_xyz[frame_number,:]
+ this_axis_vector_endpoint_xyz = rotation_data.local_origin_fr_xyz[frame_number, :] + (
+ this_rot_mat[dimension, :] * scale)
+ this_axis_vector_origin_xyz = rotation_data.local_origin_fr_xyz[frame_number, :]
this_axis_vector_line_xyz = np.array([this_axis_vector_origin_xyz, this_axis_vector_endpoint_xyz])
@@ -194,9 +283,10 @@ class QtGlLaserSkeletonVisualizer():
)
self.update_skeleton_lines()
self.update_head_axis_lines()
- # this_gaze_laser = np.vstack(
- # (self.right_eye_xyz[self.current_frame_number, :], self.right_gaze_xyz[self.current_frame_number, :]))
- # self.right_gaze_line_item.setData(pos=this_gaze_laser)
+ self.update_eye_axis_lines()
+ if self.session_data.right_gaze_vector_endpoint_fr_xyz is not None:
+ self.update_gaze_lasers()
+ self.update_gaze_laser_tails()
def update_skeleton_lines(self):
for this_skeleton_line_number, this_connection in enumerate(self.mediapipe_body_connections):
@@ -207,25 +297,98 @@ class QtGlLaserSkeletonVisualizer():
def update_head_axis_lines(self):
# X
this_frame_head_axis_x_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
- self.session_data.head_rotation_data,
- dimension='x',
- scale=self.head_axes_scale)
+ self.session_data.head_rotation_data,
+ dimension='x',
+ scale=self.head_axes_scale)
self.head_axes_x_vector_line_item.setData(pos=this_frame_head_axis_x_vector_xyz)
# Y
this_frame_head_axis_y_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
- self.session_data.head_rotation_data,
- dimension='y',
- scale=self.head_axes_scale)
+ self.session_data.head_rotation_data,
+ dimension='y',
+ scale=self.head_axes_scale)
self.head_axes_y_vector_line_item.setData(pos=this_frame_head_axis_y_vector_xyz)
# Z
this_frame_head_axis_z_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
- self.session_data.head_rotation_data,
- dimension='z',
- scale=self.head_axes_scale)
+ self.session_data.head_rotation_data,
+ dimension='z',
+ scale=self.head_axes_scale)
self.head_axes_z_vector_line_item.setData(pos=this_frame_head_axis_z_vector_xyz)
+ def update_eye_axis_lines(self):
+ # right right eye
+ # X
+ this_frame_right_eye_socket_axis_x_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='x',
+ scale=self.eye_axes_scale)
+ self.right_eye_socket_axes_x_vector_line_item.setData(pos=this_frame_right_eye_socket_axis_x_vector_xyz)
+
+ # Y
+ this_frame_right_eye_socket_axis_y_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='y',
+ scale=self.eye_axes_scale)
+ self.right_eye_socket_axes_y_vector_line_item.setData(pos=this_frame_right_eye_socket_axis_y_vector_xyz)
+
+ # Z
+ this_frame_right_eye_socket_axis_z_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.right_eye_socket_rotation_data,
+ dimension='z',
+ scale=self.eye_axes_scale)
+ self.right_eye_socket_axes_z_vector_line_item.setData(pos=this_frame_right_eye_socket_axis_z_vector_xyz)
+
+ # left eye
+ # X
+ this_frame_left_eye_socket_axis_x_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='x',
+ scale=self.eye_axes_scale)
+ self.left_eye_socket_axes_x_vector_line_item.setData(pos=this_frame_left_eye_socket_axis_x_vector_xyz)
+
+ # Y
+ this_frame_left_eye_socket_axis_y_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='y',
+ scale=self.eye_axes_scale)
+ self.left_eye_socket_axes_y_vector_line_item.setData(pos=this_frame_left_eye_socket_axis_y_vector_xyz)
+
+ # Z
+ this_frame_left_eye_socket_axis_z_vector_xyz = self.unit_vector_from_rotation_matrix(self.current_frame_number,
+ self.session_data.left_eye_socket_rotation_data,
+ dimension='z',
+ scale=self.eye_axes_scale)
+ self.left_eye_socket_axes_z_vector_line_item.setData(pos=this_frame_left_eye_socket_axis_z_vector_xyz)
+
+ def update_gaze_lasers(self):
+ # right eye
+ this_right_gaze_laser_line = np.array(
+ [self.session_data.right_eye_socket_rotation_data.local_origin_fr_xyz[self.current_frame_number, :],
+ self.session_data.right_gaze_vector_endpoint_fr_xyz[self.current_frame_number, :]])
+ self.right_gaze_vector_line_item.setData(pos=this_right_gaze_laser_line)
+
+ # left eye
+ this_left_gaze_laser_line = np.array(
+ [self.session_data.left_eye_socket_rotation_data.local_origin_fr_xyz[self.current_frame_number, :],
+ self.session_data.left_gaze_vector_endpoint_fr_xyz[self.current_frame_number, :]])
+
+ self.left_gaze_vector_line_item.setData(pos=this_left_gaze_laser_line)
+
+ def update_gaze_laser_tails(self):
+ if self.current_frame_number < self.tail_length:
+ return
+
+ # right eye
+ this_right_gaze_laser_tail = self.session_data.right_gaze_vector_endpoint_fr_xyz[
+ self.current_frame_number - self.tail_length: self.current_frame_number, :]
+ self.right_gaze_laser_tail_line_item.setData(pos=this_right_gaze_laser_tail)
+
+ # left eye
+ this_left_gaze_laser_tail = self.session_data.left_gaze_vector_endpoint_fr_xyz[
+ self.current_frame_number - self.tail_length: self.current_frame_number, :]
+ self.left_gaze_laser_tail_line_item.setData(pos=this_left_gaze_laser_tail)
+
def update_frame_number(self):
self.current_frame_number += 1
if self.current_frame_number >= self.end_frame_number:
@@ -250,13 +413,18 @@ class QtGlLaserSkeletonVisualizer():
self.mediapipe_fr_mar_xyz[:, :, 1] -= mean_position_xyz[1]
self.mediapipe_fr_mar_xyz[:, :, 2] -= mean_position_xyz[2]
- self.session_data.head_rotation_data.local_origin_fr_xyz[:,0] -= mean_position_xyz[0]
- self.session_data.head_rotation_data.local_origin_fr_xyz[:,1] -= mean_position_xyz[1]
- self.session_data.head_rotation_data.local_origin_fr_xyz[:,2] -= mean_position_xyz[2]
+ self.session_data.head_rotation_data.local_origin_fr_xyz[:, 0] -= mean_position_xyz[0]
+ self.session_data.head_rotation_data.local_origin_fr_xyz[:, 1] -= mean_position_xyz[1]
+ self.session_data.head_rotation_data.local_origin_fr_xyz[:, 2] -= mean_position_xyz[2]
- def create_head_axes_vector_line_item(self, param, head_rotation_matricies, dimension, scale):
- pass
+ if self.session_data.right_gaze_vector_endpoint_fr_xyz is not None:
+ self.session_data.right_gaze_vector_endpoint_fr_xyz[:, 0] -= mean_position_xyz[0]
+ self.session_data.right_gaze_vector_endpoint_fr_xyz[:, 1] -= mean_position_xyz[1]
+ self.session_data.right_gaze_vector_endpoint_fr_xyz[:, 2] -= mean_position_xyz[2]
+ self.session_data.left_gaze_vector_endpoint_fr_xyz[:, 0] -= mean_position_xyz[0]
+ self.session_data.left_gaze_vector_endpoint_fr_xyz[:, 1] -= mean_position_xyz[1]
+ self.session_data.left_gaze_vector_endpoint_fr_xyz[:, 2] -= mean_position_xyz[2]
if __name__ == '__main__':
diff --git a/jon_scratch/pupil_calibration_pipeline/head_rotation_calculator.py b/jon_scratch/pupil_calibration_pipeline/rotation_matrix_calculator.py
similarity index 60%
rename from jon_scratch/pupil_calibration_pipeline/head_rotation_calculator.py
rename to jon_scratch/pupil_calibration_pipeline/rotation_matrix_calculator.py
index dba76ecb..162bd161 100644
--- a/jon_scratch/pupil_calibration_pipeline/head_rotation_calculator.py
+++ b/jon_scratch/pupil_calibration_pipeline/rotation_matrix_calculator.py
@@ -20,7 +20,11 @@ def pin_point_new_origin(point_xyz, new_origin_xyz):
def normalize_length(point_xyz):
- return point_xyz / np.linalg.norm(point_xyz, axis=1)[:, None]
+ return point_xyz / get_norm_length(point_xyz, axis=1)[:, None]
+
+
+def get_norm_length(point_xyz, axis=1):
+ return np.linalg.norm(point_xyz, axis=axis)
class RotationMatrixCalculator:
@@ -29,7 +33,9 @@ class RotationMatrixCalculator:
def __init__(self, mediapipe_skeleton_fr_mar_xyz):
self.mediapipe_skeleton_fr_mar_xyz = mediapipe_skeleton_fr_mar_xyz
- def calculate_head_rotation_matricies(self, debug=False):
+ def calculate_head_rotation_matricies(self,
+ normalize_length_by_x: bool = False,
+ debug: bool = False):
"""
# Calculate head rotation matricies
@@ -45,56 +51,92 @@ class RotationMatrixCalculator:
"""
nose_id = 0
- right_ear_id = 8
left_ear_id = 7
+ right_ear_id = 8
- nose_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, nose_id, :]
- right_ear_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, right_ear_id, :]
- left_ear_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, left_ear_id, :]
+ nose_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, nose_id, :].copy()
+ right_ear_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, right_ear_id, :].copy()
+ left_ear_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, left_ear_id, :].copy()
head_center_xyz = (right_ear_xyz + left_ear_xyz) / 2
- head_rotation_data= self.define_orthonormal_basis_vectors(center_point_fr_xyz=head_center_xyz,
- x_direction_fr_xyz=nose_xyz,
- y_direction_fr_xyz=left_ear_xyz)
+ head_rotation_data = self.define_basis_vectors(center_point_fr_xyz=head_center_xyz,
+ x_direction_fr_xyz=nose_xyz,
+ y_direction_fr_xyz=left_ear_xyz,
+ normalize_length_by_x=normalize_length_by_x)
if debug:
- self.show_head_rotation_debug_plot(head_rotation_data.rotation_matricies)
+ self.show_head_rotation_debug_plot(head_rotation_data)
return head_rotation_data
- def define_orthonormal_basis_vectors(self,
- center_point_fr_xyz: np.ndarray = None,
- x_direction_fr_xyz: np.ndarray = None,
- y_direction_fr_xyz: np.ndarray = None,
- z_direction_fr_xyz: np.ndarray = None) -> RotationDataClass:
+ def calculate_eye_rotation_matricies(self,
+ eye: str,
+ normalize_length_by_x: bool = False,
+ debug: bool = False,
+ ) -> List[np.ndarray]:
+ nose_index = 0
+ if eye == 'left':
+ inner_eye_index = 1
+ eye_mid_index = 2
+ outer_eye_index = 3
+ x_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, inner_eye_index,
+ :] # this will make Z+ point forward
+ elif eye == 'right':
+ inner_eye_index = 4
+ eye_mid_index = 5
+ outer_eye_index = 6
+ x_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, outer_eye_index,
+ :] # this will make Z+ point forward
+ else:
+ raise ValueError('`eye` must be "left" or "right"')
+
+ y_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, nose_index, :]
+
+ eye_mid_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, eye_mid_index, :]
+
+ eye_socket_rotation_data = self.define_basis_vectors(center_point_fr_xyz=eye_mid_fr_xyz,
+ x_direction_fr_xyz=x_direction_fr_xyz,
+ y_direction_fr_xyz=y_direction_fr_xyz,
+ normalize_length_by_x = normalize_length_by_x)
+
+ return eye_socket_rotation_data
+
+ def define_basis_vectors(self,
+ center_point_fr_xyz: np.ndarray = None,
+ x_direction_fr_xyz: np.ndarray = None,
+ y_direction_fr_xyz: np.ndarray = None,
+ z_direction_fr_xyz: np.ndarray = None,
+ normalize_length_by_x: bool = True) -> RotationDataClass:
"""
create orthonormal basis vectors based on a center point and at least TWO of the following = a point defining X direction, a point defining Y direction, and a point defining Z direction
"""
- x_unknown = False
- y_unknown = False
- z_unknown = False
+ x_known = True
+ y_known = True
+ z_known = True
if x_direction_fr_xyz is None:
- x_unknown = True
+ x_known = False
x_direction_fr_xyz = np.empty(center_point_fr_xyz.shape)
x_direction_fr_xyz[:] = np.nan
+
if y_direction_fr_xyz is None:
- y_unknown = True
+ y_known = False
y_direction_fr_xyz = np.empty(center_point_fr_xyz.shape)
y_direction_fr_xyz[:] = np.nan
+
if z_direction_fr_xyz is None:
- z_unknown = True
+ z_known = False
z_direction_fr_xyz = np.empty(center_point_fr_xyz.shape)
z_direction_fr_xyz[:] = np.nan
- if sum([x_unknown, y_unknown, z_unknown]) > 1:
+ if sum([x_known, y_known, z_known]) < 2:
raise ValueError(
"Must specify at least two of the following: x_direction_fr_xyz, y_direction_fr_xyz, z_direction_fr_xyz")
if center_point_fr_xyz is not None: # if center point is specified, then use it to zero out the x,y,z directions vectors
- zero_x_direction_xyz = pin_point_new_origin(center_point_fr_xyz, x_direction_fr_xyz)
- zero_y_direction_xyz = pin_point_new_origin(center_point_fr_xyz, y_direction_fr_xyz)
- zero_z_direction_xyz = pin_point_new_origin(center_point_fr_xyz, z_direction_fr_xyz)
+ zero_x_direction_xyz = pin_point_new_origin(x_direction_fr_xyz, center_point_fr_xyz)
+ zero_y_direction_xyz = pin_point_new_origin(y_direction_fr_xyz, center_point_fr_xyz)
+ zero_z_direction_xyz = pin_point_new_origin(z_direction_fr_xyz, center_point_fr_xyz)
else:
zero_x_direction_xyz = x_direction_fr_xyz
zero_y_direction_xyz = y_direction_fr_xyz
@@ -104,57 +146,60 @@ class RotationMatrixCalculator:
y_hat_xyz = None
z_hat_xyz = None
- if not x_unknown:
+ if x_known:
+ x_norm_length = np.nanmean(get_norm_length(zero_x_direction_xyz))
x_hat_xyz = normalize_length(zero_x_direction_xyz)
- if not y_unknown: # we know x and y directions and need to find Z by X cross Y
+ if y_known: # we know x and y directions and need to find Z by X cross Y
z_hat_xyz = normalize_length(np.cross(x_hat_xyz, zero_y_direction_xyz))
y_hat_xyz = normalize_length(np.cross(z_hat_xyz, x_hat_xyz))
else: # we know x and z directions and need to find Y by Z cross X
- y_hat_xyz = normalize_length(np.cross(zero_y_direction_xyz, x_hat_xyz))
+ y_hat_xyz = normalize_length(np.cross(zero_z_direction_xyz, x_hat_xyz))
z_hat_xyz = normalize_length(np.cross(x_hat_xyz, y_hat_xyz))
- elif not y_unknown: # we know y and z directions and need to find X by Y cross Z
+ elif y_known: # we know y and z directions and need to find X by Y cross Z
y_hat_xyz = normalize_length(zero_y_direction_xyz)
x_hat_xyz = normalize_length(np.cross(y_hat_xyz, zero_z_direction_xyz))
z_hat_xyz = normalize_length(np.cross(x_hat_xyz, y_hat_xyz))
- # create 3x3 cosine rotation matricies by stacking X_hat, Y_hat, and Z_hat
- rotation_matricies = [np.vstack((this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz))
- for this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz in
- zip(x_hat_xyz, y_hat_xyz, z_hat_xyz)]
+ if normalize_length_by_x:
+ x_hat_xyz = x_hat_xyz * x_norm_length
+ y_hat_xyz = y_hat_xyz * x_norm_length
+ z_hat_xyz = z_hat_xyz * x_norm_length
- rotation_matricies = [this_rotation_martix.T for this_rotation_martix in rotation_matricies]
+ # #umm, this shouldn't be necessary.... but it makes it work, so....
+ # x_hat_xyz = -x_hat_xyz
+ # y_hat_xyz = -y_hat_xyz
+ # z_hat_xyz = z_hat_xyz
- return RotationDataClass(rotation_matricies=rotation_matricies,
- local_origin_fr_xyz=center_point_fr_xyz)
+ # create 3x3 cosine rotation matricies by stacking X_hat, Y_hat, and Z_hat
+ rotation_matricies = []
+ frame_number = -1
+ for this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz in zip(x_hat_xyz, y_hat_xyz, z_hat_xyz):
+ frame_number += 1
+ if frame_number > 1000:
+ f = 9
+ this_rotation_matrix = np.squeeze(np.dstack((this_x_hat_xyz, this_y_hat_xyz, this_z_hat_xyz)))
- def calculate_eye_rotation_matricies(self, debug, eye: str) -> List[np.ndarray]:
- nose_index = 0
- if eye == 'left':
- inner_eye_index = 1
- eye_mid_index = 2
- outer_eye_index = 3
- x_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, inner_eye_index,
- :] # this will make Z+ point forward
- if eye == 'right':
- inner_eye_index = 4
- eye_mid_index = 5
- outer_eye_index = 6
- x_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, outer_eye_index,
- :] # this will make Z+ point forward
+ assert this_rotation_matrix.shape == (3, 3)
- y_direction_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, nose_index, :]
+ if not np.isnan(this_x_hat_xyz).any():
+ assert (this_rotation_matrix[:, 0] == this_x_hat_xyz).all()
- eye_mid_fr_xyz = self.mediapipe_skeleton_fr_mar_xyz[:, eye_mid_index, :]
+ rotation_matricies.append(this_rotation_matrix)
- eye_socket_rotation_data = self.define_orthonormal_basis_vectors(center_point_fr_xyz=eye_mid_fr_xyz,
- x_direction_fr_xyz=x_direction_fr_xyz,
- y_direction_fr_xyz=y_direction_fr_xyz)
+ rotation_matricies = [np.transpose(this_rotation_martix) for this_rotation_martix in rotation_matricies]
- return eye_socket_rotation_data
+ return RotationDataClass(rotation_matricies=rotation_matricies,
+ local_origin_fr_xyz=center_point_fr_xyz,
+ x_hat_norm_fr_xyz=x_hat_xyz,
+ y_hat_norm_fr_xyz=y_hat_xyz,
+ z_hat_norm_fr_xyz=z_hat_xyz,
+ )
- def show_head_rotation_debug_plot(self, rotation_matricies):
+ def show_head_rotation_debug_plot(self, rotation_data: RotationDataClass):
+ rotation_matricies = rotation_data.rotation_matricies
+ local_origin_fr_xyz = rotation_data.local_origin_fr_xyz
x_hat_xyz = np.asarray([this_rot_mat[0, :] for this_rot_mat in rotation_matricies])
y_hat_xyz = np.asarray([this_rot_mat[1, :] for this_rot_mat in rotation_matricies])
@@ -166,15 +211,20 @@ class RotationMatrixCalculator:
for frame_number in range(len(rotation_matricies)):
# see https://peps.python.org/pep-0465/ for explanation of the `@` operator (which is a "matrix multiplication" operator)
- test_point_xyz_rot[frame_number, :] = rotation_matricies[frame_number].T @ test_point_xyz_og
+ test_point_xyz_rot[frame_number, :] = np.transpose(rotation_matricies[frame_number]) @ test_point_xyz_og
+ z_mediapipe_skeleton_fr_mar_xyz = np.empty(self.mediapipe_skeleton_fr_mar_xyz.shape)
+ for this_marker_number in range(self.mediapipe_skeleton_fr_mar_xyz.shape[1]):
+ z_mediapipe_skeleton_fr_mar_xyz[:, this_marker_number, :] = pin_point_new_origin(
+ self.mediapipe_skeleton_fr_mar_xyz[:, this_marker_number, :],
+ local_origin_fr_xyz)
plt.close('all')
fig = plt.figure(num=124)
fig.suptitle('Head Rotation Matrix - Debug Plot - Press ESC to Quit')
ax = fig.add_subplot(111, projection='3d')
- ax_range = 2
+ ax_range = np.nanmean(get_norm_length(x_hat_xyz)) * 2
for frame_number in range(1200, x_hat_xyz.shape[0]):
ax.clear()
@@ -214,6 +264,12 @@ class RotationMatrixCalculator:
[0, test_point_xyz_rot[frame_number, 1]],
[0, test_point_xyz_rot[frame_number, 2]], 'k-o', label='test point')
+ # plot a friendly skeleton
+ ax.scatter(z_mediapipe_skeleton_fr_mar_xyz[frame_number, :, 0],
+ z_mediapipe_skeleton_fr_mar_xyz[frame_number, :, 1],
+ z_mediapipe_skeleton_fr_mar_xyz[frame_number, :, 2],
+ 'ko')
+
ax.set_xlim([-ax_range, ax_range])
ax.set_ylim([-ax_range, ax_range])
ax.set_zlim([-ax_range, ax_range])
diff --git a/jon_scratch/pupil_calibration_pipeline/session_data_loader.py b/jon_scratch/pupil_calibration_pipeline/session_data_loader.py
index e6f44094..fb20ed00 100644
--- a/jon_scratch/pupil_calibration_pipeline/session_data_loader.py
+++ b/jon_scratch/pupil_calibration_pipeline/session_data_loader.py
@@ -5,7 +5,7 @@ from typing import Union
import numpy as np
import pandas as pd
-from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilData, PupilDataHandler
+from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilLabsDataClass, PupilDataHandler
logger = logging.getLogger(__name__)
diff --git a/jon_scratch/pupil_calibration_pipeline/vor_calibrator.py b/jon_scratch/pupil_calibration_pipeline/vor_calibrator.py
index d812d41b..105cf925 100644
--- a/jon_scratch/pupil_calibration_pipeline/vor_calibrator.py
+++ b/jon_scratch/pupil_calibration_pipeline/vor_calibrator.py
@@ -1,10 +1,13 @@
import logging
+from typing import Union, List
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
from jon_scratch.pupil_calibration_pipeline.data_classes.freemocap_session_data_class import FreemocapSessionDataClass
+from jon_scratch.pupil_calibration_pipeline.data_classes.pupil_dataclass_and_handler import PupilLabsDataClass
+from jon_scratch.pupil_calibration_pipeline.data_classes.rotation_data_class import RotationDataClass
matplotlib.use('qt5agg')
logger = logging.getLogger(__name__)
@@ -16,37 +19,86 @@ class VorCalibrator:
see methods sections of (Matthis, Yates, and Hayhoe, Current Biology, 2018) and (Matthis, Muller, Bonnen, Hayehoe, PLoS Computation Biology 2022) for details
"""
- def __init__(self, session_data: FreemocapSessionDataClass, vor_start_frame: int = None, vor_end_frame: int = None):
- self.session_data = session_data
+ def __init__(self, mediapipe_skel_fr_mar_xyz: np.ndarray,
+ vor_start_frame: int = None,
+ vor_end_frame: int = None):
+ self.mediapipe_skel_fr_mar_xyz = mediapipe_skel_fr_mar_xyz
if vor_start_frame is not None:
+ self.vor_start_frame = vor_start_frame
if vor_end_frame is None:
raise ValueError("Must specify vor_end_frame if vor_start_frame is specified")
- self.clip_out_vor_frames(vor_start_frame, vor_end_frame)
+ self.vor_end_frame = vor_end_frame
- def calibrate(self, fixation_point_fr_xyz: np.ndarray) -> FreemocapSessionDataClass:
+ def calibrate(self,
+ pupil_labs_eye_data: PupilLabsDataClass,
+ eye_socket_rotation_data: RotationDataClass,
+ fixation_point_fr_xyz: np.ndarray) -> np.ndarray:
if not fixation_point_fr_xyz.shape[1] == 3:
raise Exception("fixation_point_fr_xyz must be a numpy array with 3 columns")
- if not fixation_point_fr_xyz.shape[0] == self.mediapipe_skel_fr_mar_dim.shape[0]:
- raise Exception("fixation_point_xyz must have precisely the same number of rows as the skeleton/gaze data")
- self.calculate_optimal_rotational_offset()
- self.create_skeleton_gaze_lasers()
- return self.session_data
+ gaze_vector_start_point_fr_xyz = eye_socket_rotation_data.local_origin_fr_xyz
+ gaze_unit_vector_end_point_fr_xyz = eye_socket_rotation_data.z_hat_norm_fr_xyz
+ self.fixation_distance = self.get_fixation_distance(fixation_point_fr_xyz,
+ gaze_vector_start_point_fr_xyz[
+ self.vor_start_frame:self.vor_end_frame, :])
- def clip_out_vor_frames(self, vor_start_frame, vor_end_frame):
- self.mediapipe_skel_fr_mar_dim = self.session_data.mediapipe_skel_fr_mar_dim[vor_start_frame:vor_end_frame,:,:]
- self.head_rotation_matricies = self.session_data.head_rotation_data.rotation_matricies[vor_start_frame:vor_end_frame]
+ gaze_vector_endpoint_fr_xyz = self.create_unrotated_gaze_lasers_from_eye_rotation_matrixies(
+ gaze_vector_start_point_fr_xyz,
+ gaze_unit_vector_end_point_fr_xyz,
+ np.nanmedian(self.fixation_distance))
+
+ # self.clip_out_vor_frames(self.vor_start_frame, self.vor_end_frame)
+ # if not fixation_point_fr_xyz.shape[0] == self.mediapipe_skel_fr_mar_xyz.shape[0]:
+ # raise Exception(
+ # "fixation_point_xyz must have precisely the same number of rows as the skeleton/gaze data after clipping out the VOR data")
- self.right_eye_theta = self.session_data.right_eye_data.theta[vor_start_frame:vor_end_frame]
- self.right_eye_phi = self.session_data.right_eye_data.phi[vor_start_frame:vor_end_frame]
+ return gaze_vector_endpoint_fr_xyz
- self.left_eye_phi = self.session_data.left_eye_data.phi[vor_start_frame:vor_end_frame]
- self.left_eye_theta = self.session_data.left_eye_data.theta[vor_start_frame:vor_end_frame]
+ def clip_out_vor_frames(self, vor_start_frame, vor_end_frame):
+ self.mediapipe_skel_fr_mar_xyz = self.session_data.mediapipe_skel_fr_mar_xyz[vor_start_frame:vor_end_frame, :,
+ :]
+ # right eye
+ self.right_eyeball_center_fr_xyz = self.session_data.right_eye_socket_rotation_data.local_origin_fr_xyz[
+ vor_start_frame:vor_end_frame, :]
+ self.right_eye_rotation_matricies = self.session_data.right_eye_socket_rotation_data.rotation_matricies[
+ vor_start_frame:vor_end_frame]
+ self.right_eye_theta = self.session_data.right_eye_pupil_labs_data.theta[vor_start_frame:vor_end_frame]
+ self.right_eye_phi = self.session_data.right_eye_pupil_labs_data.phi[vor_start_frame:vor_end_frame]
+ # left eye
+ self.left_eyeball_center_fr_xyz = self.session_data.left_eye_socket_rotation_data.local_origin_fr_xyz[
+ vor_start_frame:vor_end_frame, :]
+ self.left_eye_rotation_matricies = self.session_data.left_eye_socket_rotation_data.rotation_matricies[
+ vor_start_frame:vor_end_frame]
+ self.left_eye_phi = self.session_data.left_eye_pupil_labs_data.phi[vor_start_frame:vor_end_frame]
+ self.left_eye_theta = self.session_data.left_eye_pupil_labs_data.theta[vor_start_frame:vor_end_frame]
def calculate_optimal_rotational_offset(self):
pass
- def create_skeleton_gaze_lasers(self):
- pass
+ def get_fixation_distance(self, local_origin_fr_xyz, fixation_point_fr_xyz):
+ return np.linalg.norm(local_origin_fr_xyz - fixation_point_fr_xyz, axis=1)
+
+ def create_unrotated_gaze_lasers_from_eye_rotation_matrixies(self,
+ gaze_vector_start_point_fr_xyz: np.ndarray,
+ gaze_unit_vector_end_point_fr_xyz: np.ndarray,
+ calbration_distance,
+ ) -> np.ndarray:
+ """
+ Creates an unrotated gaze laser for each frame in the skeleton/gaze data. Returns a vector the same length as the calibration_distance, shooting our of the skeletons eye_sockets (as if they eyes were paralyzed).
+ """
+ if not gaze_vector_start_point_fr_xyz.shape == gaze_unit_vector_end_point_fr_xyz.shape:
+ raise Exception("`gaze_vector_start_point_fr_xyz` and `gaze_unit_vector_end_point_fr_xyz` must have the same shape")
+
+ #scale gaze vector so it's the same norm/length as the calibration distance
+ gaze_unit_vector_end_point_fr_xyz = gaze_unit_vector_end_point_fr_xyz * calbration_distance
+
+ #move it from an zero-centered reference frame to the eye socket reference frame
+ gaze_vector_end_point_fr_xyz = gaze_unit_vector_end_point_fr_xyz + gaze_vector_start_point_fr_xyz
+
+ return gaze_vector_end_point_fr_xyz
+
+ def create_skeleton_gaze_lasers(self, gaze_laser_centered_on_zero_fr_xyz):
+
+ return gaze_laser_centered_on_zero_fr_xyz