Skip to content

read_image

ReadImage

Bases: NumpyOp

A class for reading png or jpg images from disk.

Parameters:

Name Type Description Default
inputs Union[str, Iterable[str], Callable]

Key(s) of paths to images to be loaded.

required
outputs Union[str, Iterable[str]]

Key(s) of images to be output.

required
mode Union[None, str, Iterable[str]]

What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument like "!infer" or "!train".

None
parent_path str

Parent path that will be prepended to a given path.

''
color_flag Union[str, int]

Whether to read the image as 'color', 'grey', or one of the cv2.IMREAD flags.

cv2.IMREAD_COLOR

Raises:

Type Description
AssertionError

If inputs and outputs have mismatched lengths, or the color_flag is unacceptable.

Source code in fastestimator\fastestimator\op\numpyop\univariate\read_image.py
class ReadImage(NumpyOp):
    """A class for reading png or jpg images from disk.

    Args:
        inputs: Key(s) of paths to images to be loaded.
        outputs: Key(s) of images to be output.
        mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
            regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
            like "!infer" or "!train".
        parent_path: Parent path that will be prepended to a given path.
        color_flag: Whether to read the image as 'color', 'grey', or one of the cv2.IMREAD flags.

    Raises:
        AssertionError: If `inputs` and `outputs` have mismatched lengths, or the `color_flag` is unacceptable.
    """
    def __init__(self,
                 inputs: Union[str, Iterable[str], Callable],
                 outputs: Union[str, Iterable[str]],
                 mode: Union[None, str, Iterable[str]] = None,
                 parent_path: str = "",
                 color_flag: Union[str, int] = cv2.IMREAD_COLOR):
        super().__init__(inputs=inputs, outputs=outputs, mode=mode)
        if isinstance(self.inputs, List) and isinstance(self.outputs, List):
            assert len(self.inputs) == len(self.outputs), "Input and Output lengths must match"
        self.parent_path = parent_path
        assert isinstance(color_flag, int) or color_flag in {'color', 'gray', 'grey'}, \
            f"Unacceptable color_flag value: {color_flag}"
        self.color_flag = color_flag
        if self.color_flag == "color":
            self.color_flag = cv2.IMREAD_COLOR
        elif self.color_flag in {"gray", "grey"}:
            self.color_flag = cv2.IMREAD_GRAYSCALE
        self.in_list, self.out_list = True, True

    def forward(self, data: List[str], state: Dict[str, Any]) -> List[np.ndarray]:
        results = []
        for path in data:
            path = os.path.normpath(os.path.join(self.parent_path, path))
            img = cv2.imread(path, self.color_flag)
            if self.color_flag in {cv2.IMREAD_COLOR, cv2.IMREAD_REDUCED_COLOR_2, cv2.IMREAD_REDUCED_COLOR_4,
                                   cv2.IMREAD_REDUCED_COLOR_8}:
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            if not isinstance(img, np.ndarray):
                raise ValueError('cv2 did not read correctly for file "{}"'.format(path))
            if self.color_flag in {cv2.IMREAD_GRAYSCALE, cv2.IMREAD_REDUCED_GRAYSCALE_2, cv2.IMREAD_REDUCED_GRAYSCALE_4,
                                   cv2.IMREAD_REDUCED_GRAYSCALE_8}:
                img = np.expand_dims(img, -1)
            results.append(img)
        return results