Exemple #1
0
    def _generate_epoch(self, text_only) -> Generator[InputSample, None, None]:
        filenames = list(self.filenames)
        if self.mode == PipelineMode.Training:
            shuffle(filenames)

        for filename in filenames:
            with h5py.File(filename, 'r') as f:
                codec = list(map(chr, f['codec']))
                if text_only:
                    for i, (text, idx) in enumerate(
                            zip(f['transcripts'],
                                range(len(f['transcripts'])))):
                        text = "".join([codec[c] for c in text])
                        fold_id = idx % self.n_folds if self.n_folds > 0 else -1
                        yield InputSample(
                            None, text,
                            SampleMeta(id=f"{filename}/{i}", fold_id=fold_id))
                else:
                    gen = zip(f['images'], f['images_dims'], f['transcripts'],
                              range(len(f['images'])))
                    if self.mode == PipelineMode.Training:
                        gen = list(gen)
                        shuffle(gen)

                    for i, (image, shape, text, idx) in enumerate(gen):
                        image = np.reshape(image, shape)
                        text = "".join([codec[c] for c in text])
                        fold_id = idx % self.n_folds if self.n_folds > 0 else -1
                        yield InputSample(
                            image, text,
                            SampleMeta(id=f"{filename}/{i}", fold_id=fold_id))
Exemple #2
0
 def _load_sample(self, sample, text_only):
     if text_only:
         yield InputSample(
             None,
             self._load_gt_txt(sample["text_path"]),
             SampleMeta(sample['id'], fold_id=sample['fold_id']),
         )
     else:
         yield InputSample(
             self._load_line(sample["image_path"]),
             self._load_gt_txt(sample["text_path"]),
             SampleMeta(sample['id'], fold_id=sample['fold_id']),
         )
Exemple #3
0
    def _generate_epoch(self, text_only) -> Generator[InputSample, None, None]:
        fold_id = -1
        for p, page in enumerate(self.book.pages):
            if self.mode in INPUT_PROCESSOR:
                img = load_image(page.imgFile)
                if self.binary:
                    img = img > 0.9
            else:
                img = None

            for l, line in enumerate(page.getLines()):
                for f, fo in enumerate(line.formats):
                    fold_id += 1
                    sample_id = "{}_{}_{}_{}".format(
                        os.path.splitext(
                            page.xmlFile if page.xmlFile else page.imgFile)[0],
                        p, l, f)
                    text = None
                    if self.mode in TARGETS_PROCESSOR:
                        text = fo.text

                    if text_only:
                        yield InputSample(
                            None, text,
                            SampleMeta(id=sample_id, fold_id=fold_id))

                    else:
                        cut_img = None
                        if self.mode in INPUT_PROCESSOR:
                            ly, lx = img.shape

                            # Cut the Image
                            cut_img = img[line.rect.top:-ly + line.rect.bottom,
                                          line.rect.left:-lx + line.rect.right]

                            # add padding as required from normal files
                            cut_img = np.pad(cut_img, ((3, 3), (0, 0)),
                                             mode='constant',
                                             constant_values=cut_img.max())

                        yield InputSample(
                            cut_img, text,
                            SampleMeta(id=sample_id, fold_id=fold_id))
Exemple #4
0
    def _generate_epoch(self, text_only) -> Generator[InputSample, None, None]:
        for filename in self.filenames:
            f = h5py.File(filename, 'r')
            codec = list(map(chr, f['codec']))
            if text_only:
                for i, text in enumerate(f['transcripts']):
                    text = "".join([codec[c] for c in text])
                    yield InputSample(None, text,
                                      SampleMeta(id=f"{filename}/{i}"))
            else:
                gen = zip(f['images'], f['images_dims'], f['transcripts'])
                if self.mode == PipelineMode.Training:
                    gen = list(gen)
                    shuffle(gen)

                for i, (image, shape, text) in enumerate(gen):
                    image = np.reshape(image, shape)
                    text = "".join([codec[c] for c in text])
                    yield InputSample(image, text,
                                      SampleMeta(id=f"{filename}/{i}"))
Exemple #5
0
    def __init__(self, mode: PipelineMode, images=None, texts=None, meta=None):
        """ Create a dataset from memory

        Since this dataset already contains all data in the memory, this dataset may not be loaded

        Parameters
        ----------
        images : list of images
            the images of the dataset
        texts : list of str
            the texts of this dataset
        """
        super().__init__(mode, skip_invalid=False, remove_invalid=False)

        if images is None and texts is None:
            raise Exception(
                "Empty data set is not allowed. Both images and text files are None"
            )

        if images is not None and texts is not None and len(
                images) == 0 and len(texts) == 0:
            raise Exception("Empty data set provided.")

        if texts is None or len(texts) == 0:
            if images is None:
                raise Exception("Empty data set.")

            # No gt provided, probably prediction
            texts = [None] * len(images)

        if images is None or len(images) == 0:
            if len(texts) is None:
                raise Exception("Empty data set.")

            # No images provided, probably evaluation
            images = [None] * len(texts)

        if not meta:
            meta = [SampleMeta(str(i), None) for i in range(len(images))]

        for image, text, meta in zip(images, texts, meta):
            self.add_sample({
                "image": image,
                "text": text,
                "id": meta.id,
                "meta": meta,
            })

        self.loaded = True
Exemple #6
0
    def _load_sample(self, sample,
                     text_only) -> Generator[InputSample, None, None]:
        loader = PageXMLDatasetLoader(self.mode, self._non_existing_as_empty,
                                      self.text_index, self.skip_invalid)
        image_path, xml_path = sample

        img = None
        if self.mode in INPUT_PROCESSOR:
            img = load_image(image_path)

        for sample in loader.load(image_path, xml_path):
            text = sample["text"]
            orientation = sample["orientation"]

            if not text_only and self.mode in INPUT_PROCESSOR:
                ly, lx = img.shape[:2]

                line_img = PageXMLReader.cutout(img, sample['coords'],
                                                lx / sample['img_width'])

                # rotate by orientation angle in clockwise direction to correct present skew
                # (skimage rotates in counter-clockwise direction)
                if orientation and orientation % 360 != 0:
                    line_img = rotate(line_img,
                                      orientation * -1,
                                      resize=True,
                                      mode='constant',
                                      cval=line_img.max(),
                                      preserve_range=True).astype(np.uint8)

                # add padding as required from normal files
                if self.args.pad:
                    pad = self.args.pad
                    img = np.pad(img,
                                 pad,
                                 mode='constant',
                                 constant_values=img.max())
            else:
                line_img = None

            yield InputSample(line_img, text, SampleMeta(id=sample['id']))
Exemple #7
0
    def _load_sample(self, sample,
                     text_only) -> Generator[InputSample, None, None]:
        loader = PageXMLDatasetLoader(self.mode, self._non_existing_as_empty,
                                      self.text_index, self.skip_invalid)
        image_path, xml_path, idx = sample

        img = None
        if self.mode in INPUT_PROCESSOR:
            img = load_image(image_path)

        for i, sample in enumerate(loader.load(image_path, xml_path)):
            fold_id = (idx + i) % self.n_folds if self.n_folds > 0 else -1
            text = sample["text"]
            orientation = sample["orientation"]

            if not text_only and self.mode in INPUT_PROCESSOR:
                ly, lx = img.shape[:2]

                # rotate by orientation angle in clockwise direction to correct present skew
                angle = orientation if orientation and orientation % 360 != 0 else 0

                line_img = PageXMLReader.cutout(img,
                                                sample['coords'],
                                                mode=CutMode.POLYGON,
                                                angle=angle,
                                                cval=None,
                                                scale=lx / sample['img_width'])

                # add padding as required from normal files
                if self.args.pad:
                    pad = self.args.pad
                    img = np.pad(img,
                                 pad,
                                 mode='constant',
                                 constant_values=img.max())
            else:
                line_img = None

            yield InputSample(line_img, text,
                              SampleMeta(id=sample['id'], fold_id=fold_id))
Exemple #8
0
 def _load_sample(self, sample, text_only):
     image, text = self.data_queue.get()
     fold_id = -1 if self.n_folds <= 0 else np.random.randint(self.n_folds)
     yield InputSample(image, text,
                       SampleMeta(id=sample['id'], fold_id=fold_id))
Exemple #9
0
 def _load_sample(self, sample, text_only):
     image, text = self.data_queue.get()
     yield InputSample(image, text, SampleMeta(id=sample['id']))