[refactoring2016] Moved the load functions to the to the high level implementations

parent a02fcfdb
Pipeline #3695 passed with stages
in 173 minutes and 55 seconds
...@@ -2,7 +2,7 @@ from .utils import * ...@@ -2,7 +2,7 @@ from .utils import *
from . import preprocessor from . import preprocessor
from . import extractor from . import extractor
from . import algorithm from . import algorithm
from . import database
from . import test from . import test
def get_config(): def get_config():
......
from .database import VideoBioFile
from .mobio import MobioBioDatabase
from .youtube import YoutubeBioDatabase
# gets sphinx autodoc done right - don't remove it
__all__ = [_ for _ in dir() if not _.startswith('_')]
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
""" """
from bob.bio.base.database.file import BioFile from bob.bio.base.database.file import BioFile
from bob.bio.video.utils.FrameSelector import FrameSelector
class VideoBioFile(BioFile): class VideoBioFile(BioFile):
...@@ -20,4 +21,8 @@ class VideoBioFile(BioFile): ...@@ -20,4 +21,8 @@ class VideoBioFile(BioFile):
self.__f = f self.__f = f
def load(self, directory=None, extension='.avi'):
return FrameSelector()(self.make_path(directory, extension))
...@@ -12,6 +12,21 @@ ...@@ -12,6 +12,21 @@
from .database import VideoBioFile from .database import VideoBioFile
from bob.bio.base.database import ZTBioDatabase, BioFile from bob.bio.base.database import ZTBioDatabase, BioFile
from bob.bio.video.utils import FrameContainer
import os
import bob.io.base
class YoutubeBioFile(VideoBioFile):
def load(self, directory=None, extension='.jpg'):
files = os.listdir(self.make_path(directory, ''))
fc = FrameContainer()
for f in files:
if extension == os.path.splitext(f)[1]:
file_name = os.path.join(self.make_path(directory, ''), f)
fc.add(os.path.basename(file_name), bob.io.base.load(file_name))
return fc
class YoutubeBioDatabase(ZTBioDatabase): class YoutubeBioDatabase(ZTBioDatabase):
...@@ -38,12 +53,12 @@ class YoutubeBioDatabase(ZTBioDatabase): ...@@ -38,12 +53,12 @@ class YoutubeBioDatabase(ZTBioDatabase):
def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs): def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs):
retval = self.__db.objects(groups=groups, protocol=protocol, purposes=purposes, model_ids=model_ids, **kwargs) retval = self.__db.objects(groups=groups, protocol=protocol, purposes=purposes, model_ids=model_ids, **kwargs)
return [VideoBioFile(client_id=f.client_id, path=f.path, file_id=f.id) for f in retval] return [YoutubeBioFile(BioFile(client_id=f.client_id, path=f.path, file_id=f.id)) for f in retval]
def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs): def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs):
retval = self.__db.tobjects(groups=groups, protocol=protocol, model_ids=model_ids, **kwargs) retval = self.__db.tobjects(groups=groups, protocol=protocol, model_ids=model_ids, **kwargs)
return [VideoBioFile(client_id=f.client_id, path=f.path, file_id=f.id) for f in retval] return [YoutubeBioFile(BioFile(client_id=f.client_id, path=f.path, file_id=f.id)) for f in retval]
def zobjects(self, groups=None, protocol=None, **kwargs): def zobjects(self, groups=None, protocol=None, **kwargs):
retval = self.__db.zobjects(groups=groups, protocol=protocol, **kwargs) retval = self.__db.zobjects(groups=groups, protocol=protocol, **kwargs)
return [VideoBioFile(client_id=f.client_id, path=f.path, file_id=f.id) for f in retval] return [YoutubeBioFile(BioFile(client_id=f.client_id, path=f.path, file_id=f.id)) for f in retval]
...@@ -2,15 +2,12 @@ ...@@ -2,15 +2,12 @@
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
import bob.bio.base import bob.bio.base
import numpy
import glob
import os
import bob.io.base import bob.io.base
from .. import utils from .. import utils
class Wrapper (bob.bio.base.preprocessor.Preprocessor):
class Wrapper(bob.bio.base.preprocessor.Preprocessor):
"""Wrapper class to run image preprocessing algorithms on video data. """Wrapper class to run image preprocessing algorithms on video data.
This class provides functionality to read original video data from several databases. This class provides functionality to read original video data from several databases.
...@@ -47,10 +44,11 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor): ...@@ -47,10 +44,11 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor):
""" """
def __init__(self, def __init__(self,
preprocessor = 'landmark-detect', preprocessor='landmark-detect',
frame_selector = utils.FrameSelector(), frame_selector=utils.FrameSelector(),
quality_function = None, quality_function=None,
compressed_io = False compressed_io=False,
read_original_data=lambda biofile, directory, extension: biofile.load(directory, extension)
): ):
# load preprocessor configuration # load preprocessor configuration
...@@ -65,7 +63,8 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor): ...@@ -65,7 +63,8 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor):
self, self,
preprocessor=preprocessor, preprocessor=preprocessor,
frame_selector=frame_selector, frame_selector=frame_selector,
compressed_io=compressed_io compressed_io=compressed_io,
read_original_data=read_original_data
) )
self.frame_selector = frame_selector self.frame_selector = frame_selector
...@@ -76,8 +75,7 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor): ...@@ -76,8 +75,7 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor):
"""Checks if the given video is in the desired format.""" """Checks if the given video is in the desired format."""
assert isinstance(frames, utils.FrameContainer) assert isinstance(frames, utils.FrameContainer)
def __call__(self, frames, annotations=None):
def __call__(self, frames, annotations = None):
"""__call__(frames, annotations = None) -> preprocessed """__call__(frames, annotations = None) -> preprocessed
Preprocesses the given frames using the desired ``preprocessor``. Preprocesses the given frames using the desired ``preprocessor``.
...@@ -130,29 +128,6 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor): ...@@ -130,29 +128,6 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor):
return fc return fc
def read_original_data(self, data):
"""read_original_data(data) -> frames
Reads the original data from file and selects some frames using the desired ``frame_selector``.
Currently, two types of data is supported:
1. video data, which is stored in a 3D or 4D :py:class:`numpy.ndarray`, which will be read using :py:func:`bob.io.base.load`
2. image lists, which is given as a list of strings of image file names. Each image will be read with :py:func:`bob.io.base.load`
**Parameters:**
data : 3D or 4D :py:class:`numpy.ndarray`, or [str]
The original data to read.
**Returns:**
frames : :py:class:`bob.bio.video.FrameContainer`
The selected frames, stored in a frame container.
"""
return self.frame_selector(data)
def read_data(self, filename): def read_data(self, filename):
"""read_data(filename) -> frames """read_data(filename) -> frames
...@@ -174,7 +149,6 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor): ...@@ -174,7 +149,6 @@ class Wrapper (bob.bio.base.preprocessor.Preprocessor):
else: else:
return utils.FrameContainer(bob.io.base.HDF5File(filename), self.preprocessor.read_data) return utils.FrameContainer(bob.io.base.HDF5File(filename), self.preprocessor.read_data)
def write_data(self, frames, filename): def write_data(self, frames, filename):
"""Writes the preprocessed data to file. """Writes the preprocessed data to file.
......
from bob.bio.base.database import ZTBioDatabase from bob.bio.base.database import ZTBioDatabase
from bob.bio.base.test.utils import atnt_database_directory from bob.bio.base.test.utils import atnt_database_directory
from bob.bio.base.database.file import BioFile
from bob.bio.video.database import VideoBioFile
from bob.bio.video.utils import FrameContainer
import bob.io.base
import os
class DummyBioFile(VideoBioFile):
def load(self, directory=None, extension='.avi'):
file_name = self.make_path(directory, extension)
fc = FrameContainer()
fc.add(os.path.basename(file_name), bob.io.base.load(file_name))
return fc
class DummyDatabase(ZTBioDatabase): class DummyDatabase(ZTBioDatabase):
...@@ -17,11 +30,14 @@ class DummyDatabase(ZTBioDatabase): ...@@ -17,11 +30,14 @@ class DummyDatabase(ZTBioDatabase):
import bob.db.atnt import bob.db.atnt
self.__db = bob.db.atnt.Database() self.__db = bob.db.atnt.Database()
def _make_bio(self, files):
return [DummyBioFile(BioFile(client_id=f.client_id, path=f.path, file_id=f.id)) for f in files]
def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs): def model_ids_with_protocol(self, groups=None, protocol=None, **kwargs):
return self.__db.model_ids(groups, protocol) return self.__db.model_ids(groups, protocol)
def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs): def objects(self, groups=None, protocol=None, purposes=None, model_ids=None, **kwargs):
return self.__db.objects(model_ids, groups, purposes, protocol, **kwargs) return self._make_bio(self.__db.objects(model_ids, groups, purposes, protocol, **kwargs))
def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs): def tobjects(self, groups=None, protocol=None, model_ids=None, **kwargs):
return [] return []
...@@ -39,8 +55,9 @@ class DummyDatabase(ZTBioDatabase): ...@@ -39,8 +55,9 @@ class DummyDatabase(ZTBioDatabase):
return self.probe_files(None, group) return self.probe_files(None, group)
# override all_files to return a one-element lists of files # override all_files to return a one-element lists of files
def all_files(self, groups): #def all_files(self, groups):
return [[n] for n in super(DummyDatabase, self).all_files(groups)] #return [[n] for n in super(DummyDatabase, self).all_files(groups)]
#return [[n] for n in super(DummyDatabase, self).all_files(groups)]
def file_names(self, files, directory, extension): def file_names(self, files, directory, extension):
if isinstance(files[0], list): if isinstance(files[0], list):
......
...@@ -9,42 +9,54 @@ import bob.io.video ...@@ -9,42 +9,54 @@ import bob.io.video
import bob.bio.base import bob.bio.base
import bob.bio.video import bob.bio.video
import bob.db.base import bob.db.base
import pkg_resources import pkg_resources
from bob.bio.video.test.dummy.database import DummyBioFile
regenerate_refs = False regenerate_refs = False
def test_annotations(): def test_annotations():
original_path = pkg_resources.resource_filename("bob.bio.face.test", "")
image_files = DummyBioFile(bob.bio.base.database.BioFile(client_id=1, file_id=1, path="data/testimage"))
# use annotations to grep # use annotations to grep
image_files = [pkg_resources.resource_filename("bob.bio.face.test", "data/testimage.jpg")] annotations = {os.path.basename(image_files.make_path(original_path, ".jpg")): bob.db.base.read_annotation_file(
annotations = {os.path.basename(image_files[0]) : bob.db.base.read_annotation_file(pkg_resources.resource_filename("bob.bio.face.test", "data/testimage.pos"), 'named')} pkg_resources.resource_filename("bob.bio.face.test", "data/testimage.pos"), 'named')}
# video preprocessor using a face crop preprocessor # video preprocessor using a face crop preprocessor
frame_selector = bob.bio.video.FrameSelector(selection_style="all") frame_selector = bob.bio.video.FrameSelector(selection_style="all")
preprocessor = bob.bio.video.preprocessor.Wrapper('face-crop-eyes', frame_selector, compressed_io=False) preprocessor = bob.bio.video.preprocessor.Wrapper('face-crop-eyes', frame_selector, compressed_io=False)
# read original data # read original data
original = preprocessor.read_original_data(image_files) original = preprocessor.read_original_data(image_files, original_path, ".jpg")
assert isinstance(original, bob.bio.video.FrameContainer) assert isinstance(original, bob.bio.video.FrameContainer)
assert len(original) == 1 assert len(original) == 1
assert original[0][0] == os.path.basename(image_files[0]) assert original[0][0] == os.path.basename(image_files.make_path(original_path, ".jpg"))
# preprocess data including annotations # preprocess data including annotations
preprocessed = preprocessor(original, annotations) preprocessed = preprocessor(original, annotations)
assert isinstance(preprocessed, bob.bio.video.FrameContainer) assert isinstance(preprocessed, bob.bio.video.FrameContainer)
assert len(preprocessed) == 1 assert len(preprocessed) == 1
assert preprocessed[0][0] == os.path.basename(image_files[0]) assert preprocessed[0][0] == os.path.basename(image_files.make_path(original_path, ".jpg"))
assert preprocessed[0][2] is None assert preprocessed[0][2] is None
assert numpy.allclose(preprocessed[0][1], bob.io.base.load(pkg_resources.resource_filename("bob.bio.face.test", "data/cropped.hdf5"))) assert numpy.allclose(preprocessed[0][1],
bob.io.base.load(pkg_resources.resource_filename("bob.bio.face.test", "data/cropped.hdf5")))
def test_detect(): def test_detect():
def read_original_data(video_object, original_path, original_extension):
return frame_selector(video_object.make_path(original_path, original_extension))
# load test video # load test video
video_file = pkg_resources.resource_filename("bob.bio.video.test", "data/testvideo.avi") original_path = pkg_resources.resource_filename("bob.bio.video.test", "")
video_object = bob.bio.video.database.VideoBioFile(bob.bio.base.database.BioFile(
client_id=1, file_id=1, path="data/testvideo"))
frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread") frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread")
preprocessor = bob.bio.video.preprocessor.Wrapper('face-detect', frame_selector, compressed_io=False,
read_original_data=read_original_data)
preprocessor = bob.bio.video.preprocessor.Wrapper('face-detect', frame_selector, compressed_io=False) video = preprocessor.read_original_data(video_object, original_path,".avi")
video = preprocessor.read_original_data(video_file)
assert isinstance(video, bob.bio.video.FrameContainer) assert isinstance(video, bob.bio.video.FrameContainer)
preprocessed_video = preprocessor(video) preprocessed_video = preprocessor(video)
...@@ -60,11 +72,17 @@ def test_detect(): ...@@ -60,11 +72,17 @@ def test_detect():
def test_flandmark(): def test_flandmark():
video_file = pkg_resources.resource_filename("bob.bio.video.test", "data/testvideo.avi") def read_original_data(video_object, original_path, original_extension):
return frame_selector(video_object.make_path(original_path, original_extension))
original_path = pkg_resources.resource_filename("bob.bio.video.test", "")
video_object = bob.bio.video.database.VideoBioFile(bob.bio.base.database.BioFile(
client_id=1, file_id=1, path="data/testvideo"))
frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread") frame_selector = bob.bio.video.FrameSelector(max_number_of_frames=3, selection_style="spread")
preprocessor = bob.bio.video.preprocessor.Wrapper('landmark-detect', frame_selector, compressed_io=False) preprocessor = bob.bio.video.preprocessor.Wrapper('landmark-detect', frame_selector, compressed_io=False,
video = preprocessor.read_original_data(video_file) read_original_data=read_original_data)
video = preprocessor.read_original_data(video_object, original_path, ".avi")
assert isinstance(video, bob.bio.video.FrameContainer) assert isinstance(video, bob.bio.video.FrameContainer)
preprocessed_video = preprocessor(video) preprocessed_video = preprocessor(video)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment