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)
Example #2
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)
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)
Example #4
0
    def to_tf_example(self):
        """
        Convert to TfRecord Example
        """
        panel_xmins = []
        panel_ymins = []
        panel_xmaxs = []
        panel_ymaxs = []
        label_xmins = []
        label_ymins = []
        label_xmaxs = []
        label_ymaxs = []
        label_texts = []
        label_classes = []
        for panel in self.panels:
            panel_xmins.append(panel.panel_rect[0] / self.image_width)
            panel_ymins.append(panel.panel_rect[1] / self.image_height)
            panel_xmaxs.append(panel.panel_rect[2] / self.image_width)
            panel_ymaxs.append(panel.panel_rect[3] / self.image_height)

            if panel.label_rect is not None:
                label_text = panel.label
                if len(label_text
                       ) == 1:  # We handle 1 char label only for now.
                    label_text = misc.map_label(label_text)
                    label_texts.append(label_text.encode('utf8'))
                    label_classes.append(misc.LABEL_CLASS_MAPPING[label_text])
                    label_xmins.append(panel.label_rect[0] / self.image_width)
                    label_ymins.append(panel.label_rect[1] / self.image_height)
                    label_xmaxs.append(panel.label_rect[2] / self.image_width)
                    label_ymaxs.append(panel.label_rect[3] / self.image_height)
                else:
                    label_text = misc.map_label(label_text)
                    label_texts.append(label_text.encode('utf8'))
                    label_classes.append(
                        -1)  # We handle 1 char label only for now.
                    label_xmins.append(panel.label_rect[0] / self.image_width)
                    label_ymins.append(panel.label_rect[1] / self.image_height)
                    label_xmaxs.append(panel.label_rect[2] / self.image_width)
                    label_ymaxs.append(panel.label_rect[3] / self.image_height)
            else:
                label_texts.append('NONE'.encode('utf8'))
                label_classes.append(-1)
                label_xmins.append(0)
                label_xmaxs.append(0)
                label_ymaxs.append(0)
                label_ymins.append(0)

        feature_dict = {
            'image/height':
            misc.int64_feature(self.image_height),
            'image/width':
            misc.int64_feature(self.image_width),
            'image/filename':
            misc.bytes_feature(self.image_path.encode('utf8')),
            'image/image':
            misc.bytes_feature(tf.compat.as_bytes(self.image.tostring())),
            'image/panel/bbox/xmin':
            misc.float_list_feature(panel_xmins),
            'image/panel/bbox/ymin':
            misc.float_list_feature(panel_ymins),
            'image/panel/bbox/xmax':
            misc.float_list_feature(panel_xmaxs),
            'image/panel/bbox/ymax':
            misc.float_list_feature(panel_ymaxs),
            'image/label/bbox/xmin':
            misc.float_list_feature(label_xmins),
            'image/label/bbox/ymin':
            misc.float_list_feature(label_ymins),
            'image/label/bbox/xmax':
            misc.float_list_feature(label_xmaxs),
            'image/label/bbox/ymax':
            misc.float_list_feature(label_ymaxs),
            'image/panel/label/text':
            misc.bytes_list_feature(label_texts),
            'image/panel/label/class':
            misc.int64_list_feature(label_classes),
        }

        example = tf.train.Example(features=tf.train.Features(
            feature=feature_dict))
        return example
Example #5
0
    def to_tf_example(self):
        """
        Convert to TfRecord Example
        """
        panel_xmins = []
        panel_ymins = []
        panel_xmaxs = []
        panel_ymaxs = []
        label_xmins = []
        label_ymins = []
        label_xmaxs = []
        label_ymaxs = []
        label_texts = []
        label_classes = []
        for panel in self.panels:
            panel_xmins.append(panel.panel_rect[0]/self.image_width)
            panel_ymins.append(panel.panel_rect[1]/self.image_height)
            panel_xmaxs.append(panel.panel_rect[2]/self.image_width)
            panel_ymaxs.append(panel.panel_rect[3]/self.image_height)

            if panel.label_rect is not None:
                label_text = panel.label
                if len(label_text) == 1:    # We handle 1 char label only for now.
                    label_text = misc.map_label(label_text)
                    label_texts.append(label_text.encode('utf8'))
                    label_classes.append(misc.LABEL_CLASS_MAPPING[label_text])
                    label_xmins.append(panel.label_rect[0]/self.image_width)
                    label_ymins.append(panel.label_rect[1]/self.image_height)
                    label_xmaxs.append(panel.label_rect[2]/self.image_width)
                    label_ymaxs.append(panel.label_rect[3]/self.image_height)
                else:
                    label_text = misc.map_label(label_text)
                    label_texts.append(label_text.encode('utf8'))
                    label_classes.append(-1)    # We handle 1 char label only for now.
                    label_xmins.append(panel.label_rect[0]/self.image_width)
                    label_ymins.append(panel.label_rect[1]/self.image_height)
                    label_xmaxs.append(panel.label_rect[2]/self.image_width)
                    label_ymaxs.append(panel.label_rect[3]/self.image_height)
            else:
                label_texts.append('NONE'.encode('utf8'))
                label_classes.append(-1)
                label_xmins.append(0)
                label_xmaxs.append(0)
                label_ymaxs.append(0)
                label_ymins.append(0)

        feature_dict = {
            'image/height': misc.int64_feature(self.image_height),
            'image/width': misc.int64_feature(self.image_width),
            'image/filename': misc.bytes_feature(self.image_path.encode('utf8')),
            'image/image': misc.bytes_feature(tf.compat.as_bytes(self.image.tostring())),
            'image/panel/bbox/xmin': misc.float_list_feature(panel_xmins),
            'image/panel/bbox/ymin': misc.float_list_feature(panel_ymins),
            'image/panel/bbox/xmax': misc.float_list_feature(panel_xmaxs),
            'image/panel/bbox/ymax': misc.float_list_feature(panel_ymaxs),
            'image/label/bbox/xmin': misc.float_list_feature(label_xmins),
            'image/label/bbox/ymin': misc.float_list_feature(label_ymins),
            'image/label/bbox/xmax': misc.float_list_feature(label_xmaxs),
            'image/label/bbox/ymax': misc.float_list_feature(label_ymaxs),
            'image/panel/label/text': misc.bytes_list_feature(label_texts),
            'image/panel/label/class': misc.int64_list_feature(label_classes),
        }

        example = tf.train.Example(features=tf.train.Features(feature=feature_dict))
        return example