Commit dcaf6888 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Merge branch 'amir' into 'master'

Add support for external annotations in replaymobile

See merge request !64
parents 207cbed8 88190b4d
Pipeline #19452 passed with stages
in 40 minutes and 11 seconds
......@@ -6,6 +6,7 @@ from bob.bio.video import FrameSelector
from bob.pad.base.database import PadDatabase
from bob.pad.face.database import VideoPadFile
from bob.pad.face.utils import frames, number_of_frames
from bob.db.base.annotations import read_annotation_file
import numpy
from bob.extension import rc
# documentation imports
......@@ -88,18 +89,20 @@ class ReplayMobilePadDatabase(PadDatabase):
def __init__(
self,
# grandtest is the default protocol for this database
protocol='grandtest',
original_directory=rc['bob.db.replaymobile.directory'],
original_extension='.mov',
annotation_directory=None,
annotation_extension='.json',
annotation_type='json',
**kwargs):
"""
Parameters
----------
protocol : str or None
The name of the protocol that defines the default experimental
setup for this database.
setup for this database. ``grandtest`` is the default protocol for
this database.
original_directory : str
The directory where the original data of the database are stored.
......@@ -107,7 +110,14 @@ class ReplayMobilePadDatabase(PadDatabase):
original_extension : str
The file name extension of the original data.
kwargs
annotation_directory : str
If provided, the annotations will be read from this directory
instead of the default annotations that are provided.
annotation_extension : str
The extension of annotations when annotation_extension is provided.
**kwargs
The arguments of the :py:class:`bob.bio.base.database.BioDatabase`
base class constructor.
"""
......@@ -131,6 +141,9 @@ class ReplayMobilePadDatabase(PadDatabase):
protocol=protocol,
original_directory=original_directory,
original_extension=original_extension,
annotation_directory=annotation_directory,
annotation_extension=annotation_extension,
annotation_type=annotation_type,
**kwargs)
@property
......@@ -200,6 +213,9 @@ class ReplayMobilePadDatabase(PadDatabase):
for each video frame. The annotations are returned as dictionary of
dictionaries.
If ``self.annotation_directory`` is not None, it will read the
annotations from there.
Parameters
----------
f : :any:`ReplayMobilePadFile`
......@@ -216,6 +232,14 @@ class ReplayMobilePadDatabase(PadDatabase):
in frame N.
"""
if self.annotation_directory is not None:
# return the external annotations
annotations = read_annotation_file(
f.make_path(self.annotation_directory,
self.annotation_extension),
self.annotation_type)
return annotations
# numpy array containing the face bounding box data for each video
# frame, returned data format described in the f.bbx() method of the
# low level interface
......
......@@ -21,9 +21,13 @@ import bob.ip.color
import bob.ip.facedetect
import logging
#==============================================================================
# Main body:
logger = logging.getLogger(__name__)
class FrameDifference(Preprocessor):
"""
......@@ -414,6 +418,10 @@ class FrameDifference(Preprocessor):
selected_frames, selected_annotations = self.check_face_size(
frames, annotations, self.min_face_size)
if not len(selected_annotations):
logger.warn("None of the annotations are valid.")
return None
diff = self.comp_face_bg_diff(
frames=selected_frames,
annotations=selected_annotations,
......
......@@ -4,9 +4,9 @@ from bob.io.video import reader
from bob.ip.base import scale, block, block_output_shape
from bob.ip.color import rgb_to_yuv, rgb_to_hsv
from bob.ip.facedetect import bounding_box_from_annotation
from collections import OrderedDict
from functools import partial
import numpy
import six
def frames(path):
......@@ -108,14 +108,12 @@ def yield_faces(database, padfile, cropper, normalizer=None):
if annotations is None:
raise ValueError("No annotations were returned.")
if normalizer is None:
annotations_gen = annotations.items()
else:
annotations_gen = normalizer(annotations)
if normalizer is not None:
annotations = OrderedDict(normalizer(annotations))
# normalize annotations and crop faces
for _, annot in annotations_gen:
frame = six.next(frames_gen)
for frame_id, frame in enumerate(frames_gen):
annot = annotations.get(str(frame_id), None)
if annot is None:
continue
face = cropper(frame, annotations=annot)
......
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