Esempio n. 1
0
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']
Esempio n. 2
0
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
Esempio n. 3
0
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']
Esempio n. 4
0
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
Esempio n. 5
0
  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
    # abs(ellipse_cpp['angle'] - ellipse_py['angle'])<10.1

  def compare_dict(first, second):