Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.bio.face
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.bio.face
Commits
38e6503f
Commit
38e6503f
authored
4 years ago
by
Amir MOHAMMADI
Browse files
Options
Downloads
Patches
Plain Diff
break out the color channel change into a function; fixes
#26
parent
3968a435
No related branches found
No related tags found
2 merge requests
!71
Face crop improvements
,
!64
Dask pipelines
Pipeline
#44906
failed
4 years ago
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bob/bio/face/preprocessor/Base.py
+75
-66
75 additions, 66 deletions
bob/bio/face/preprocessor/Base.py
bob/bio/face/preprocessor/utils.py
+1
-18
1 addition, 18 deletions
bob/bio/face/preprocessor/utils.py
with
76 additions
and
84 deletions
bob/bio/face/preprocessor/Base.py
+
75
−
66
View file @
38e6503f
import
numpy
import
bob.io.image
import
bob.ip.color
import
numpy
from
sklearn.base
import
BaseEstimator
from
sklearn.base
import
TransformerMixin
def
change_color_channel
(
image
,
color_channel
):
if
image
.
ndim
==
2
:
if
color_channel
==
"
rgb
"
:
return
bob
.
ip
.
color
.
gray_to_rgb
(
image
)
if
color_channel
!=
"
gray
"
:
raise
ValueError
(
"
There is no rule to extract a
"
+
color_channel
+
"
image from a gray level image!
"
)
return
image
from
sklearn.base
import
TransformerMixin
,
BaseEstimator
if
color_channel
==
"
rgb
"
:
return
image
if
color_channel
==
"
gray
"
:
return
bob
.
ip
.
color
.
rgb_to_gray
(
image
)
if
color_channel
==
"
red
"
:
return
image
[
0
,
:,
:]
if
color_channel
==
"
green
"
:
return
image
[
1
,
:,
:]
if
color_channel
==
"
blue
"
:
return
image
[
2
,
:,
:]
raise
ValueError
(
"
The image channel
'
%s
'
is not known or not yet implemented
"
,
color_channel
)
class
Base
(
TransformerMixin
,
BaseEstimator
):
"""
Performs color space adaptations and data type corrections for the given
image.
image.
**Parameters:**
**Parameters:**
dtype : :py:class:`numpy.dtype` or convertible or ``None``
The data type that the resulting image will have.
dtype : :py:class:`numpy.dtype` or convertible or ``None``
The data type that the resulting image will have.
color_channel : one of ``(
'
gray
'
,
'
red
'
,
'
gren
'
,
'
blue
'
,
'
rgb
'
)``
The specific color channel, which should be extracted from the image.
"""
color_channel : one of ``(
'
gray
'
,
'
red
'
,
'
gren
'
,
'
blue
'
,
'
rgb
'
)``
The specific color channel, which should be extracted from the image.
"""
def
__init__
(
self
,
dtype
=
None
,
color_channel
=
"
gray
"
,
**
kwargs
):
self
.
channel
=
color_channel
self
.
color_
channel
=
color_channel
self
.
dtype
=
dtype
@property
def
channel
(
self
):
return
self
.
color_channel
def
_more_tags
(
self
):
return
{
"
stateless
"
:
True
,
"
requires_fit
"
:
False
}
...
...
@@ -31,85 +63,62 @@ class Base(TransformerMixin, BaseEstimator):
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:**
**Parameters:**
image : 2D or 3D :py:class:`numpy.ndarray`
The image to get the specified channel from.
image : 2D or 3D :py:class:`numpy.ndarray`
The image to get the specified channel from.
**Returns:**
**Returns:**
channel : 2D or 3D :py:class:`numpy.ndarray`
The extracted color channel.
"""
channel : 2D or 3D :py:class:`numpy.ndarray`
The extracted color channel.
"""
if
image
.
ndim
==
2
:
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
"
+
self
.
channel
+
"
image from a gray level image!
"
)
return
image
if
self
.
channel
==
"
rgb
"
:
return
image
if
self
.
channel
==
"
gray
"
:
return
bob
.
ip
.
color
.
rgb_to_gray
(
image
)
if
self
.
channel
==
"
red
"
:
return
image
[
0
,
:,
:]
if
self
.
channel
==
"
green
"
:
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
change_color_channel
(
image
,
self
.
color_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:**
**Parameters:**
image : 2D or 3D :py:class:`numpy.ndarray`
The image to convert.
image : 2D or 3D :py:class:`numpy.ndarray`
The image to convert.
**Returns:**
**Returns:**
image : 2D or 3D :py:class:`numpy.ndarray`
The image converted to the desired data type, if any.
"""
image : 2D or 3D :py:class:`numpy.ndarray`
The image converted to the desired data type, if any.
"""
if
self
.
dtype
is
not
None
and
image
is
not
None
:
image
=
image
.
astype
(
self
.
dtype
)
return
image
def
transform
(
self
,
image
,
annotations
=
None
):
"""
__call__(image, annotations = None) -> image
def
transform
(
self
,
image
s
,
annotations
=
None
):
"""
Extracts the desired color channel and converts to the desired data type.
Extracts the desired color channel and converts to the desired data type.
**Parameters:**
**Parameters:**
image : 2D or 3D :py:class:`numpy.ndarray`
The image to preprocess.
image : 2D or 3D :py:class:`numpy.ndarray`
The image to preprocess
.
annotations : any
Ignored
.
annotations : any
Ignored.
**Returns:**
**Returns:**
image : 2D :py:class:`numpy.ndarray`
The image converted converted to the desired color channel and type.
"""
return
[
self
.
_transform_one_image
(
img
)
for
img
in
images
]
image : 2D :py:class:`numpy.ndarray`
The image converted converted to the desired color channel and type.
"""
def
_transform_one_image
(
self
,
image
):
assert
isinstance
(
image
,
numpy
.
ndarray
)
and
image
.
ndim
in
(
2
,
3
)
# convert to grayscale
image
=
self
.
color_channel
(
image
)
...
...
This diff is collapsed.
Click to expand it.
bob/bio/face/preprocessor/utils.py
+
1
−
18
View file @
38e6503f
import
bob.bio.base
import
six
def
load_cropper
(
face_cropper
):
...
...
@@ -7,25 +6,9 @@ def load_cropper(face_cropper):
if
face_cropper
is
None
:
cropper
=
None
elif
isinstance
(
face_cropper
,
s
ix
.
string_types
):
elif
isinstance
(
face_cropper
,
s
tr
):
cropper
=
bob
.
bio
.
base
.
load_resource
(
face_cropper
,
"
preprocessor
"
)
else
:
cropper
=
face_cropper
return
cropper
def
load_cropper_only
(
face_cropper
):
from
.FaceCrop
import
FaceCrop
if
face_cropper
is
None
:
cropper
=
None
elif
isinstance
(
face_cropper
,
six
.
string_types
):
cropper
=
bob
.
bio
.
base
.
load_resource
(
face_cropper
,
"
preprocessor
"
)
elif
isinstance
(
face_cropper
,
FaceCrop
):
cropper
=
face_cropper
else
:
raise
ValueError
(
"
The given face cropper type is not understood
"
)
assert
cropper
is
None
or
isinstance
(
cropper
,
FaceCrop
)
return
cropper
This diff is collapsed.
Click to expand it.
Laurent COLBOIS
@lcolbois
mentioned in merge request
!75 (merged)
·
4 years ago
mentioned in merge request
!75 (merged)
mentioned in merge request !75
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment