def get_iris_pupil_center_from_eye_parts(data_to_fit): pupilPts, irisPts = getValidPoints(data_to_fit) if len(pupilPts) > 0: model_pupil = ransac(pupilPts, ElliFit, 15, 40, 5e-3, 15).loop() pupil_fit_error = my_ellipse(model_pupil.model).verify(pupilPts) r, c = np.where(data_to_fit == 2) pupil_loc = model_pupil.model[: 2] if pupil_fit_error < 0.05 else np.stack( [np.mean(c), np.mean(r)], axis=0) else: pupil_loc = None # Iris ellipse fit if len(irisPts) > 0: model_iris = ransac(irisPts, ElliFit, 15, 40, 5e-3, 15).loop() iris_fit_error = my_ellipse(model_iris.model).verify(irisPts) iris_loc = model_iris.model[0:2] else: iris_loc = None return pupil_loc, iris_loc
def evaluate_ellseg_on_image(frame, model): assert len(frame.shape) == 4, 'Frame must be [1,1,H,W]' with torch.no_grad(): x4, x3, x2, x1, x = model.enc(frame) latent = torch.mean(x.flatten(start_dim=2), -1) elOut = model.elReg(x, 0) seg_out = model.dec(x4, x3, x2, x1, x) seg_out, elOut, latent = seg_out.cpu(), elOut.squeeze().cpu( ), latent.squeeze().cpu() seg_map = get_predictions(seg_out).squeeze().numpy() ellipse_from_network = 1 if args.ellseg_ellipses == 1 else 0 ellipse_from_output = 1 if args.ellseg_ellipses == 0 else 0 no_ellipse = 1 if args.ellseg_ellipses == -1 else 0 if ellipse_from_network: # Get EllSeg proposed ellipse predictions # Ellipse Centers -> derived from segmentation output # Ellipse axes and orientation -> Derived from latent space _, norm_pupil_center = get_seg2ptLoss(seg_out[:, 2, ...], torch.zeros(2, ), temperature=4) _, norm_iris_center = get_seg2ptLoss(-seg_out[:, 0, ...], torch.zeros(2, ), temperature=4) norm_pupil_ellipse = torch.cat([norm_pupil_center, elOut[7:10]]) norm_iris_ellipse = torch.cat([norm_iris_center, elOut[2:5]]) # Transformation function H _, _, H, W = frame.shape H = np.array([[W / 2, 0, W / 2], [0, H / 2, H / 2], [0, 0, 1]]) pupil_ellipse = my_ellipse( norm_pupil_ellipse.numpy()).transform(H)[0][:-1] iris_ellipse = my_ellipse( norm_iris_ellipse.numpy()).transform(H)[0][:-1] if ellipse_from_output: # Get ElliFit derived ellipse fits from segmentation mask seg_map_temp = copy.deepcopy(seg_map) seg_map_temp[seg_map_temp == 2] += 1 # Pupil by PartSeg standard is 3 seg_map_temp[seg_map_temp == 1] += 1 # Iris by PartSeg standard is 2 pupilPts, irisPts = getValidPoints(seg_map_temp, isPartSeg=False) if np.sum(seg_map_temp == 3) > 50 and type(pupilPts) is not list: if args.skip_ransac: model_pupil = ElliFit(**{'data': pupilPts}) else: model_pupil = ransac(pupilPts, ElliFit, 15, 40, 5e-3, 15).loop() else: print('Not enough pupil points') model_pupil = type('model', (object, ), {}) model_pupil.model = np.array([-1, -1, -1, -1, -1]) if np.sum(seg_map_temp == 2) > 50 and type(irisPts) is not list: if args.skip_ransac: model_iris = ElliFit(**{'data': irisPts}) else: model_iris = ransac(irisPts, ElliFit, 15, 40, 5e-3, 15).loop() else: print('Not enough iris points') model_iris = type('model', (object, ), {}) model_iris.model = np.array([-1, -1, -1, -1, -1]) model_iris.Phi = np.array([-1, -1, -1, -1, -1]) # iris_fit_error = np.inf pupil_ellipse = np.array(model_pupil.model) iris_ellipse = np.array(model_iris.model) if no_ellipse: pupil_ellipse = np.array([-1, -1, -1, -1, -1]) iris_ellipse = np.array([-1, -1, -1, -1, -1]) return seg_map, latent.cpu().numpy(), pupil_ellipse, iris_ellipse
#%% Make sure images are 640x480 r = np.where(LabelMat)[0] c = int(0.5 * (np.max(r) + np.min(r))) top, bot = (0, c + 150 - (c - 150)) if c - 150 < 0 else (c - 150, c + 150) I = I[top:bot, :] LabelMat = LabelMat[top:bot, :] I = cv2.resize(I, (640, 480), interpolation=cv2.INTER_LANCZOS4) LabelMat = cv2.resize(LabelMat, (640, 480), interpolation=cv2.INTER_NEAREST) #%% pupilPts, irisPts = getValidPoints(LabelMat) if np.sum(LabelMat == 3) > 150 and type(pupilPts) is not list: model_pupil = ransac(pupilPts, ElliFit, 15, 40, 5e-3, 15).loop() pupil_fit_error = my_ellipse( model_pupil.model).verify(pupilPts) else: print('Not enough pupil points') model_pupil = type('model', (object, ), {}) model_pupil.model = np.array([-1, -1, -1, -1, -1]) pupil_fit_error = np.inf if np.sum(LabelMat == 2) > 200 and type(irisPts) is not list: model_iris = ransac(irisPts, ElliFit, 15, 40, 5e-3, 15).loop() iris_fit_error = my_ellipse(model_iris.model).verify(irisPts) else: print('Not enough iris points') model_iris = type('model', (object, ), {}) model_iris.model = np.array([-1, -1, -1, -1, -1])
print('Error in mask. Package: {}. Idx: {}'.format(fName, boo)) mask_noSkin = -np.ones_like(I) Data['Images'].append(I) Data['Masks'].append(mask) Data['Masks_noSkin'].append(mask_noSkin) Data['Info'].append(str_imName) keydict['resolution'].append(I.shape) keydict['archive'].append(ds_name) temp = 255 * (mask_noSkin == 3) edge = cv2.Canny(temp.astype(np.uint8), 10, 150) + cv2.Canny( (255 - temp).astype(np.uint8), 10, 150) r, c = np.where(edge) temp_pts = np.stack([c, r], axis=1) # Improve readability model_pupil = ransac(temp_pts, ElliFit, 15, 10, 0.05, np.round(temp_pts.shape[0] / 2)).loop() pupil_fit_error = my_ellipse(model_pupil.model).verify(temp_pts) pupil_loc = model_pupil.model[:2] temp = 255 * ((mask_noSkin == 2) | (mask_noSkin == 3)) edge = cv2.Canny(temp.astype(np.uint8), 10, 150) + cv2.Canny( (255 - temp).astype(np.uint8), 10, 150) r, c = np.where(edge) temp_pts = np.stack([c, r], axis=1) model_iris = ransac(temp_pts, ElliFit, 15, 10, 0.05, np.round(temp_pts.shape[0] / 2)).loop() iris_fit_error = my_ellipse(model_iris.model).verify(temp_pts) Data['Fits']['pupil'].append(model_pupil.model) Data['Fits']['iris'].append(model_iris.model)