def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    figure_set = FigureSet()
    figure_set.load_list(args.list_path)
    with open(args.annotation_path, 'w', newline='') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')
        for idx, file in enumerate(figure_set.files):
            # if '1465-9921-6-55-4' not in file:
            #     continue
            print('Processing Image {:d}: {:s}.'.format(idx, file))

            # Create figure object
            figure = Figure(file)

            # Load image
            figure.load_image()

            # Load annotation
            xml_path = os.path.join(
                figure.image_path.replace('.jpg', '_data.xml'))
            figure.load_annotation_iphotodraw(xml_path)

            # write to CSV file
            # The format is:
            # image_path,label_x1,label_y1,label_x2,label_y2,label
            # if there is no label, the format becomes:
            # image_path,,,,,

            # TODO : factorize this
            has_label = False
            for panel in figure.panels:
                if panel.label_rect is not None:
                    label = map_label(panel.label)
                    if len(label
                           ) == 1:  # We care for single letter label for now
                        row = list()

                        # add image_path
                        row.append(figure.image_path)
                        row.append(str(panel.label_rect[0]))
                        row.append(str(panel.label_rect[1]))
                        row.append(str(panel.label_rect[2]))
                        row.append(str(panel.label_rect[3]))
                        row.append(label)
                        csv_writer.writerow(row)
                        has_label = True

            if not has_label:
                row = list()
                row.append(figure.image_path)  # add image_path
                row.append('')
                row.append('')
                row.append('')
                row.append('')
                row.append('')
                csv_writer.writerow(row)
Пример #2
0
    def save_gt_preview(self):
        for idx, file in enumerate(self.files):
            # if idx != 5:
            #     continue
            logging.info('Generate GT Annotation Preview for Image %d: %s.',
                         idx, file)

            try:
                if not os.path.exists(file):
                    raise PanelSegError('Could not find {}.'.format(file))
                figure = Figure()
                figure.load_image(file)

                xml_path = os.path.join(
                    figure.image_path.replace('.jpg', '_data.xml'))
                if not os.path.exists(xml_path):
                    raise PanelSegError('Could not find {}.'.format(xml_path))
                figure.load_annotation_iphotodraw(xml_path)

            except PanelSegError as ex:
                logging.warning(ex.message)
                continue

            # Save preview
            folder, file = os.path.split(figure.image_path)
            folder = os.path.join(folder, "prev")
            figure.save_preview(folder)
Пример #3
0
def eval_mAP(args):
    #  Read all figures to be evaluated
    with open(args.eval_list) as f:
        lines = f.readlines()

    all_boxes = []
    all_scores = []
    all_num = []
    figures = []
    with open(args.eval_file, 'a') as f:

        for idx, filepath in enumerate(lines):
            print(str(idx) + ': ' + filepath)
            filepath = filepath.strip()
            figure = Figure(filepath, padding=0)
            figure.load_gt_annotation(which_annotation='panel')

            pickle_file = os.path.join(args.auto_dir,
                                       figure.file.replace('.jpg', '.pickle'))
            with open(pickle_file, 'rb') as handle:
                boxes, scores, classes, num = pickle.load(handle)

            all_boxes.append(boxes[0])
            all_scores.append(scores[0])
            all_num.append(num[0])
            figures.append(figure)
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    figure_set = FigureSet()
    figure_set.load_list(args.list_path)
    with open(args.annotation_path, 'w', newline='') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')
        for idx, file in enumerate(figure_set.files):
            # if '1465-9921-6-55-4' not in file:
            #     continue
            print('Processing Image {:d}: {:s}.'.format(idx, file))
            figure = Figure(file)
            figure.load_image()
            xml_path = os.path.join(
                figure.image_path.replace('.jpg', '_data.xml'))
            figure.load_annotation_iphotodraw(xml_path)

            # write to CSV file
            for panel in figure.panels:
                csv_writer.writerow([
                    figure.image_path,
                    str(panel.panel_rect[0]),
                    str(panel.panel_rect[1]),
                    str(panel.panel_rect[2]),
                    str(panel.panel_rect[3]), 'panel'
                ])
Пример #5
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    figure_set = FigureSet()
    figure_set.load_list(args.list_path)
    with open(args.annotation_path, 'w', newline='') as csvfile:
        csv_writer = csv.writer(csvfile, delimiter=',')
        for idx, file in enumerate(figure_set.files):
            # if '1465-9921-6-55-4' not in file:
            #     continue
            print('Processing Image {:d}: {:s}.'.format(idx, file))
            figure = Figure(file)
            figure.load_image()
            xml_path = os.path.join(
                figure.image_path.replace('.jpg', '_data.xml'))
            figure.load_annotation_iphotodraw(xml_path)

            # write to CSV file
            # The format is:
            # image_path,panel_x1,panel_y1,panel_x2,panel_y2,label_x1,label_y1,label_x2,label_y2,label
            # if there is no label, the format becomes:
            # image_path,panel_x1,panel_y1,panel_x2,panel_y2,,,,,
            for panel in figure.panels:
                row = list()
                row.append(figure.image_path)  # add image_path
                row.append(str(panel.panel_rect[0]))
                row.append(str(panel.panel_rect[1]))
                row.append(str(panel.panel_rect[2]))
                row.append(str(panel.panel_rect[3]))
                row.append('panel')
                if panel.label_rect is None:
                    row.append('')
                    row.append('')
                    row.append('')
                    row.append('')
                    row.append('')
                else:
                    label = map_label(panel.label)
                    row.append(str(panel.label_rect[0]))
                    row.append(str(panel.label_rect[1]))
                    row.append(str(panel.label_rect[2]))
                    row.append(str(panel.label_rect[3]))
                    row.append(label)
                csv_writer.writerow(row)
Пример #6
0
    def validate_annotation(self):
        for idx, file in enumerate(self.files):
            # if idx != 5:
            #     continue
            logging.info('Validate Annotation of Image %d: %s.', idx, file)

            try:
                if not os.path.exists(file):
                    raise PanelSegError('Could not find {}.'.format(file))
                figure = Figure()
                figure.load_image(file)

                xml_path = os.path.join(
                    figure.image_path.replace('.jpg', '_data.xml'))
                if not os.path.exists(xml_path):
                    raise PanelSegError('Could not find {}.'.format(xml_path))
                figure.load_annotation_iphotodraw(xml_path)

            except PanelSegError as ex:
                logging.warning(ex.message)
                continue
Пример #7
0
def eval_ImageCLEF(args):
    def compute_metric(gt_panels, auto_panels):

        correct = 0
        picked_auto_idx = [False] * len(auto_panels)
        for gt_idx, gt_panel in enumerate(gt_panels):
            max_overlapping = -1
            max_auto_idx = -1
            for auto_idx, auto_panel in enumerate(auto_panels):
                if picked_auto_idx[auto_idx]:
                    continue
                intersection = intersect(auto_panel.panel_rect,
                                         gt_panel.panel_rect)
                if intersection is None:
                    continue
                intersection_area = (intersection[2] - intersection[0]) * (
                    intersection[3] - intersection[1])
                auto_area = (
                    auto_panel.panel_rect[2] - auto_panel.panel_rect[0]) * (
                        auto_panel.panel_rect[3] - auto_panel.panel_rect[1])
                overlapping = intersection_area / auto_area
                if overlapping > max_overlapping:
                    max_overlapping = overlapping
                    max_auto_idx = auto_idx
            # if max_overlapping > 0.66:
            if max_overlapping > 0.5:
                correct += 1
                picked_auto_idx[max_auto_idx] = True

        return correct

    #  Read all figures to be evaluated
    with open(args.eval_list) as f:
        lines = f.readlines()

    with open(args.eval_file, 'a') as f:

        overall_correct_count = 0
        overall_gt_count = 0
        overall_auto_count = 0

        overall_accuracy = 0.0
        overall_recall = 0.0
        overall_precision = 0.0

        for idx, filepath in enumerate(lines):
            # if '1471-213X-8-30-5' not in filepath:
            #     continue

            print(str(idx) + ': ' + filepath)
            filepath = filepath.strip()
            figure = Figure(filepath)
            if 'clef_eval' in args.eval_list:
                csv_path = os.path.join(
                    figure.image_path.replace('.jpg', '.csv'))
                figure.load_annotation_csv(csv_path)
            else:
                figure.load_image()
                xml_path = os.path.join(
                    figure.image_path.replace('.jpg', '_data.xml'))
                figure.load_annotation_iphotodraw(xml_path)
            gt_panels = figure.panels

            image_folder, image_file = os.path.split(figure.image_path)
            auto_file = os.path.join(args.auto_dir,
                                     image_file.replace('.jpg', '.csv'))
            figure.load_annotation_csv(auto_file)
            auto_panels = figure.panels

            correct = compute_metric(gt_panels, auto_panels)

            k = max(len(auto_panels), len(gt_panels))
            accuracy = correct / k
            recall = correct / len(gt_panels)
            if len(auto_panels) == 0:
                precision = 0
            else:
                precision = correct / len(auto_panels)

            f.write("{0}: {1}\t{2}\t{3}\n".format(figure.image_path, accuracy,
                                                  precision, recall))

            overall_accuracy += accuracy
            overall_recall += recall
            overall_precision += precision

            overall_correct_count += correct
            overall_gt_count += len(gt_panels)
            overall_auto_count += len(auto_panels)

        overall_accuracy /= len(lines)
        overall_precision /= len(lines)
        overall_recall /= len(lines)

        f.write("Overall ImageCLEF Accuracy: {}, Precision: {}, Recall: {}\n".
                format(overall_accuracy, overall_precision, overall_recall))

        overall_accuracy = overall_correct_count / max(overall_auto_count,
                                                       overall_gt_count)
        overall_recall = overall_correct_count / overall_gt_count
        overall_precision = overall_correct_count / overall_auto_count

        f.write("Overall Accuracy: {}, Precision: {}, Recall: {}\n".format(
            overall_accuracy, overall_precision, overall_recall))