Skip to content
Snippets Groups Projects

Base preprocessor accept **kwargs

Merged Amir MOHAMMADI requested to merge base into master
1 file
+ 21
17
Compare changes
  • Side-by-side
  • Inline
@@ -4,8 +4,10 @@ import bob.ip.color
from bob.bio.base.preprocessor import Preprocessor
class Base (Preprocessor):
"""Performs color space adaptations and data type corrections for the given image.
"""Performs color space adaptations and data type corrections for the given
image.
**Parameters:**
@@ -16,17 +18,17 @@ class Base (Preprocessor):
The specific color channel, which should be extracted from the image.
"""
def __init__(self, dtype = None, color_channel = 'gray'):
Preprocessor.__init__(self, dtype=str(dtype), color_channel=color_channel)
def __init__(self, dtype=None, color_channel='gray', **kwargs):
Preprocessor.__init__(self, dtype=str(dtype),
color_channel=color_channel, **kwargs)
self.channel = color_channel
self.dtype = dtype
def color_channel(self, image):
"""color_channel(image) -> channel
Returns the channel of the given image, which was selected in the constructor.
Currently, gray, red, green and blue channels are supported.
Returns the channel of the given image, which was selected in the
constructor. Currently, gray, red, green and blue channels are supported.
**Parameters:**
@@ -42,7 +44,8 @@ class Base (Preprocessor):
if self.channel == 'rgb':
return bob.ip.color.gray_to_rgb(image)
if self.channel != 'gray':
raise ValueError("There is no rule to extract a " + channel + " image from a gray level image!")
raise ValueError("There is no rule to extract a " +
self.channel + " image from a gray level image!")
return image
if self.channel == 'rgb':
@@ -50,20 +53,22 @@ class Base (Preprocessor):
if self.channel == 'gray':
return bob.ip.color.rgb_to_gray(image)
if self.channel == 'red':
return image[0,:,:]
return image[0, :, :]
if self.channel == 'green':
return image[1,:,:]
return image[1, :, :]
if self.channel == 'blue':
return image[2,:,:]
raise ValueError("The image channel '%s' is not known or not yet implemented", self.channel)
return image[2, :, :]
raise ValueError(
"The image channel '%s' is not known or not yet implemented",
self.channel)
def data_type(self, image):
"""data_type(image) -> image
Converts the given image into the data type specified in the constructor of this class.
If no data type was specified, or the ``image`` is ``None``, no conversion is performed.
Converts the given image into the data type specified in the constructor of
this class. If no data type was specified, or the ``image`` is ``None``, no
conversion is performed.
**Parameters:**
@@ -79,8 +84,7 @@ class Base (Preprocessor):
image = image.astype(self.dtype)
return image
def __call__(self, image, annotations = None):
def __call__(self, image, annotations=None):
"""__call__(image, annotations = None) -> image
Extracts the desired color channel and converts to the desired data type.
@@ -98,7 +102,7 @@ class Base (Preprocessor):
image : 2D :py:class:`numpy.ndarray`
The image converted converted to the desired color channel and type.
"""
assert isinstance(image, numpy.ndarray) and image.ndim in (2,3)
assert isinstance(image, numpy.ndarray) and image.ndim in (2, 3)
# convert to grayscale
image = self.color_channel(image)
return self.data_type(image)
Loading