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
from bob.bio.video import FrameSelector
from bob.extension import rc
from bob.io.video import reader
from bob.db.base.annotations import read_annotation_file
from . import OULUNPU_FRAME_SHAPE
......@@ -19,8 +20,7 @@ class File(VideoPadFile):
:any:`numpy.array`
A frame of the video. The size is (3, 1920, 1080).
"""
vfilename = self.make_path(
directory=self.original_directory, extension='.avi')
vfilename = self.make_path(directory=self.original_directory, extension=".avi")
return iter(reader(vfilename))
@property
......@@ -32,8 +32,7 @@ class File(VideoPadFile):
int
The number of frames.
"""
vfilename = self.make_path(
directory=self.original_directory, extension='.avi')
vfilename = self.make_path(directory=self.original_directory, extension=".avi")
return reader(vfilename).number_of_frames
@property
......@@ -49,7 +48,11 @@ class File(VideoPadFile):
@property
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
-------
......@@ -57,24 +60,35 @@ class File(VideoPadFile):
The annotations as a dictionary, e.g.:
``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}``
"""
path = self.make_path(
directory=self.original_directory, extension='.txt')
if (
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 = {}
with open(path) as f:
for line in f:
line = line.strip()
if not line:
continue
num_frame, x_eye_right, y_eye_right, x_eye_left, y_eye_left = \
line.split(',')
num_frame, x_eye_right, y_eye_right, x_eye_left, y_eye_left = line.split(
","
)
annotations[num_frame] = {
'reye': (int(y_eye_right), int(x_eye_right)),
'leye': (int(y_eye_left), int(x_eye_left)),
"reye": (int(y_eye_right), int(x_eye_right)),
"leye": (int(y_eye_left), int(x_eye_left)),
}
return annotations
def load(self, directory=None, extension='.avi',
frame_selector=FrameSelector(selection_style='all')):
def load(
self,
directory=None,
extension=".avi",
frame_selector=FrameSelector(selection_style="all"),
):
"""Loads the video file and returns in a
:any:`bob.bio.video.FrameContainer`.
......@@ -100,29 +114,70 @@ class File(VideoPadFile):
class Database(FileListPadDatabase):
"""The database interface for the OULU-NPU dataset."""
def __init__(self, original_directory=rc['bob.db.oulunpu.directory'],
name='oulunpu', pad_file_class=None,
original_extension=".avi", **kwargs):
def __init__(
self,
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:
pad_file_class = File
filelists_directory = resource_filename(__name__, 'lists')
filelists_directory = resource_filename(__name__, "lists")
super(Database, self).__init__(
filelists_directory=filelists_directory, name=name,
filelists_directory=filelists_directory,
name=name,
original_directory=original_directory,
pad_file_class=pad_file_class,
original_extension=original_extension,
annotation_directory=annotation_directory,
training_depends_on_protocol=True,
**kwargs)
def objects(self, groups=None, protocol=None, purposes=None,
model_ids=None, classes=None, **kwargs):
**kwargs
)
def objects(
self,
groups=None,
protocol=None,
purposes=None,
model_ids=None,
classes=None,
**kwargs
):
"""Returns the requested samples."""
files = super(Database, self).objects(
groups=groups, protocol=protocol, purposes=purposes,
model_ids=model_ids, classes=classes, **kwargs)
groups=groups,
protocol=protocol,
purposes=purposes,
model_ids=model_ids,
classes=classes,
**kwargs
)
for f in files:
f.original_directory = self.original_directory
f.original_extension = self.original_extension
f.annotation_directory = self.annotation_directory
return files
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