Commit 5f423214 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira

Merge branch 'sample_loader_as_transformer' into 'master'

SampleLoader as transformer

See merge request !88
parents 50af61d4 6b6c80e4
Pipeline #46569 passed with stages
in 43 minutes and 49 seconds
from bob.bio.face.database import MEDSDatabase
# In case protocol is comming from chain loading
# https://www.idiap.ch/software/bob/docs/bob/bob.extension/stable/py_api.html#bob.extension.config.load
if "protocol" not in locals():
protocol = "verification_fold1"
database = MEDSDatabase(protocol=protocol)
......@@ -9,12 +9,15 @@
from bob.bio.base.database import (
CSVDataset,
CSVDatasetZTNorm,
CSVToSampleLoader,
)
from bob.pipelines.datasets import CSVToSampleLoader
from bob.bio.face.database.sample_loaders import EyesAnnotations
from bob.extension import rc
from bob.extension.download import get_file
import bob.io.base
from bob.bio.face.database.sample_loaders import eyes_annotations_loader
from sklearn.pipeline import make_pipeline
# from bob.bio.face.database.sample_loaders import eyes_annotations_loader
import os
......@@ -57,13 +60,15 @@ class MEDSDatabase(CSVDatasetZTNorm):
database = CSVDataset(
dataset_protocol_path,
protocol,
csv_to_sample_loader=CSVToSampleLoader(
data_loader=bob.io.base.load,
metadata_loader=eyes_annotations_loader,
dataset_original_directory=rc["bob.db.meds.directory"]
if rc["bob.db.meds.directory"]
else "",
extension=".jpg",
csv_to_sample_loader=make_pipeline(
CSVToSampleLoader(
data_loader=bob.io.base.load,
dataset_original_directory=rc["bob.db.meds.directory"]
if rc["bob.db.meds.directory"]
else "",
extension=".jpg",
),
EyesAnnotations(),
),
)
......
......@@ -4,23 +4,40 @@
""" Sample and Metatada loaders"""
def eyes_annotations_loader(row, header=None):
"""
Convert leye_x, leye_y, reye_x, reye_y attributes to `annotations = (leye, reye)`
"""
def find_attribute(attribute):
for i, a in enumerate(header):
if a == attribute:
return i
else:
ValueError(f"Attribute not found in the dataset: {a}")
eyes = {
"leye": (row[find_attribute("leye_x")], row[find_attribute("leye_y")]),
"reye": (row[find_attribute("reye_x")], row[find_attribute("reye_y")]),
}
annotation = {"annotations": eyes}
return annotation
from bob.pipelines import DelayedSample, Sample, SampleSet
from sklearn.base import TransformerMixin, BaseEstimator
class EyesAnnotations(TransformerMixin, BaseEstimator):
def fit(self, X, y=None):
return self
def _more_tags(self):
return {
"stateless": True,
"requires_fit": False,
}
def transform(self, X):
"""
Convert leye_x, leye_y, reye_x, reye_y attributes to `annotations = (leye, reye)`
"""
def find_attribute(x, attribute):
if hasattr(x, attribute):
return getattr(x, attribute)
else:
ValueError(f"Attribute not found in the dataset: {attribute}")
annotated_samples = []
for x in X:
eyes = {
"leye": (find_attribute, (x, "leye_x"), find_attribute(x, "leye_y")),
"reye": (find_attribute(x, "reye_x"), find_attribute(x, "reye_y")),
}
sample = DelayedSample(x._load, parent=x, annotations=eyes)
[delattr(sample, a) for a in ["leye_x", "leye_y", "reye_x", "reye_y"]]
annotated_samples.append(sample)
return annotated_samples
......@@ -110,6 +110,7 @@ setup(
"replaymobile-img-licit = bob.bio.face.config.database.replaymobile:replaymobile_licit",
"replaymobile-img-spoof = bob.bio.face.config.database.replaymobile:replaymobile_spoof",
"fargo = bob.bio.face.config.database.fargo:database",
"meds = bob.bio.face.config.database.meds:database",
],
"bob.bio.annotator": [
"facedetect = bob.bio.face.config.annotator.facedetect:annotator",
......@@ -170,6 +171,7 @@ setup(
"replaymobile-img-licit = bob.bio.face.config.database.replaymobile_licit",
"replaymobile-img-spoof = bob.bio.face.config.database.replaymobile_spoof",
"fargo = bob.bio.face.config.database.fargo",
"meds = bob.bio.face.config.database.meds",
],
"bob.bio.cli": [
"display-face-annotations = bob.bio.face.script.display_face_annotations:display_face_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