Commit d66176e9 authored by Manuel Günther's avatar Manuel Günther
Browse files

First running version of bob.bio.video

parents
*~
*.swp
*.pyc
bin
eggs
parts
.installed.cfg
.mr.developer.cfg
*.egg-info
src
develop-eggs
sphinx
dist
This diff is collapsed.
include README.rst bootstrap-buildout.py buildout.cfg COPYING version.txt
recursive-include doc *.py *.rst
Example buildout environment
============================
This simple example demonstrates how to wrap Bob-based scripts on buildout
environments. This may be useful for homework assignments, tests or as a way to
distribute code to reproduce your publication. In summary, if you need to give
out code to others, we recommend you do it following this template so your code
can be tested, documented and run in an orderly fashion.
Installation
------------
.. note::
To follow these instructions locally you will need a local copy of this
package. For that, you can use the github tarball API to download the package::
$ wget --no-check-certificate https://github.com/idiap/bob.project.example/tarball/master -O- | tar xz
$ mv idiap-bob.project* bob.project.example
Documentation and Further Information
-------------------------------------
Please refer to the latest Bob user guide, accessing from the `Bob website
<http://idiap.github.com/bob/>`_ for how to create your own packages based on
this example. In particular, the Section entitled `Organize Your Work in
Satellite Packages <http://www.idiap.ch/software/bob/docs/releases/last/sphinx/html/OrganizeYourCode.html>`_
contains details on how to setup, build and roll out your code.
#see http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
__import__('pkg_resources').declare_namespace(__name__)
#see http://peak.telecommunity.com/DevCenter/setuptools#namespace-packages
__import__('pkg_resources').declare_namespace(__name__)
from .utils import *
from . import preprocessor
from . import extractor
from . import algorithm
from . import test
import bob.bio.base
import bob.io.base
from .. import utils
class Algorithm (bob.bio.base.algorithm.Algorithm):
def __init__(self,
algorithm,
frame_selector = utils.FrameSelector(selection_style='all'),
compressed_io = False
):
# load algorithm configuration
if isinstance(algorithm, str):
self.algorithm = bob.bio.base.load_resource(algorithm, "algorithm")
elif isinstance(algorithm, bob.bio.base.algorithm.Algorithm):
self.algorithm = algorithm
else:
raise ValueError("The given algorithm could not be interpreter")
bob.bio.base.algorithm.Algorithm.__init__(
self,
self.algorithm.performs_projection,
self.algorithm.requires_projector_training,
self.algorithm.split_training_features_by_client,
self.algorithm.use_projected_features_for_enrollment,
self.algorithm.requires_enroller_training,
algorithm=algorithm,
frame_selector=frame_selector,
compressed_io=compressed_io
)
self.frame_selector = frame_selector
# if we select the frames during feature extraction, for enrollment we use all files
# otherwise select frames during enrollment (or enroller training)
self.enroll_frame_selector = (lambda i : i) if self.use_projected_features_for_enrollment else frame_selector
self.compressed_io = compressed_io
# PROJECTION
def train_projector(self, data_list, projector_file):
"""Trains the projector using features from selected frames."""
if self.split_training_features_by_client:
training_features = [[frame[1] for frame_container in client_containers for frame in self.frame_selector(frame_container)] for client_containers in data_list]
else:
training_features = [frame[1] for frame_container in data_list for frame in self.frame_selector(frame_container)]
self.algorithm.train_projector(training_features, projector_file)
def load_projector(self, projector_file):
return self.algorithm.load_projector(projector_file)
def project(self, frame_container):
"""Projects each frame and saves them in a frame container."""
fc = utils.FrameContainer()
for index, frame, quality in self.frame_selector(frame_container):
# extract features
projected = self.algorithm.project(frame)
features = projected if isinstance(projected, (list,tuple)) else projected.copy()
# add image to frame container
fc.add(index, features, quality)
return fc
def save_feature(self, frames, projected_file):
if self.compressed_io:
return utils.save_compressed(frame_container, projected_file, self.algorithm.write_feature)
else:
frame_container.save(bob.io.base.HDF5File(projected_file, 'w'), self.algorithm.write_feature)
def read_feature(self, projected_file):
if self.compressed_io:
return utils.load_compressed(projected_file, self.algorithm.read_feature)
else:
return utils.FrameContainer(bob.io.base.HDF5File(projected_file), self.algorithm.read_feature)
# ENROLLMENT
def train_enroller(self, training_frames, enroller_file):
features = [[frame[1] for frame_container in client_frames for frame in self.enroll_frame_selector(frame_container)] for client_frames in training_frames]
self.algorithm.train_enroller(features, enroller_file)
def load_enroller(self, enroller_file):
self.algorithm.load_enroller(enroller_file)
def enroll(self, enroll_frames):
"""Enrolls the model from features of all images of all videos."""
features = [frame[1] for frame_container in enroll_frames for frame in self.enroll_frame_selector(frame_container)]
return self.algorithm.enroll(features)
def save_model(self, model, filename):
"""Saves the model using the algorithm's save function."""
self.algorithm.save_model(model, filename)
# SCORING
def read_model(self, filename):
"""Reads the model using the algorithm's read function."""
return self.algorithm.read_model(filename)
def read_probe(self, filename):
"""Reads the model using the algorithm's read function."""
try:
if self.compressed_io:
return utils.load_compressed(filename, self.algorithm.read_probe)
else:
return utils.FrameContainer(bob.io.base.HDF5File(filename), self.algorithm.read_probe)
except IOError:
return self.algorithm.read_probe(filename)
def score(self, model, probe):
"""Computes the score between the given model and the probe, which is a list of frames."""
if isinstance(probe, utils.FrameContainer):
features = [frame[1] for frame in probe]
return self.algorithm.score_for_multiple_probes(model, features)
else:
return self.algorithm.score(model, probe)
def score_for_multiple_probes(self, model, probes):
"""Computes the score between the given model and the probes, where each probe is a list of frames."""
probe = [frame[1] for frame in probe for probe in probes]
return self.algorithm.score_for_multiple_probes(model, probe)
from .Algorithm import Algorithm
from . import mobio
from . import youtube
#!/usr/bin/env python
import bob.db.mobio
import bob.bio.base
mobio_video_directory = "[YOUR_MOBIO_VIDEO_DIRECTORY]"
database = bob.bio.base.database.DatabaseBobZT(
database = bob.db.mobio.Database(
original_directory = mobio_video_directory,
original_extension = '.mp4',
),
name = "mobio",
protocol = 'male',
models_depend_on_protocol = True,
all_files_options = {'subworld' : 'twothirds-subsampled'},
extractor_training_options = {'subworld' : 'twothirds-subsampled'},
projector_training_options = {'subworld' : 'twothirds-subsampled'},
enroller_training_options = {'subworld' : 'twothirds-subsampled'},
)
#!/usr/bin/env python
import bob.db.youtube
import bob.bio.base
youtube_directory = "[YOUR_YOUTUBE_DIRECTORY]"
database = bob.bio.base.database.DatabaseBobZT(
database = bob.db.youtube.Database(
original_directory = youtube_directory,
),
name = "youtube",
protocol = 'fold1',
models_depend_on_protocol = True,
training_depends_on_protocol = True,
all_files_options = {'subworld' : 'fivefolds'},
extractor_training_options = {'subworld' : 'fivefolds'},
projector_training_options = {'subworld' : 'fivefolds'},
enroller_training_options = {'subworld' : 'fivefolds'},
)
import bob.bio.base
import bob.io.base
import os
from .. import utils
class Extractor (bob.bio.base.extractor.Extractor):
def __init__(self,
extractor,
frame_selector = utils.FrameSelector(selection_style='all'),
compressed_io = False
):
# load extractor configuration
if isinstance(extractor, str):
self.extractor = bob.bio.base.load_resource(extractor, "extractor")
elif isinstance(extractor, bob.bio.base.extractor.Extractor):
self.extractor = extractor
else:
raise ValueError("The given extractor could not be interpreter")
self.frame_selector = frame_selector
self.compressed_io = compressed_io
# register extractor's details
bob.bio.base.extractor.Extractor.__init__(self, requires_training=self.extractor.requires_training, split_training_data_by_client=self.extractor.split_training_data_by_client, extractor=extractor, frame_selector=frame_selector, compressed_io=compressed_io)
def __call__(self, frame_container, annotations=None):
"""Extracts the frames from the video and returns a frame container."""
# now, go through the frames and extract the features
fc = utils.FrameContainer()
for index, frame, quality in self.frame_selector(frame_container):
# extract features
extracted = self.extractor(frame)
# add features to new frame container
fc.add(index, extracted, quality)
return fc
def read_feature(self, filename):
if self.compressed_io:
return utils.load_compressed(filename, self.extractor.read_feature)
else:
return utils.FrameContainer(bob.io.base.HDF5File(filename), self.extractor.read_feature)
def write_feature(self, frame_container, filename):
if self.compressed_io:
return utils.save_compressed(frame_container, filename, self.extractor.write_feature)
else:
frame_container.save(bob.io.base.HDF5File(filename, 'w'), self.extractor.save_feature)
def train(self, data_list, extractor_file):
"""Trains the feature extractor with the image data of the given frames."""
if self.split_training_data_by_client:
features = [[frame[1] for frame_container in client_containers for frame in self.frame_selector(frame_container)] for client_containers in data_list]
else:
features = [frame[1] for frame_container in data_list for frame in self.frame_selector(frame_container)]
self.extractor.train(features, extractor_file)
def load(self, extractor_file):
self.extractor.load(extractor_file)
from .Extractor import Extractor
import bob.bio.base
import numpy
import glob
import os
import bob.io.base
from .. import utils
class Preprocessor (bob.bio.base.preprocessor.Preprocessor):
def __init__(self,
preprocessor = 'landmark-detect',
frame_selector = utils.FrameSelector(),
quality_function = None,
compressed_io = False
):
# load preprocessor configuration
if isinstance(preprocessor, str):
self.preprocessor = bob.bio.base.load_resource(preprocessor, "preprocessor")
elif isinstance(preprocessor, bob.bio.base.preprocessor.Preprocessor):
self.preprocessor = preprocessor
else:
raise ValueError("The given algorithm could not be interpreter")
bob.bio.base.preprocessor.Preprocessor.__init__(self, preprocessor=preprocessor, frame_selector=frame_selector, compressed_io=compressed_io)
self.frame_selector = frame_selector
self.quality_function = quality_function
self.compressed_io = compressed_io
def __call__(self, frame_container, annotations=None):
"""Extracts the frames from the video and returns a frame container.
Faces are extracted for all frames in the given frame container, using the ``preprocessor`` specified in the contructor.
If given, the annotations need to be in a dictionary.
The key is either the frame number (for video data) or the image name (for image list data).
The value is another dictionary, building the relation between keypoint names and their location, e.g., {'leye' : (le_y, le_x), 'reye' : (re_y, re_x)}
The annotations for the according frames, if present, are passed to the preprocessor.
"""
annots = None
fc = utils.FrameContainer()
for index, frame, _ in frame_container:
# if annotations are given, we take them
if annotations is not None: annots = annotations[index]
# preprocess image (by default: detect a face)
preprocessed = self.preprocessor(frame, annots)
if preprocessed is not None:
# compute the quality of the detection
if self.quality_function is not None:
quality = self.quality_function(preprocessed)
elif hasattr(self.preprocessor, 'quality'):
quality = self.preprocessor.quality
else:
quality = None
# add image to frame container
if hasattr(preprocessed, 'copy'):
preprocessed = preprocessed.copy()
fc.add(index, preprocessed, quality)
return fc
def read_original_data(self, data):
return self.frame_selector(data)
def read_data(self, filename):
if self.compressed_io:
return utils.load_compressed(filename, self.preprocessor.read_data)
else:
return utils.FrameContainer(bob.io.base.HDF5File(filename), self.preprocessor.read_data)
def save_data(self, frame_container, filename):
if self.compressed_io:
return utils.save_compressed(frame_container, filename, self.preprocessor.write_data)
else:
frame_container.save(bob.io.base.HDF5File(filename, 'w'), self.preprocessor.write_data)
from .Preprocessor import Preprocessor
Supports Markdown
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