def _encode_image(self, np_image): """Returns np_image encoded as jpeg or png.""" if not self._runner: self._runner = utils.TFGraphRunner() if np_image.dtype != self._dtype.as_numpy_dtype: raise ValueError('Image dtype should be %s. Detected: %s.' % (self._dtype.as_numpy_dtype, np_image.dtype)) utils.assert_shape_match(np_image.shape, self._shape) return self._runner.run(ENCODE_FN[self._encoding_format], np_image)
def _encode_image(self, np_image): """Returns np_image encoded as jpeg or png.""" if not self._runner: self._runner = utils.TFGraphRunner() if np_image.dtype != self._dtype.as_numpy_dtype: raise ValueError('Image dtype should be %s. Detected: %s.' % ( self._dtype.as_numpy_dtype, np_image.dtype)) utils.assert_shape_match(np_image.shape, self._shape) # When encoding isn't defined, default to PNG. # Should we be more strict about explicitly define the encoding (raise # error / warning instead) ? # It has created subtle issues for imagenet_corrupted: images are read as # JPEG images to apply some processing, but final image saved as PNG # (default) rather than JPEG. return self._runner.run(ENCODE_FN[self._encoding_format or 'png'], np_image)
def __init__(self, shape=None): """Construct the connector. Args: shape (tuple): The shape of the image (h, w, c). The image dimensions can be None if the images do not have constant length. Raises: ValueError: If the shape is invalid """ self._shape = shape or (None, None, 1) self._image_encoder = None # Runner to execute the image encoding/decoding self._runner = utils.TFGraphRunner() if len(self._shape) != 3: raise ValueError('Shape {} should be of length 3.'.format( self._shape)) if self._shape[-1] is None: raise ValueError('Shape {} should have a non-None channel.'.format( self._shape))
def _runner(self): # TODO(epot): Should clear the runner once every image has been encoded. # TODO(epot): Better support for multi-shape image (instead of re-building # a new graph every time) return utils.TFGraphRunner()
def _runner(self) -> utils.TFGraphRunner: return utils.TFGraphRunner()