def gaze_mapping_result_2d(img_path, calibration="dummy", calibration_path="", calibration_ground_truth=True): ##### prepare data ##### img = cv2.imread(img_path) frame = Frame(1, img, 1) u_r = Roi(frame.img.shape) detector2d = detector_2d.Detector_2D() pupil_datum = detector2d.detect(frame, user_roi=u_r, visualize=False) pupil_datum['id'] = 0 #print(pupil_datum) pool = SimpleNamespace() pool.active_gaze_mapping_plugin = None ##### prepare data ##### ##### map gaze ##### if calibration == "dummy": pool.active_gaze_mapping_plugin = gaze_mappers.Dummy_Gaze_Mapper(pool) elif calibration == "monocular": cal_pt_cloud = read_monocular_calibration_data_from_file( calibration_path, calibration_ground_truth) #cal_pt_cloud = read_monocular_calibration_data(calibration_path, calibration_ground_truth) _, _, params = calibrate.calibrate_2d_polynomial(cal_pt_cloud) #print(params) pool.active_gaze_mapping_plugin = gaze_mappers.Monocular_Gaze_Mapper( pool, params) gaze_data = pool.active_gaze_mapping_plugin.on_pupil_datum(pupil_datum) #print(gaze_data) ##### map gaze ##### return gaze_data[0]['norm_pos']
def pupil_detection_result_3d(img_path): img = cv2.imread(img_path) frame = Frame(1, img, 1) #print(frame) Pool = namedtuple('Pool', 'user_dir') pool = Pool('/') #print(pool) detector3d = detector_3d.Detector_3D() #print(detector3d) u_r = Roi(frame.img.shape) #print(u_r) print(detector3d) result = detector3d.detect(frame, user_roi=u_r, visualize=False) center_x = result['ellipse']['center'][0] center_y = result['ellipse']['center'][1] center = [center_x, center_y] axes_0 = result['ellipse']['axes'][0] axes_1 = result['ellipse']['axes'][1] axes = [axes_0, axes_1] degree = result['ellipse']['angle'] #fitted_ellipse = Ellipse(center, axes[0], axes[1], degree) #frame_img = frame.img #fig = plt.figure() #ax = fig.add_subplot(1,1,1) #ax.add_artist(fitted_ellipse) #plt.imshow(frame_img) #plt.scatter(center_x, center_y, color = 'red', s=10) return center, degree, axes
def pupil_detection_result_2d(img_path, visualization=False): img = cv2.imread(img_path) frame = Frame(1, img, 1) #print(frame) detector2d = detector_2d.Detector_2D() #print(detector2d) u_r = Roi(frame.img.shape) #print(u_r) result = detector2d.detect(frame, user_roi=u_r, visualize=False) center_x = result['ellipse']['center'][0] center_y = result['ellipse']['center'][1] center = [center_x, center_y] axes_0 = result['ellipse']['axes'][0] axes_1 = result['ellipse']['axes'][1] axes = [axes_0, axes_1] degree = result['ellipse']['angle'] #print(result['ellipse']) #print(result['norm_pos']) #print('') if visualization: fitted_ellipse = Ellipse(center, axes[0], axes[1], degree) frame_img = frame.img fig = plt.figure() ax = fig.add_subplot(111) ax.add_artist(fitted_ellipse) plt.imshow(frame_img) plt.scatter(center_x, center_y, color='red', s=10) return center, degree, axes
def add(self, payload) -> bool: self.__counter += 1 if self.__counter % self.frame_per_frames == 0: if payload["format"] not in ("gray", "bgr", "jpeg"): raise NotImplementedError( "The eye frame format '{}' is currently not supported!" .format(payload["format"])) shape = [self.frame_size[1], self.frame_size[0]] if payload["format"] != "gray": shape.append(3) data = np.frombuffer(payload["__raw_data__"][-1], dtype=np.uint8) if len(data) == np.prod(shape) or payload["format"] == "jpeg": raw_frame = (cv2.imdecode(data, cv2.IMREAD_COLOR) if payload["format"] == "jpeg" else data.reshape(shape)) # Pupil/OpenCV seems to tamper the underlying data. Better copy it. if payload["format"] == "gray": raw_frame = raw_frame.copy() grayscale_frame = (raw_frame if len(shape) == 2 else cv2.cvtColor( raw_frame, cv2.COLOR_BGR2GRAY)) color_frame = (raw_frame if len(shape) == 3 else cv2.cvtColor(raw_frame, cv2.COLOR_GRAY2BGR)) # Extract the pupil pupil_2d = self.__detector.detect( frame_=PreviewGenerator.ImageStream.FrameWrapper( grayscale_frame, color_frame), user_roi=Roi(grayscale_frame.shape), visualize=True, ) frame = PreviewFrame( self.eye_id, self.__counter, pupil_2d["confidence"], self.frame_format, ) frame.save(self.folder, color_frame) return True else: raise RuntimeWarning( "Image size {} does not match expected shape.".format( len(data))) return False
def gaze_mapping_with_gt_pupil_2d(img_path, calibration="dummy", calibration_path="", calibration_ground_truth=True): ##### prepare data ##### img = cv2.imread(img_path) frame = Frame(1, img, 1) u_r = Roi(frame.img.shape) detector2d = detector_2d.Detector_2D() pupil_datum = detector2d.detect(frame, user_roi=u_r, visualize=False) center, degree, axes = gt_pupil_info(img_path) pupil_datum['confidence'] = 1.0 pupil_datum['diameter'] = max(axes) pupil_datum['ellipse']['center'] = center pupil_datum['ellipse']['angle'] = degree pupil_datum['ellipse']['axes'] = axes pupil_datum['norm_pos'] = [center[0] / 320, (240 - center[1]) / 240] pupil_datum['id'] = 0 #print(pupil_datum) pool = SimpleNamespace() pool.active_gaze_mapping_plugin = None ##### prepare data ##### ##### map gaze ##### if calibration == "dummy": pool.active_gaze_mapping_plugin = gaze_mappers.Dummy_Gaze_Mapper(pool) elif calibration == "monocular": cal_pt_cloud = read_monocular_calibration_data_from_file( calibration_path, calibration_ground_truth) #cal_pt_cloud = read_monocular_calibration_data(calibration_path, calibration_ground_truth) _, _, params = calibrate.calibrate_2d_polynomial(cal_pt_cloud) #print(params) pool.active_gaze_mapping_plugin = gaze_mappers.Monocular_Gaze_Mapper( pool, params) gaze_data = pool.active_gaze_mapping_plugin.on_pupil_datum(pupil_datum) #print(gaze_data) ##### map gaze ##### return gaze_data[0]['norm_pos']
def read_monocular_calibration_data(calibration_path, ground_truth=True): #cal_data = [ # (*pair["pupil"]["norm_pos"], *pair["ref"]["norm_pos"]) for pair in matched_data #] pupil_positions = [] ref_positions = [] detector2d = detector_2d.Detector_2D() if ground_truth: pupil_cornea_path = calibration_path + "pupil_cornea\\" images = [f for f in glob.glob(pupil_cornea_path + "*.png")] else: eye_path = calibration_path + "eye\\" images = [f for f in glob.glob(eye_path + "*.png")] for i in images: img = cv2.imread(i) frame = Frame(1, img, 1) u_r = Roi(frame.img.shape) pupil_datum = detector2d.detect(frame, user_roi=u_r, visualize=False) pupil_norm = (pupil_datum['norm_pos'][0], pupil_datum['norm_pos'][1]) pupil_positions.append(pupil_norm) f = open(calibration_path + "gaze_in_scene_camera.txt", "r") for line in f: if line.startswith(os.path.splitext(i)[0]): ref_norm = (float(line.split(' ')[1]), float(line.split(' ')[2])) ref_positions.append(ref_norm) break cal_data = [(pupil_positions[i][0], pupil_positions[i][1], ref_positions[i][0], ref_positions[i][1]) for i in range(len(ref_positions))] return cal_data
'contour_size_min': 5, 'ellipse_roundness_ratio': 0.1, 'initial_ellipse_fit_treshhold': 1.8, 'final_perimeter_ratio_range_min': 0.6, 'final_perimeter_ratio_range_max': 1.2, 'ellipse_true_support_min_dist': 2.5, 'support_pixel_ratio_exponent': 2.0 ''' vout = None if int(sys.argv[1]): fourcc = cv2.VideoWriter_fourcc(*'x264') vout = cv2.VideoWriter( 'pupilnorm.mp4', fourcc, 24, (int(frame.img.shape[1]), int(frame.img.shape[0])) ) roi = Roi(frame.img.shape) offset = [-0.64, -1.28, 0.0] while True: image0 = vs0.read() image1 = vs1.read() if image0 is not None: # image0 = cv2.rotate(image0, cv2.ROTATE_90_CLOCKWISE) frame.gray = cv2.cvtColor(image0, cv2.COLOR_BGR2GRAY) frame.img = image0.copy() prevImage = image0.copy() frame.timestamp = time.time() else: frame.img = prevImage.copy() frame.gray = cv2.cvtColor(prevImage, cv2.COLOR_BGR2GRAY) frame.timestamp = time.time()
cap_size = (640,480) # Initialize capture cap = autoCreateCapture(cap_src, timebase=None) default_settings = {'frame_size':cap_size,'frame_rate':30} cap.settings = default_settings # Test capture try: frame = cap.get_frame() except CameraCaptureError: print "Could not retrieve image from capture" cap.close() Pool = namedtuple('Pool', 'user_dir'); pool = Pool('/') u_r = Roi(frame.img.shape) #Our detectors we wanna compare detector_cpp = detector_2d.Detector_2D() detector_py = Canny_Detector(pool) # detector_py.coarse_detection= False test_file_Folder = '../../../../../Pupil_Test_Files/' # write files to the project root folder def compareEllipse( ellipse_cpp , ellipse_py): return \ abs(ellipse_cpp['center'][0] - ellipse_py['center'][0]) <.1 and \ abs(ellipse_cpp['center'][1] - ellipse_py['center'][1]) <.1 and \ abs(ellipse_cpp['major'] - ellipse_py['major'])<.1 and \ abs(ellipse_cpp['minor'] - ellipse_py['minor'])<.1