Exemple #1
0
    def preprocess(self, filepath: Path) -> None:
        """Process one file."""
        try:
            image = np.array(utils.imread(filepath))
        except Exception as e:
            logger.warning(f"Cannot read file {filepath}, exception: {e}")
            return

        filename, extention = filepath.stem, filepath.suffix
        out_dir = (self.out_dir / filepath.relative_to(self.in_dir)).parent
        out_dir.mkdir(parents=True, exist_ok=True)

        patches = cut_with_overlap(
            image=image.clip(0, 255).round().astype(np.uint8),
            patch_height=self.patch_height,
            patch_width=self.patch_width,
            height_overlap=self.height_overlap,
            width_overlap=self.width_overlap,
            min_height=self.min_height,
            min_width=self.min_width,
        )

        for index, patch in enumerate(patches):
            out_path = out_dir / f"{filename}_{index}{extention}"
            utils.imwrite(uri=out_path, im=patch)
Exemple #2
0
    def preprocess(self, image_path: Path):
        """@TODO: Docs. Contribution is welcome."""
        try:
            _, extension = os.path.splitext(image_path)
            kwargs = {
                "grayscale": self.grayscale,
                "expand_dims": self.expand_dims,
            }
            if extension.lower() in {"jpg", "jpeg"}:
                # imread does not have exifrotate for non-jpeg type
                kwargs["exifrotate"] = not self.clear_exif

            image = np.array(imread(uri=image_path, **kwargs))
        except Exception as e:
            print(f"Cannot read file {image_path}, exception: {e}")
            return

        if self.max_size is not None:
            image = longest_max_size(image, self.max_size, self.interpolation)

        target_path = self.out_dir / image_path.relative_to(self.in_dir)
        target_path.parent.mkdir(parents=True, exist_ok=True)

        image = image.clip(0, 255).round().astype(np.uint8)
        imwrite(target_path, image)
Exemple #3
0
    def preprocess(self, image_path: Path):
        try:
            if self.extension in ("jpg", "JPG", "jpeg", "JPEG"):
                image = np.array(
                    imread(uri=image_path,
                           grayscale=self.grayscale,
                           expand_dims=self.expand_dims,
                           exifrotate=not self.clear_exif))
            else:  # imread does not have exifrotate for non-jpeg type
                image = np.array(
                    imread(uri=image_path,
                           grayscale=self.grayscale,
                           expand_dims=self.expand_dims))
        except Exception as e:
            print(f"Cannot read file {image_path}, exception: {e}")
            return

        if self.max_size is not None:
            image = longest_max_size(image, self.max_size, self.interpolation)

        target_path = self.out_dir / image_path.relative_to(self.in_dir)
        target_path.parent.mkdir(parents=True, exist_ok=True)

        image = image.clip(0, 255).round().astype(np.uint8)
        imwrite(target_path, image)
Exemple #4
0
    def preprocess(self, image_path: Path):
        try:
            image = np.array(
                imread(uri=image_path,
                       grayscale=self.grayscale,
                       expand_dims=self.expand_dims,
                       exifrotate=not self.clear_exif))
        except Exception:
            return

        if self.max_size is not None:
            image = longest_max_size(image, self.max_size, self.interpolation)

        target_path = self.out_dir / image_path.relative_to(self.in_dir)
        target_path.parent.mkdir(parents=True, exist_ok=True)

        image = image.clip(0, 255).round().astype(np.uint8)
        imwrite(target_path, image)