From 3d4ddbb423c8a988e9b719783e8e3cb59ddb8e9c Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Wed, 25 Apr 2018 14:55:41 +0200 Subject: [PATCH 1/3] Refactor calibrate.preprocess_*_data_* Fixes #1125 --- .../calibration_routines/calibrate.py | 51 +++++++------------ pupil_src/shared_modules/camera_models.py | 16 ++---- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/pupil_src/shared_modules/calibration_routines/calibrate.py b/pupil_src/shared_modules/calibration_routines/calibrate.py index 45695929f7..f88a7ca8bd 100644 --- a/pupil_src/shared_modules/calibration_routines/calibrate.py +++ b/pupil_src/shared_modules/calibration_routines/calibrate.py @@ -313,53 +313,36 @@ def find_nearest_idx(array,value): def preprocess_2d_data_monocular(matched_data): - cal_data = [] - for pair in matched_data: - ref,pupil = pair['ref'],pair['pupil'] - cal_data.append( (pupil["norm_pos"][0], pupil["norm_pos"][1],ref['norm_pos'][0],ref['norm_pos'][1]) ) + cal_data = [(*pair['pupil']["norm_pos"], + *pair['ref']["norm_pos"]) + for pair in matched_data] return cal_data + def preprocess_2d_data_binocular(matched_data): - cal_data = [] - for triplet in matched_data: - ref,p0,p1 = triplet['ref'],triplet['pupil'],triplet['pupil1'] - data_pt = p0["norm_pos"][0], p0["norm_pos"][1],p1["norm_pos"][0], p1["norm_pos"][1],ref['norm_pos'][0],ref['norm_pos'][1] - cal_data.append( data_pt ) + cal_data = [(*triplet['pupil']["norm_pos"], + *triplet['pupil1']["norm_pos"], + *triplet['ref']["norm_pos"]) + for triplet in matched_data] return cal_data -def preprocess_3d_data(matched_data, g_pool): - ref_processed = [] - pupil0_processed = [] - pupil1_processed = [] - is_binocular = len(matched_data[0] ) == 3 - for data_point in matched_data: - try: - # taking the pupil normal as line of sight vector - pupil0 = data_point['pupil'] - gaze_vector0 = np.array(pupil0['circle_3d']['normal']) - pupil0_processed.append( gaze_vector0 ) +def preprocess_3d_data(matched_data, g_pool): + pupil0_processed = [dp['pupil']['circle_3d']['normal'] for dp in matched_data if 'circle_3d' in dp['pupil']] - if is_binocular: # we have binocular data - pupil1 = data_point['pupil1'] - gaze_vector1 = np.array(pupil1['circle_3d']['normal']) - pupil1_processed.append( gaze_vector1 ) + pupil1_processed = [dp['pupil1']['circle_3d']['normal'] for dp in matched_data if 'pupil1' in dp and 'circle_3d' in dp['pupil1']] - # projected point uv to normal ray vector of camera - ref = data_point['ref']['screen_pos'] - ref_vector = np.array(ref).reshape(-1,1,2) - ref_vector = g_pool.capture.intrinsics.unprojectPoints(ref_vector, normalize=True) - # assuming a fixed (assumed) distance we get a 3d point in world camera 3d coords. - ref_processed.append(ref_vector) + ref = np.array([dp['ref']['screen_pos'] for dp in matched_data]) + ref_processed = g_pool.capture.intrinsics.unprojectPoints(ref, normalize=True) - except KeyError as e: - # this pupil data point did not have 3d detected data. - pass + print(len(ref_processed), len(pupil0_processed), len(pupil1_processed)) - return ref_processed,pupil0_processed,pupil1_processed + return ref_processed, pupil0_processed, pupil1_processed def find_rigid_transform(A, B): + # we expect the shape to be of length 2 + assert len(A.shape) == len(B.shape) == 2 assert A.shape[0] == B.shape[0] centroid_A = np.mean(A, axis=0) diff --git a/pupil_src/shared_modules/camera_models.py b/pupil_src/shared_modules/camera_models.py index 2a16cdbd6d..5f4a63a6a4 100644 --- a/pupil_src/shared_modules/camera_models.py +++ b/pupil_src/shared_modules/camera_models.py @@ -165,10 +165,9 @@ def unprojectPoints(self, pts_2d, use_distortion=True, normalize=False): Undistorts points according to the camera model. cv2.fisheye.undistortPoints does *NOT* perform the same unprojection step the original cv2.unprojectPoints does. Thus we implement this function ourselves. - :param pts_2d: Distorted points. Can be a list of points or a single point. - :return: Array of unprojected 3d points + :param pts_2d, shape: Nx2 + :return: Array of unprojected 3d points, shape: Nx3 """ - input_shape = pts_2d.shape pts_2d = pts_2d.reshape((-1, 2)) eps = np.finfo(np.float32).eps @@ -303,15 +302,10 @@ def undistort(self, img): def unprojectPoints(self, pts_2d, use_distortion=True, normalize=False): """ Undistorts points according to the camera model. - :param pts_2d: 2d points. Can be a list of points or a single point. - :return: Array of unprojected 3d points + :param pts_2d, shape: Nx2 + :return: Array of unprojected 3d points, shape: Nx3 """ pts_2d = np.array(pts_2d, dtype=np.float32) - input_shape = pts_2d.shape - - # If given a single point expand to a 1-point list - if len(pts_2d.shape) == 1: - pts_2d = pts_2d.reshape((1, 2)) # Delete any posibly wrong 3rd dimension if pts_2d.ndim == 3: @@ -336,7 +330,7 @@ def unprojectPoints(self, pts_2d, use_distortion=True, normalize=False): pts_3d.shape = -1, 3 if normalize: - pts_3d /= np.linalg.norm(pts_3d, axis=1) + pts_3d /= np.linalg.norm(pts_3d, axis=1)[:, np.newaxis] return pts_3d From bae5f60de5b94757ce9152d3290b24e1e8821b42 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Wed, 25 Apr 2018 15:17:23 +0200 Subject: [PATCH 2/3] Background Helper: Print exception trace logger does not work in background processes in our case --- pupil_src/shared_modules/background_helper.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pupil_src/shared_modules/background_helper.py b/pupil_src/shared_modules/background_helper.py index 51f42328f8..d40bd2a1d1 100644 --- a/pupil_src/shared_modules/background_helper.py +++ b/pupil_src/shared_modules/background_helper.py @@ -47,8 +47,9 @@ def _wrapper(self, pipe, _should_terminate_flag, generator, *args, **kwargs): pipe.send(datum) except Exception as e: pipe.send(e) - import traceback - logger.warning(traceback.format_exc()) + if not isinstance(e, EarlyCancellationError): + import traceback + print(traceback.format_exc()) else: pipe.send(StopIteration()) finally: From d99f63f37248a1602bc673f5715278e65586b3e2 Mon Sep 17 00:00:00 2001 From: Pablo Prietz Date: Wed, 25 Apr 2018 15:32:12 +0200 Subject: [PATCH 3/3] Ammend 3d4ddb: Remove print line --- pupil_src/shared_modules/calibration_routines/calibrate.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pupil_src/shared_modules/calibration_routines/calibrate.py b/pupil_src/shared_modules/calibration_routines/calibrate.py index f88a7ca8bd..f86aa895cc 100644 --- a/pupil_src/shared_modules/calibration_routines/calibrate.py +++ b/pupil_src/shared_modules/calibration_routines/calibrate.py @@ -335,8 +335,6 @@ def preprocess_3d_data(matched_data, g_pool): ref = np.array([dp['ref']['screen_pos'] for dp in matched_data]) ref_processed = g_pool.capture.intrinsics.unprojectPoints(ref, normalize=True) - print(len(ref_processed), len(pupil0_processed), len(pupil1_processed)) - return ref_processed, pupil0_processed, pupil1_processed