Coverage for src/bob/bio/vein/database/roi_annotation.py: 59%
22 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-12 23:27 +0200
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-12 23:27 +0200
1from pathlib import Path
3from numpy import loadtxt
4from sklearn.base import BaseEstimator, TransformerMixin
6from bob.pipelines import DelayedSample
9class ROIAnnotation(TransformerMixin, BaseEstimator):
10 """
11 Transformer class to read ROI annotation file for grayscale images
12 """
14 def __init__(self, roi_path):
15 super(ROIAnnotation, self).__init__()
16 self.roi_path = Path(roi_path) if roi_path else False
18 def fit(self, X, y=None):
19 return self
21 def _more_tags(self):
22 return {
23 "requires_fit": False,
24 }
26 def transform(self, X):
27 """
28 If the annotation file exists, read it and add the ROI points as an attribute to the sample.
29 """
30 if self.roi_path and self.roi_path.exists():
31 annotated_samples = []
32 for x in X:
33 roi_file = (self.roi_path / x.key).with_suffix(".txt")
34 roi = loadtxt(roi_file, dtype="uint16")
36 sample = DelayedSample.from_sample(x, roi=roi)
37 annotated_samples.append(sample)
39 return annotated_samples
40 else:
41 return X