Skip to content
Snippets Groups Projects
Commit ca1d34b8 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Merge branch 'framediff' into 'master'

Changes to the FrameDifference preprocessor

See merge request !63
parents 90ad3d5a 43c12bdc
No related branches found
No related tags found
1 merge request!63Changes to the FrameDifference preprocessor
Pipeline #
...@@ -22,12 +22,10 @@ this resource. ...@@ -22,12 +22,10 @@ this resource.
from ..preprocessor import FrameDifference from ..preprocessor import FrameDifference
NUMBER_OF_FRAMES = None # process all frames NUMBER_OF_FRAMES = None # process all frames
CHECK_FACE_SIZE_FLAG = True # Check size of the face
MIN_FACE_SIZE = 50 # Minimal size of the face to consider MIN_FACE_SIZE = 50 # Minimal size of the face to consider
preprocessor = FrameDifference( preprocessor = FrameDifference(
number_of_frames=NUMBER_OF_FRAMES, number_of_frames=NUMBER_OF_FRAMES,
check_face_size_flag=CHECK_FACE_SIZE_FLAG,
min_face_size=MIN_FACE_SIZE) min_face_size=MIN_FACE_SIZE)
""" """
In the preprocessing stage the frame differences are computed for both facial and non-facial/background In the preprocessing stage the frame differences are computed for both facial and non-facial/background
......
...@@ -24,12 +24,10 @@ this resource. ...@@ -24,12 +24,10 @@ this resource.
from ..preprocessor import FrameDifference from ..preprocessor import FrameDifference
NUMBER_OF_FRAMES = None # process all frames NUMBER_OF_FRAMES = None # process all frames
CHECK_FACE_SIZE_FLAG = True # Check size of the face
MIN_FACE_SIZE = 50 # Minimal size of the face to consider MIN_FACE_SIZE = 50 # Minimal size of the face to consider
preprocessor = FrameDifference( preprocessor = FrameDifference(
number_of_frames=NUMBER_OF_FRAMES, number_of_frames=NUMBER_OF_FRAMES,
check_face_size_flag=CHECK_FACE_SIZE_FLAG,
min_face_size=MIN_FACE_SIZE) min_face_size=MIN_FACE_SIZE)
""" """
In the preprocessing stage the frame differences are computed for both facial and non-facial/background In the preprocessing stage the frame differences are computed for both facial and non-facial/background
......
...@@ -19,11 +19,13 @@ import bob.ip.base ...@@ -19,11 +19,13 @@ import bob.ip.base
import bob.ip.color import bob.ip.color
import bob.ip.facedetect
#============================================================================== #==============================================================================
# Main body: # Main body:
class FrameDifference(Preprocessor, object): class FrameDifference(Preprocessor):
""" """
This class is designed to compute frame differences for both facial and This class is designed to compute frame differences for both facial and
background regions. The constraint of minimal size of the face can be background regions. The constraint of minimal size of the face can be
...@@ -39,10 +41,6 @@ class FrameDifference(Preprocessor, object): ...@@ -39,10 +41,6 @@ class FrameDifference(Preprocessor, object):
The number of frames to extract the frame differences from. The number of frames to extract the frame differences from.
If ``None``, all frames of the input video are used. Default: ``None``. If ``None``, all frames of the input video are used. Default: ``None``.
``check_face_size_flag`` : :py:class:`bool`
If True, only return the frames containing faces of the size above the
specified threshold ``min_face_size``. Default: ``False``.
``min_face_size`` : :py:class:`int` ``min_face_size`` : :py:class:`int`
The minimal size of the face in pixels. Only valid when ``check_face_size_flag`` The minimal size of the face in pixels. Only valid when ``check_face_size_flag``
is set to True. Default: 50. is set to True. Default: 50.
...@@ -50,16 +48,15 @@ class FrameDifference(Preprocessor, object): ...@@ -50,16 +48,15 @@ class FrameDifference(Preprocessor, object):
def __init__(self, def __init__(self,
number_of_frames=None, number_of_frames=None,
check_face_size_flag=False, min_face_size=50,
min_face_size=50): **kwargs):
super(FrameDifference, self).__init__( super(FrameDifference, self).__init__(
number_of_frames=number_of_frames, number_of_frames=number_of_frames,
check_face_size_flag=check_face_size_flag, min_face_size=min_face_size,
min_face_size=min_face_size) **kwargs)
self.number_of_frames = number_of_frames self.number_of_frames = number_of_frames
self.check_face_size_flag = check_face_size_flag
self.min_face_size = min_face_size self.min_face_size = min_face_size
#========================================================================== #==========================================================================
...@@ -148,13 +145,17 @@ class FrameDifference(Preprocessor, object): ...@@ -148,13 +145,17 @@ class FrameDifference(Preprocessor, object):
else: else:
y1 = annotations['topleft'][0] - border y1 = annotations['topleft'][0] - border
if y1 < 0: y1 = 0 if y1 < 0:
y1 = 0
x1 = annotations['topleft'][1] - border x1 = annotations['topleft'][1] - border
if x1 < 0: x1 = 0 if x1 < 0:
x1 = 0
y2 = y1 + height + (2 * border) y2 = y1 + height + (2 * border)
if y2 > full_diff.shape[0]: y2 = full_diff.shape[0] if y2 > full_diff.shape[0]:
y2 = full_diff.shape[0]
x2 = x1 + width + (2 * border) x2 = x1 + width + (2 * border)
if x2 > full_diff.shape[1]: x2 = full_diff.shape[1] if x2 > full_diff.shape[1]:
x2 = full_diff.shape[1]
full = full_diff[y1:y2, x1:x2].sum() full = full_diff[y1:y2, x1:x2].sum()
full_size = full_diff[y1:y2, x1:x2].size full_size = full_diff[y1:y2, x1:x2].size
...@@ -167,7 +168,7 @@ class FrameDifference(Preprocessor, object): ...@@ -167,7 +168,7 @@ class FrameDifference(Preprocessor, object):
bg = full - face bg = full - face
normalization = float(full_size - face_diff.size) normalization = float(full_size - face_diff.size)
if normalization < 1: #prevents zero division if normalization < 1: # prevents zero division
bg = 0.0 bg = 0.0
else: else:
bg /= float(full_size - face_diff.size) bg /= float(full_size - face_diff.size)
...@@ -217,8 +218,15 @@ class FrameDifference(Preprocessor, object): ...@@ -217,8 +218,15 @@ class FrameDifference(Preprocessor, object):
for idx in range(0, len(annotations)): # idx - frame index for idx in range(0, len(annotations)): # idx - frame index
frame_annotations = annotations[str( # annotations for particular frame
idx)] # annotations for particular frame frame_annotations = annotations[str(idx)]
# Estimate bottomright and topleft if they are not available:
if 'topleft' not in frame_annotations:
bbx = bob.ip.facedetect.bounding_box_from_annotation(
**frame_annotations)
frame_annotations['topleft'] = bbx.topleft
frame_annotations['bottomright'] = bbx.bottomright
# size of current face # size of current face
face_size = np.min( face_size = np.min(
...@@ -357,7 +365,7 @@ class FrameDifference(Preprocessor, object): ...@@ -357,7 +365,7 @@ class FrameDifference(Preprocessor, object):
cleaned_annotations = {} cleaned_annotations = {}
for idx, valid_frame_num in enumerate(valid_frames): for idx, valid_frame_num in enumerate(valid_frames):
## valid_frame_num - is the number of the original frame having annotations # valid_frame_num - is the number of the original frame having annotations
cleaned_annotations[str(idx)] = annotations[str( cleaned_annotations[str(idx)] = annotations[str(
valid_frame_num)] # correct the frame numbers valid_frame_num)] # correct the frame numbers
...@@ -375,8 +383,7 @@ class FrameDifference(Preprocessor, object): ...@@ -375,8 +383,7 @@ class FrameDifference(Preprocessor, object):
This method calls the ``comp_face_bg_diff`` function of this class This method calls the ``comp_face_bg_diff`` function of this class
computing the frame differences for both facial and background regions. computing the frame differences for both facial and background regions.
The frame differences are computed for selected frames, which are returned The frame differences are computed for selected frames, which are returned
by ``check_face_size`` function of this class. This ``check_face_size`` is by ``check_face_size`` function of this class.
done only if ``check_face_size_flag = True``.
**Parameters:** **Parameters:**
...@@ -400,14 +407,12 @@ class FrameDifference(Preprocessor, object): ...@@ -400,14 +407,12 @@ class FrameDifference(Preprocessor, object):
if len(frames) != len(annotations): # if some annotations are missing if len(frames) != len(annotations): # if some annotations are missing
## Select only annotated frames: # Select only annotated frames:
frames, annotations = self.select_annotated_frames( frames, annotations = self.select_annotated_frames(
frames, annotations) frames, annotations)
if self.check_face_size_flag: selected_frames, selected_annotations = self.check_face_size(
frames, annotations, self.min_face_size)
selected_frames, selected_annotations = self.check_face_size(
frames, annotations, self.min_face_size)
diff = self.comp_face_bg_diff( diff = self.comp_face_bg_diff(
frames=selected_frames, frames=selected_frames,
......
...@@ -20,14 +20,10 @@ requirements: ...@@ -20,14 +20,10 @@ requirements:
host: host:
- python {{ python }} - python {{ python }}
- setuptools {{ setuptools }} - setuptools {{ setuptools }}
- six {{ six }}
- sphinx {{ sphinx }}
- numpy {{ numpy }}
- bob.extension - bob.extension
- bob.bio.base - bob.bio.base
- bob.io.base - bob.io.base
- bob.ip.base - bob.ip.base
- scikit-learn
- bob.pad.base - bob.pad.base
- bob.bio.face - bob.bio.face
- bob.bio.video - bob.bio.video
...@@ -41,7 +37,6 @@ requirements: ...@@ -41,7 +37,6 @@ requirements:
- python - python
- setuptools - setuptools
- six - six
- sphinx
- numpy - numpy
- scikit-learn - scikit-learn
......
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