Ejemplo n.º 1
0
    def generate_encoded_samples(self, data_dir, tmp_dir, dataset_split):
        """Generate samples of the encoded frames with possible extra data.

    By default this function just encodes the numpy array returned as "frame"
    from `self.generate_samples` into a PNG image. Override this function to
    get other encodings on disk.

    Args:
      data_dir: final data directory. Typically only used in this method to copy
        over user-supplied vocab files if there are extra fields needing them.
      tmp_dir: temporary directory that you can use for downloading and scratch.
      dataset_split: problem.DatasetSplit, which data split to generate samples
        for (for example, training and evaluation).

    Yields:
      Sample: dict<str feature_name, feature value> which is in disk encoding.

    Raises:
      ValueError: if the frame has a different number of channels than required.
    """
        for features in self.generate_samples(data_dir, tmp_dir,
                                              dataset_split):
            unencoded_frame = features.pop("frame")
            height, width, channels = unencoded_frame.shape
            if channels != self.num_channels:
                raise ValueError(
                    "Generated frame has %d channels while the class "
                    "assumes %d channels." % (channels, self.num_channels))
            if height != self.frame_height:
                raise ValueError(
                    "Generated frame has height %d while the class "
                    "assumes height %d." % (height, self.frame_height))
            if width != self.frame_width:
                raise ValueError(
                    "Generated frame has width %d while the class "
                    "assumes width %d." % (width, self.frame_width))
            encoded_frame = six.next(
                image_utils.encode_images_as_png([unencoded_frame]))
            features["image/encoded"] = [encoded_frame]
            features["image/format"] = ["png"]
            features["image/height"] = [height]
            features["image/width"] = [width]
            yield features
Ejemplo n.º 2
0
def encode_image_to_png(image):
    encoded = six.next(image_utils.encode_images_as_png([image]))
    return encoded