Base.py 2.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import bob.bio.base
from .. import utils


class Base(bob.bio.base.annotator.Annotator):
  """The base class for video annotators.

  Parameters
  ----------
  frame_selector : :any:`bob.bio.video.FrameSelector`
    A frame selector class to define, which frames of the video to use.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
12
  read_original_data : ``callable``
13 14 15 16 17 18 19 20 21 22 23
    A function with the signature of
    ``data = read_original_data(biofile, directory, extension)``
    that will be used to load the data from biofiles. By default the
    ``frame_selector`` is used to load the data.
  """

  def __init__(self, frame_selector=utils.FrameSelector(selection_style='all'),
               read_original_data=None, **kwargs):

    def _read_video_data(biofile, directory, extension):
      """Read video data using the frame_selector of this object"""
24 25
      if hasattr(biofile, 'frames'):
        return biofile.frames
26 27 28 29 30 31 32 33
      return biofile.load(directory, extension, frame_selector)

    if read_original_data is None:
      read_original_data = _read_video_data

    super(Base, self).__init__(read_original_data=read_original_data, **kwargs)

  @staticmethod
34
  def frame_ids_and_frames(frames):
35
    """Takes the frames and yields frame_ids and frames.
36 37 38

    Parameters
    ----------
39
    frames : :any:`bob.bio.video.FrameContainer` or an iterable of arrays
40 41
        The frames of the video file.

42 43 44 45 46 47
    Yields
    ------
    frame_id : str
        A string that represents the frame id.
    frame : :any:`numpy.array`
        The frame of the video file as an array.
48 49
    """
    if isinstance(frames, utils.FrameContainer):
50 51 52 53 54
      for fid, fr, _ in frames:
        yield fid, fr
    else:
      for fid, fr in enumerate(frames):
        yield str(fid), fr
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

  def annotate(self, frames, **kwargs):
    """Annotates videos.

    Parameters
    ----------
    frames : :any:`bob.bio.video.FrameContainer` or :any:`numpy.array`
        The frames of the video file.
    **kwargs
        Extra arguments that annotators may need.

    Returns
    -------
    collections.OrderedDict
        A dictionary where its key is the frame id as a string and its value
        is a dictionary that are the annotations for that frame.


    .. note::

75
        You can use the :any:`Base.frame_ids_and_frames` functions to normalize
76 77 78
        the input in your implementation.
    """
    raise NotImplementedError()