From 5aeab6eea86c4ff1734bd3cde486356dccbb97cc Mon Sep 17 00:00:00 2001
From: Amir MOHAMMADI <amir.mohammadi@idiap.ch>
Date: Thu, 23 Nov 2017 15:43:11 +0100
Subject: [PATCH] Accept frame_selector in load methods

---
 bob/pad/face/database/aggregated_db.py |  2 +-
 bob/pad/face/database/mifs.py          |  8 ++----
 bob/pad/face/database/msu_mfsd.py      | 39 +++-----------------------
 bob/pad/face/database/replay.py        |  9 ++----
 bob/pad/face/database/replay_mobile.py | 38 +++----------------------
 5 files changed, 15 insertions(+), 81 deletions(-)

diff --git a/bob/pad/face/database/aggregated_db.py b/bob/pad/face/database/aggregated_db.py
index fec72e5b..c8072e24 100644
--- a/bob/pad/face/database/aggregated_db.py
+++ b/bob/pad/face/database/aggregated_db.py
@@ -15,7 +15,7 @@ from bob.pad.face.database import msu_mfsd as msu_mfsd_hldi
 
 from bob.bio.video.database.mobio import MobioBioFile
 
-from bob.bio.video import FrameSelector
+from bob.bio.video import FrameSelector, FrameContainer
 
 import numpy as np
 
diff --git a/bob/pad/face/database/mifs.py b/bob/pad/face/database/mifs.py
index beaba9dd..f15ee12b 100644
--- a/bob/pad/face/database/mifs.py
+++ b/bob/pad/face/database/mifs.py
@@ -5,7 +5,8 @@
 
 #==============================================================================
 
-import bob.bio.video  # Used in MIFSPadFile class
+# Used in ReplayMobilePadFile class
+from bob.bio.video import FrameSelector, FrameContainer
 import bob.io.base
 import numpy as np
 
@@ -26,7 +27,7 @@ class MIFSPadFile(PadFile):
                                           file_id)
 
     #==========================================================================
-    def load(self, directory=None, extension=None):
+    def load(self, directory=None, extension=None, frame_selector=FrameSelector(selection_style='all')):
         """
         Overridden version of the load method defined in the ``PadFile``.
 
@@ -49,9 +50,6 @@ class MIFSPadFile(PadFile):
 
         path = self.make_path(
             directory=directory, extension=extension)  # path to the file
-        frame_selector = bob.bio.video.FrameSelector(
-            selection_style='all'
-        )  # this frame_selector will select all frames from the video file
 
         data = bob.io.base.load(path)
         data = np.expand_dims(data, axis=0)  # upgrade to 4D (video)
diff --git a/bob/pad/face/database/msu_mfsd.py b/bob/pad/face/database/msu_mfsd.py
index eb4ed5f7..5432ced1 100644
--- a/bob/pad/face/database/msu_mfsd.py
+++ b/bob/pad/face/database/msu_mfsd.py
@@ -2,7 +2,8 @@
 # -*- coding: utf-8 -*-
 
 #==============================================================================
-import bob.bio.video  # Used in MsuMfsdPadFile class
+# Used in ReplayMobilePadFile class
+from bob.bio.video import FrameSelector, FrameContainer
 
 from bob.pad.base.database import PadFile  # Used in MsuMfsdPadFile class
 
@@ -50,35 +51,7 @@ class MsuMfsdPadFile(PadFile):
             file_id=f.id)
 
     #==========================================================================
-    def convert_arr_to_frame_cont(self, data):
-        """
-        This function converts an input 4D array with frames into FrameContainer,
-        where each frame is an RGB image. The dimensionality of the input array
-        is [N_frames, 3, N_rows, N_cols].
-
-        **Parameters:**
-
-        ``data`` : 4D :py:class:`numpy.ndarray`
-            An input 4D array with frames of the dimensionality:
-            [N_frames, 3, N_rows, N_cols].
-
-        **Returns:**
-
-        ``frames`` : FrameContainer
-            Resulting FrameContainer containing RGB frames.
-        """
-
-        frames = bob.bio.video.FrameContainer(
-        )  # initialize the FrameContainer
-
-        for idx, sample in enumerate(data):
-
-            frames.add(idx, sample)
-
-        return frames
-
-    #==========================================================================
-    def load(self, directory=None, extension=None):
+    def load(self, directory=None, extension=None, frame_selector=FrameSelector(selection_style='all')):
         """
         Overridden version of the load method defined in the ``PadFile``.
 
@@ -105,11 +78,7 @@ class MsuMfsdPadFile(PadFile):
 
         video_data_array = self.f.load(
             directory=directory, extension=extension)
-
-        video_data = self.convert_arr_to_frame_cont(
-            video_data_array)  # the result is now a FrameContainer
-
-        return video_data
+        return frame_selector(video_data_array)
 
 
 #==============================================================================
diff --git a/bob/pad/face/database/replay.py b/bob/pad/face/database/replay.py
index fb1092ca..203136c0 100644
--- a/bob/pad/face/database/replay.py
+++ b/bob/pad/face/database/replay.py
@@ -3,7 +3,8 @@
 
 #==============================================================================
 
-import bob.bio.video  # Used in ReplayPadFile class
+# Used in ReplayMobilePadFile class
+from bob.bio.video import FrameSelector, FrameContainer
 
 from bob.pad.base.database import PadFile  # Used in ReplayPadFile class
 
@@ -48,7 +49,7 @@ class ReplayPadFile(PadFile):
             file_id=f.id)
 
     #==========================================================================
-    def load(self, directory=None, extension='.mov'):
+    def load(self, directory=None, extension='.mov', frame_selector=FrameSelector(selection_style='all')):
         """
         Overridden version of the load method defined in the ``PadFile``.
 
@@ -70,10 +71,6 @@ class ReplayPadFile(PadFile):
         path = self.f.make_path(
             directory=directory, extension=extension)  # path to the video file
 
-        frame_selector = bob.bio.video.FrameSelector(
-            selection_style='all'
-        )  # this frame_selector will select all frames from the video file
-
         video_data = frame_selector(path)  # video data
 
         return video_data  # video data
diff --git a/bob/pad/face/database/replay_mobile.py b/bob/pad/face/database/replay_mobile.py
index 862d4555..843dee92 100644
--- a/bob/pad/face/database/replay_mobile.py
+++ b/bob/pad/face/database/replay_mobile.py
@@ -2,7 +2,8 @@
 # -*- coding: utf-8 -*-
 
 #==============================================================================
-import bob.bio.video  # Used in ReplayMobilePadFile class
+# Used in ReplayMobilePadFile class
+from bob.bio.video import FrameSelector, FrameContainer
 
 from bob.pad.base.database import PadFile  # Used in ReplayMobilePadFile class
 
@@ -46,35 +47,7 @@ class ReplayMobilePadFile(PadFile):
             file_id=f.id)
 
     #==========================================================================
-    def convert_arr_to_frame_cont(self, data):
-        """
-        This function converts an input 4D array with frames into FrameContainer,
-        where each frame is an RGB image. The dimensionality of the input array
-        is [N_frames, 3, N_rows, N_cols].
-
-        **Parameters:**
-
-        ``data`` : 4D :py:class:`numpy.ndarray`
-            An input 4D array with frames of the dimensionality:
-            [N_frames, 3, N_rows, N_cols].
-
-        **Returns:**
-
-        ``frames`` : FrameContainer
-            Resulting FrameContainer containing RGB frames.
-        """
-
-        frames = bob.bio.video.FrameContainer(
-        )  # initialize the FrameContainer
-
-        for idx, sample in enumerate(data):
-
-            frames.add(idx, sample)
-
-        return frames
-
-    #==========================================================================
-    def load(self, directory=None, extension='.mov'):
+    def load(self, directory=None, extension='.mov', frame_selector=FrameSelector(selection_style='all')):
         """
         Overridden version of the load method defined in the ``PadFile``.
 
@@ -96,10 +69,7 @@ class ReplayMobilePadFile(PadFile):
         video_data_array = self.f.load(
             directory=directory, extension=extension)
 
-        video_data = self.convert_arr_to_frame_cont(
-            video_data_array)  # the result is now a FrameContainer
-
-        return video_data
+        return frame_selector(video_data_array)
 
 
 #==============================================================================
-- 
GitLab