Skip to content
Snippets Groups Projects
Commit 2a82f3ef authored by Amir Mohammadi's avatar Amir Mohammadi
Browse files

Base preprocessor accept **kwargs

parent 0c57b664
No related branches found
No related tags found
1 merge request!27Base preprocessor accept **kwargs
Pipeline #
This commit is part of merge request !27. Comments created here will be created in the context of that merge request.
......@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment