Commit 3aee6ec0 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'annot' into 'master'

Add support for external annotations

See merge request !6
parents 3556d566 52e979cb
Pipeline #31972 failed with stages
in 22 minutes and 31 seconds
...@@ -4,6 +4,7 @@ from bob.pad.face.database import VideoPadFile ...@@ -4,6 +4,7 @@ from bob.pad.face.database import VideoPadFile
from bob.bio.video import FrameSelector from bob.bio.video import FrameSelector
from bob.extension import rc from bob.extension import rc
from bob.io.video import reader from bob.io.video import reader
from bob.db.base.annotations import read_annotation_file
from . import OULUNPU_FRAME_SHAPE from . import OULUNPU_FRAME_SHAPE
...@@ -19,8 +20,7 @@ class File(VideoPadFile): ...@@ -19,8 +20,7 @@ class File(VideoPadFile):
:any:`numpy.array` :any:`numpy.array`
A frame of the video. The size is (3, 1920, 1080). A frame of the video. The size is (3, 1920, 1080).
""" """
vfilename = self.make_path( vfilename = self.make_path(directory=self.original_directory, extension=".avi")
directory=self.original_directory, extension='.avi')
return iter(reader(vfilename)) return iter(reader(vfilename))
@property @property
...@@ -32,8 +32,7 @@ class File(VideoPadFile): ...@@ -32,8 +32,7 @@ class File(VideoPadFile):
int int
The number of frames. The number of frames.
""" """
vfilename = self.make_path( vfilename = self.make_path(directory=self.original_directory, extension=".avi")
directory=self.original_directory, extension='.avi')
return reader(vfilename).number_of_frames return reader(vfilename).number_of_frames
@property @property
...@@ -49,7 +48,11 @@ class File(VideoPadFile): ...@@ -49,7 +48,11 @@ class File(VideoPadFile):
@property @property
def annotations(self): def annotations(self):
"""Reads the annotations """Reads the annotations.
If the file object has an attribute of annotation_directory, it will read
annotations from there instead of loading annotations that are shipped with the
database.
Returns Returns
------- -------
...@@ -57,24 +60,35 @@ class File(VideoPadFile): ...@@ -57,24 +60,35 @@ class File(VideoPadFile):
The annotations as a dictionary, e.g.: The annotations as a dictionary, e.g.:
``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}`` ``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}``
""" """
path = self.make_path( if (
directory=self.original_directory, extension='.txt') hasattr(self, "annotation_directory")
and self.annotation_directory is not None
):
path = self.make_path(self.annotation_directory, extension=".json")
return read_annotation_file(path, annotation_type="json")
path = self.make_path(directory=self.original_directory, extension=".txt")
annotations = {} annotations = {}
with open(path) as f: with open(path) as f:
for line in f: for line in f:
line = line.strip() line = line.strip()
if not line: if not line:
continue continue
num_frame, x_eye_right, y_eye_right, x_eye_left, y_eye_left = \ num_frame, x_eye_right, y_eye_right, x_eye_left, y_eye_left = line.split(
line.split(',') ","
)
annotations[num_frame] = { annotations[num_frame] = {
'reye': (int(y_eye_right), int(x_eye_right)), "reye": (int(y_eye_right), int(x_eye_right)),
'leye': (int(y_eye_left), int(x_eye_left)), "leye": (int(y_eye_left), int(x_eye_left)),
} }
return annotations return annotations
def load(self, directory=None, extension='.avi', def load(
frame_selector=FrameSelector(selection_style='all')): self,
directory=None,
extension=".avi",
frame_selector=FrameSelector(selection_style="all"),
):
"""Loads the video file and returns in a """Loads the video file and returns in a
:any:`bob.bio.video.FrameContainer`. :any:`bob.bio.video.FrameContainer`.
...@@ -100,29 +114,70 @@ class File(VideoPadFile): ...@@ -100,29 +114,70 @@ class File(VideoPadFile):
class Database(FileListPadDatabase): class Database(FileListPadDatabase):
"""The database interface for the OULU-NPU dataset.""" """The database interface for the OULU-NPU dataset."""
def __init__(self, original_directory=rc['bob.db.oulunpu.directory'], def __init__(
name='oulunpu', pad_file_class=None, self,
original_extension=".avi", **kwargs): original_directory=rc["bob.db.oulunpu.directory"],
name="oulunpu",
pad_file_class=None,
original_extension=".avi",
annotation_directory=None,
**kwargs
):
"""Summary
Parameters
----------
original_directory : TYPE, optional
Description
name : str, optional
Description
pad_file_class : None, optional
Description
original_extension : str, optional
Description
annotation_directory : str
If provided, the annotations will be read from this directory
instead of the default annotations that are provided.
**kwargs
Description
"""
if pad_file_class is None: if pad_file_class is None:
pad_file_class = File pad_file_class = File
filelists_directory = resource_filename(__name__, 'lists') filelists_directory = resource_filename(__name__, "lists")
super(Database, self).__init__( super(Database, self).__init__(
filelists_directory=filelists_directory, name=name, filelists_directory=filelists_directory,
name=name,
original_directory=original_directory, original_directory=original_directory,
pad_file_class=pad_file_class, pad_file_class=pad_file_class,
original_extension=original_extension, original_extension=original_extension,
annotation_directory=annotation_directory,
training_depends_on_protocol=True, training_depends_on_protocol=True,
**kwargs) **kwargs
)
def objects(self, groups=None, protocol=None, purposes=None,
model_ids=None, classes=None, **kwargs): def objects(
self,
groups=None,
protocol=None,
purposes=None,
model_ids=None,
classes=None,
**kwargs
):
"""Returns the requested samples.""" """Returns the requested samples."""
files = super(Database, self).objects( files = super(Database, self).objects(
groups=groups, protocol=protocol, purposes=purposes, groups=groups,
model_ids=model_ids, classes=classes, **kwargs) protocol=protocol,
purposes=purposes,
model_ids=model_ids,
classes=classes,
**kwargs
)
for f in files: for f in files:
f.original_directory = self.original_directory f.original_directory = self.original_directory
f.original_extension = self.original_extension f.original_extension = self.original_extension
f.annotation_directory = self.annotation_directory
return files return files
def frames(self, padfile): def frames(self, padfile):
......
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