Commit 8167a404 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

refactor the interface a bit

parent a7c81a89
Pipeline #20055 passed with stages
in 48 minutes and 50 seconds
UVAD_FRAME_SHAPE = (3, 720, 1024)
"Shape of the video frames in the uvad database."
from .query import Database, File from .query import Database, File
......
from pkg_resources import resource_filename from . import UVAD_FRAME_SHAPE
from bob.extension import rc
from bob.io.video import reader
from bob.pad.base.database import FileListPadDatabase from bob.pad.base.database import FileListPadDatabase
from bob.pad.face.database import VideoPadFile from bob.pad.face.database import VideoPadFile
from bob.pad.face.utils import frames, number_of_frames from pkg_resources import resource_filename
import numpy import numpy
from bob.extension import rc
class File(VideoPadFile): class File(VideoPadFile):
"""The file objects of the UVAD dataset.""" """The file objects of the OULU-NPU dataset."""
pass
@property
class Database(FileListPadDatabase): def frames(self):
"""The database interface for the UVAD dataset.""" """Yields the frames of the biofile one by one.
def __init__(self, original_directory=rc['bob.db.uvad.directory'],
bio_file_class=None, name='uvad',
original_extension=None, **kwargs):
if bio_file_class is None:
bio_file_class = File
filelists_directory = resource_filename(__name__, 'lists')
super(Database, self).__init__(
filelists_directory=filelists_directory, name=name,
original_directory=original_directory,
bio_file_class=bio_file_class,
original_extension=original_extension,
training_depends_on_protocol=True,
models_depend_on_protocol=True,
**kwargs)
def frames(self, padfile):
"""Yields the frames of the padfile one by one.
Parameters
----------
padfile : :any:`File`
The high-level pad file
Yields Yields
------ ------
:any:`numpy.array` :any:`numpy.array`
A frame of the video. The size is (3, 720, 1024). A frame of the video. The size is (3, 1920, 1080).
""" """
vfilename = padfile.make_path( vfilename = self.make_path(directory=self.original_directory)
directory=self.original_directory, for frame in reader(vfilename):
extension=self.original_extension)
for frame in frames(vfilename):
# crop frames to 720 x 1024 # crop frames to 720 x 1024
h, w = numpy.shape(frame)[-2:] h, w = numpy.shape(frame)[-2:]
dh, dw = (h - 720) // 2, (w - 1024) // 2 dh, dw = (h - 720) // 2, (w - 1024) // 2
...@@ -56,23 +31,17 @@ class Database(FileListPadDatabase): ...@@ -56,23 +31,17 @@ class Database(FileListPadDatabase):
assert frame.shape == self.frame_shape, frame.shape assert frame.shape == self.frame_shape, frame.shape
yield frame yield frame
def number_of_frames(self, padfile): @property
def number_of_frames(self):
"""Returns the number of frames in a video file. """Returns the number of frames in a video file.
Parameters
----------
padfile : :any:`File`
The high-level pad file
Returns Returns
------- -------
int int
The number of frames. The number of frames.
""" """
vfilename = padfile.make_path( vfilename = self.make_path(directory=self.original_directory)
directory=self.original_directory, return reader(vfilename).number_of_frames
extension=self.original_extension)
return number_of_frames(vfilename)
@property @property
def frame_shape(self): def frame_shape(self):
...@@ -81,6 +50,75 @@ class Database(FileListPadDatabase): ...@@ -81,6 +50,75 @@ class Database(FileListPadDatabase):
Returns Returns
------- -------
(int, int, int) (int, int, int)
The (#Channels, Height, Width) which is (3, 720, 1024). The (#Channels, Height, Width) which is :any:`UVAD_FRAME_SHAPE`.
"""
return UVAD_FRAME_SHAPE
class Database(FileListPadDatabase):
"""The database interface for the OULU-NPU dataset."""
def __init__(self, original_directory=rc['bob.db.uvad.directory'],
name='uvad', pad_file_class=None,
original_extension=None, **kwargs):
if pad_file_class is None:
pad_file_class = File
filelists_directory = resource_filename(__name__, 'lists')
super(Database, self).__init__(
filelists_directory=filelists_directory, name=name,
original_directory=original_directory,
pad_file_class=pad_file_class,
original_extension=original_extension,
**kwargs)
def objects(self, groups=None, protocol=None, purposes=None,
model_ids=None, classes=None, **kwargs):
files = super(Database, self).objects(
groups=groups, protocol=protocol, purposes=purposes,
model_ids=model_ids, classes=classes, **kwargs)
for f in files:
f.original_directory = self.original_directory
return files
def frames(self, padfile):
return padfile.frames
def number_of_frames(self, padfile):
return padfile.number_of_frames
@property
def frame_shape(self):
return UVAD_FRAME_SHAPE
def annotations(self, padfile):
"""Reads the annotations for the given padfile.
Parameters
----------
padfile : :any:`File`
The file object for which the annotations should be read.
Returns
-------
dict
The annotations as a dictionary, e.g.:
``{'0': {'reye':(re_y,re_x), 'leye':(le_y,le_x)}, ...}``
""" """
return (3, 720, 1024) path = padfile.make_path(
directory=self.original_directory, extension=None)
# the annotations are in the uvad/release_1/face-locations-v3 folder
path = path.replace('release_1', 'release_1/face-locations-v3')
path = path[:-3] + 'face'
annotations = {}
with open(path) as f:
for line in f:
line = line.strip()
if not line:
continue
num_frame, x_eye_left, y_eye_left, x_eye_right, y_eye_right = \
line.split()
annotations[num_frame] = {
'reye': (int(y_eye_right), int(x_eye_right)),
'leye': (int(y_eye_left), int(x_eye_left)),
}
return annotations
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