def getTrackingErrors(tracker_path_orig,
                      gt_path,
                      _arch_fid=None,
                      _reinit_from_gt=0,
                      _reinit_frame_skip=5,
                      _use_reinit_gt=0,
                      start_ids=None,
                      _err_type=0,
                      _overflow_err=1e3,
                      _show_jaccard_img=0):
    print 'Reading ground truth from: {:s}...'.format(gt_path)
    if _use_reinit_gt:
        n_gt_frames, gt_data = readReinitGT(gt_path, 0)
    else:
        n_gt_frames, gt_data = readGT(gt_path)

    if n_gt_frames is None or gt_data is None:
        print "Ground truth could not be read successfully"
        return None, None

    if start_ids is None:
        start_ids = [0]

    tracking_errors = []
    failure_count = 0
    for start_id in start_ids:
        if start_id == 0:
            tracker_path = tracker_path_orig
        else:
            tracker_path = tracker_path_orig.replace(
                '.txt', '_init_{:d}.txt'.format(start_id))
        print 'Reading tracking data for start_id {:d} from: {:s}...'.format(
            start_id, tracker_path)
        if _arch_fid is not None:
            tracking_data = _arch_fid.open(tracker_path, 'r').readlines()
        else:
            tracking_data = open(tracker_path, 'r').readlines()
        if len(tracking_data) < 2:
            print 'Tracking data file is invalid.'
            return None, None
        # remove header
        del (tracking_data[0])
        n_lines = len(tracking_data)

        if not _reinit_from_gt and n_lines != n_gt_frames - start_id:
            print "No. of frames in tracking result ({:d}) and the ground truth ({:d}) do not match".format(
                n_lines, n_gt_frames)
            return None, None

        reinit_gt_id = 0
        reinit_start_id = 0
        # ignore the first frame where tracker was initialized
        line_id = 1
        invalid_tracker_state_found = False
        is_initialized = True
        # id of the last frame where tracking failure was detected
        failure_frame_id = -1

        while line_id < n_lines:
            tracking_data_line = tracking_data[line_id].strip().split()
            frame_fname = str(tracking_data_line[0])
            fname_len = len(frame_fname)
            frame_fname_1 = frame_fname[0:5]
            frame_fname_2 = frame_fname[-4:]
            if frame_fname_1 != 'frame' or frame_fname_2 != '.jpg':
                print 'Invaid formatting on tracking data line {:d}: {:s}'.format(
                    line_id + 1, tracking_data_line)
                print 'frame_fname: {:s} fname_len: {:d} frame_fname_1: {:s} frame_fname_2: {:s}'.format(
                    frame_fname, fname_len, frame_fname_1, frame_fname_2)
                return None, None
            frame_id_str = frame_fname[5:-4]
            frame_num = int(frame_id_str)
            if not _reinit_from_gt and frame_num != start_id + line_id + 1:
                print "Unexpected frame number {:d} found in line {:d} of tracking result for start_id {:d}: {:s}".format(
                    frame_num, line_id + 1, start_id, tracking_data_line)
                return None, None
            if is_initialized:
                # id of the frame in which the tracker is reinitialized
                reinit_start_id = frame_num - 2
                if failure_frame_id >= 0 and reinit_start_id != failure_frame_id + _reinit_frame_skip:
                    print 'Tracker was reinitialized in frame {:d} rather than {:d} where it should have been with {:d} frames being skipped'.format(
                        reinit_start_id + 1,
                        failure_frame_id + _reinit_frame_skip + 1,
                        _reinit_frame_skip)
                    return None, None
                is_initialized = False

            # print 'line_id: {:d} frame_id_str: {:s} frame_num: {:d}'.format(
            # line_id, frame_id_str, frame_num)
            if len(tracking_data_line) != 9:
                if _reinit_from_gt and len(
                        tracking_data_line
                ) == 2 and tracking_data_line[1] == 'tracker_failed':
                    print 'tracking failure detected in frame: {:d} at line {:d}'.format(
                        frame_num, line_id + 1)
                    failure_count += 1
                    failure_frame_id = frame_num - 1
                    # skip the frame where the tracker failed as well as the one where it was reinitialized
                    # whose result will (or should) be in the line following this one
                    line_id += 2
                    is_initialized = True
                    continue
                elif len(tracking_data_line) == 2 and tracking_data_line[
                        1] == 'invalid_tracker_state':
                    if not invalid_tracker_state_found:
                        print 'invalid tracker state detected in frame: {:d} at line {:d}'.format(
                            frame_num, line_id + 1)
                        invalid_tracker_state_found = True
                    line_id += 1
                    tracking_errors.append(_overflow_err)
                    continue
                else:
                    print 'Invalid formatting on line {:d}: {:s}'.format(
                        line_id, tracking_data[line_id])
                    return None, None
            # if is_initialized:frame_num
            # is_initialized = False
            # line_id += 1
            # continue

            if _use_reinit_gt:
                if reinit_gt_id != reinit_start_id:
                    n_gt_frames, gt_data = readReinitGT(
                        gt_path, reinit_start_id)
                    reinit_gt_id = reinit_start_id
                curr_gt = gt_data[frame_num - reinit_start_id - 1]
            else:
                curr_gt = gt_data[frame_num - 1]

            curr_tracking_data = [
                float(tracking_data_line[1]),
                float(tracking_data_line[2]),
                float(tracking_data_line[3]),
                float(tracking_data_line[4]),
                float(tracking_data_line[5]),
                float(tracking_data_line[6]),
                float(tracking_data_line[7]),
                float(tracking_data_line[8])
            ]
            # print 'line_id: {:d} gt: {:s}'.format(line_id, gt_line)

            if _err_type == 0:
                err = getMeanCornerDistanceError(curr_tracking_data, curr_gt,
                                                 _overflow_err)
            elif _err_type == 1:
                err = getCenterLocationError(curr_tracking_data, curr_gt)
            elif _err_type == 2:
                err = getJaccardError(curr_tracking_data, curr_gt,
                                      _show_jaccard_img)
            else:
                print 'Invalid error type provided: {:d}'.format(_err_type)
                return None, None
            tracking_errors.append(err)
            line_id += 1
        if _reinit_from_gt and n_lines < n_gt_frames - failure_count * (
                _reinit_frame_skip - 1):
            print "Unexpected no. of frames in reinit tracking result ({:d}) which should be at least {:d}".format(
                n_lines,
                n_gt_frames - failure_count * (_reinit_frame_skip - 1))
            return None, None
    return tracking_errors, failure_count
Пример #2
0
                gt_corners_fname = '{:s}/{:s}/ReinitGT/{:s}_{:s}.bin'.format(
                    db_root_dir, actor, seq_name, opt_gt_ssm)
            else:
                gt_corners_fname = '{:s}/{:s}/ReinitGT/{:s}.bin'.format(
                    db_root_dir, actor, seq_name)
        else:
            if use_opt_gt:
                gt_corners_fname = '{:s}/{:s}/OptGT/{:s}_{:s}.txt'.format(
                    db_root_dir, actor, seq_name, opt_gt_ssm)
            else:
                gt_corners_fname = '{:s}/{:s}/{:s}.txt'.format(
                    db_root_dir, actor, seq_name)

        print 'Reading ground truth from:: ', gt_corners_fname
        if use_reinit_gt:
            no_of_frames, ground_truth = readReinitGT(gt_corners_fname,
                                                      init_frame_id)
        else:
            no_of_frames, ground_truth = readGT(gt_corners_fname)

        src_dir = db_root_dir + '/' + actor + '/' + seq_name
        if video_fmt is None:
            src_fname = db_root_dir + '/' + actor + '/' + seq_name + '/' + img_name_fmt
            file_list = [
                each for each in os.listdir(src_dir) if each.endswith('.jpg')
            ]
            n_files = len(file_list)
        else:
            src_fname = db_root_dir + '/' + actor + '/' + seq_name + '.' + video_fmt
            n_files = -1
        cap = cv2.VideoCapture()
        if not cap.open(src_fname):