Skip to content
Snippets Groups Projects
Commit a9653ef6 authored by Yannick DAYER's avatar Yannick DAYER
Browse files

Use read_annotation_file from bob.db

parent 6cad6f98
No related branches found
No related tags found
1 merge request!106Vulnerability framework - CSV datasets
...@@ -44,8 +44,8 @@ logger.info(f"Annotations files will be fetched from '{annotations_path}'") ...@@ -44,8 +44,8 @@ logger.info(f"Annotations files will be fetched from '{annotations_path}'")
logger.debug(f"Instantiation of ReplayMobile bio database with protocol '{protocol}'") logger.debug(f"Instantiation of ReplayMobile bio database with protocol '{protocol}'")
database = ReplayMobileBioDatabase( database = ReplayMobileBioDatabase(
dataset_protocols_path=dataset_protocol_path, protocol_definition_path=dataset_protocol_path,
protocol=protocol, protocol_name=protocol,
data_path=data_path, data_path=data_path,
data_extension=data_extension, data_extension=data_extension,
annotations_path=annotations_path, annotations_path=annotations_path,
......
...@@ -2,14 +2,14 @@ ...@@ -2,14 +2,14 @@
# Yannick Dayer <yannick.dayer@idiap.ch> # Yannick Dayer <yannick.dayer@idiap.ch>
from bob.bio.base.database import CSVDataset, CSVToSampleLoaderBiometrics from bob.bio.base.database import CSVDataset, CSVToSampleLoaderBiometrics
from bob.pipelines.datasets.sample_loaders import AnnotationsLoader from bob.pipelines.sample_loaders import AnnotationsLoader
from bob.pipelines.sample import DelayedSample from bob.pipelines.sample import DelayedSample
from bob.db.base.annotations import read_annotation_file
from bob.extension.download import get_file from bob.extension.download import get_file
from bob.io.video import reader from bob.io.video import reader
from bob.extension import rc from bob.extension import rc
import bob.core import bob.core
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.pipeline import make_pipeline from sklearn.pipeline import make_pipeline
import functools import functools
import os.path import os.path
...@@ -54,7 +54,7 @@ def load_frame_from_file_replaymobile(file_name, frame, should_flip): ...@@ -54,7 +54,7 @@ def load_frame_from_file_replaymobile(file_name, frame, should_flip):
image = numpy.transpose(image, (0, 2, 1)) image = numpy.transpose(image, (0, 2, 1))
return image return image
def read_frame_annotation_file_replaymobile(file_name, frame): def read_frame_annotation_file_replaymobile(file_name, frame, annotations_type="json"):
"""Returns the bounding-box for one frame of a video file of replay-mobile. """Returns the bounding-box for one frame of a video file of replay-mobile.
Given an annnotation file location and a frame number, returns the bounding Given an annnotation file location and a frame number, returns the bounding
...@@ -72,36 +72,16 @@ def read_frame_annotation_file_replaymobile(file_name, frame): ...@@ -72,36 +72,16 @@ def read_frame_annotation_file_replaymobile(file_name, frame):
---------- ----------
file_name: str file_name: str
The complete annotation file path and name (with extension). The annotation file name (relative to annotations_path).
frame: int frame: int
The video frame index. The video frame index.
""" """
logger.debug(f"Reading annotation file '{file_name}', frame {frame}.") logger.debug(f"Reading annotation file '{file_name}', frame {frame}.")
if not file_name:
return None
if not os.path.exists(file_name): video_annotations = read_annotation_file(file_name, annotation_type=annotations_type)
raise IOError(f"The annotation file '{file_name}' was not found") # read_annotation_file returns an ordered dict with string keys
return video_annotations[f"{frame}"]
with open(file_name, 'r') as f:
# One line is one frame, each line contains a bounding box coordinates
line = f.readlines()[frame]
positions = line.split(' ')
if len(positions) != 4:
raise ValueError(f"The content of '{file_name}' was not correct for frame {frame} ({positions})")
annotations = {
'topleft': (float(positions[1]), float(positions[0])),
'bottomright':(
float(positions[1])+float(positions[3]),
float(positions[0])+float(positions[2])
)
}
return annotations
class ReplayMobileCSVFrameSampleLoader(CSVToSampleLoaderBiometrics): class ReplayMobileCSVFrameSampleLoader(CSVToSampleLoaderBiometrics):
"""A loader transformer returning a specific frame of a video file. """A loader transformer returning a specific frame of a video file.
...@@ -194,7 +174,7 @@ class FrameBoundingBoxAnnotationLoader(AnnotationsLoader): ...@@ -194,7 +174,7 @@ class FrameBoundingBoxAnnotationLoader(AnnotationsLoader):
delayed_attributes=dict( delayed_attributes=dict(
annotations=functools.partial( annotations=functools.partial(
read_frame_annotation_file_replaymobile, read_frame_annotation_file_replaymobile,
file_name=annotation_file, file_name=f"{self.annotation_directory}:{x.path}{self.annotation_extension}",
frame=int(x.frame), frame=int(x.frame),
) )
), ),
...@@ -235,10 +215,12 @@ class ReplayMobileBioDatabase(CSVDataset): ...@@ -235,10 +215,12 @@ class ReplayMobileBioDatabase(CSVDataset):
""" """
def __init__( def __init__(
self, self,
protocol_name="bio-grandtest", protocol_name="grandtest",
protocol_definition_path=None, protocol_definition_path=None,
data_path=None, data_path=None,
annotation_path=None, data_extension=".mov",
annotations_path=None,
annotations_extension=".json",
**kwargs **kwargs
): ):
if protocol_definition_path is None: if protocol_definition_path is None:
...@@ -253,27 +235,27 @@ class ReplayMobileBioDatabase(CSVDataset): ...@@ -253,27 +235,27 @@ class ReplayMobileBioDatabase(CSVDataset):
# Defaults to cwd if config not defined # Defaults to cwd if config not defined
data_path = rc.get("bob.db.replaymobile.directory", "") data_path = rc.get("bob.db.replaymobile.directory", "")
if annotation_path is None: if annotations_path is None:
# Defaults to {data_path}/faceloc/rect if config not defined # Defaults to {data_path}/faceloc/rect if config not defined
annotation_path = rc.get( annotations_path = rc.get(
"bob.db.replaymobile.annotation_directory", "bob.db.replaymobile.annotation_directory",
os.path.join(data_path, "faceloc/rect/") os.path.join(data_path, "faceloc/rect/")
) )
logger.info(f"Database: Loading database definition from '{protocol_definition_path}'.") logger.info(f"Database: Loading database definition from '{protocol_definition_path}'.")
logger.info(f"Database: Defining data files path as '{data_path}'.") logger.info(f"Database: Defining data files path as '{data_path}'.")
logger.info(f"Database: Defining annotation files path as '{annotation_path}'.") logger.info(f"Database: Defining annotation files path as '{annotations_path}'.")
super().__init__( super().__init__(
protocol_definition_path, protocol_definition_path,
protocol_name, protocol_name,
csv_to_sample_loader=make_pipeline( csv_to_sample_loader=make_pipeline(
ReplayMobileCSVFrameSampleLoader( ReplayMobileCSVFrameSampleLoader(
dataset_original_directory=data_path, dataset_original_directory=data_path,
extension=".mov", extension=data_extension,
), ),
FrameBoundingBoxAnnotationLoader( FrameBoundingBoxAnnotationLoader(
annotation_directory=annotation_path, annotation_directory=annotations_path,
annotation_extension=".face", annotation_extension=annotations_extension,
), ),
), ),
**kwargs **kwargs
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment