__init__.py 2.09 KB
Newer Older
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
1
def normalize_annotations(annotations, validator, max_age=-1):
2 3 4 5 6 7 8 9 10 11 12
  """Normalizes the annotations of one video sequence. It fills the
  annotations for frames from previous ones if the annotation for the current
  frame is not valid.

  Parameters
  ----------
  annotations : collections.OrderedDict
      A dict of dict where the keys to the first dict are frame indices as
      strings (starting from 0). The inside dicts contain annotations for that
      frame. The dictionary needs to be an ordered dict in order for this to
      work.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
13
  validator : ``callable``
14
      Takes a dict (annotations) and returns True if the annotations are valid.
15 16
      This can be a check based on minimal face size for example: see
      :any:`bob.bio.face.annotator.min_face_size_validator`.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  max_age : :obj:`int`, optional
      An integer indicating for a how many frames a detected face is valid if
      no detection occurs after such frame. A value of -1 == forever

  Yields
  ------
  str
      The index of frame.
  dict
      The corrected annotations of the frame.
  """
  # the annotations for the current frame
  current = None
  age = 0

  for k, annot in annotations.items():
33
    if validator(annot):
34 35 36 37 38 39 40 41
      current = annot
      age = 0
    elif max_age < 0 or age < max_age:
      age += 1
    else:  # no detections and age is larger than maximum allowed
      current = None

    yield k, current
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
42 43


44 45
# these imports should be here to avoid circular dependencies
from .Base import Base
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
46 47 48 49
from .Wrapper import Wrapper
from .FailSafeVideo import FailSafeVideo


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
50 51
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
52 53 54
  """Says object was actually declared here, and not in the import module.
  Fixing sphinx warnings of not being able to find classes, when path is
  shortened.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
55

56 57 58 59
  Parameters
  ----------
  *args
      An iterable of objects to modify
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
60

61 62 63
  Resolves `Sphinx referencing issues
  <https://github.com/sphinx-doc/sphinx/issues/3048>`
  """
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
64

65 66
  for obj in args:
    obj.__module__ = __name__
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
67 68 69


__appropriate__(
70
    Base,
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
71 72 73 74 75
    Wrapper,
    FailSafeVideo,
)

__all__ = [_ for _ in dir() if not _.startswith('_')]