Coverage for src/bob/bio/base/preprocessor/Preprocessor.py: 89%
19 statements
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-14 21:41 +0100
« prev ^ index » next coverage.py v7.6.5, created at 2024-11-14 21:41 +0100
1#!/usr/bin/env python
2# vim: set fileencoding=utf-8 :
3# @author: Manuel Guenther <Manuel.Guenther@idiap.ch>
4# @date: Tue Oct 2 12:12:39 CEST 2012
6import warnings
8from .. import utils
11class Preprocessor(object):
12 """This is the base class for all preprocessors.
13 It defines the minimum requirements for all derived proprocessor classes.
15 **Parameters:**
17 writes_data : bool
18 Select, if the preprocessor actually writes preprocessed images, or if it is simply returning values.
20 read_original_data: callable or ``None``
21 This function is used to read the original data from file.
22 It takes three inputs: A :py:class:`bob.bio.base.database.BioFile` (or one of its derivatives), the original directory (as ``str``) and the original extension (as ``str``).
23 If ``None``, the default function :py:func:`bob.bio.base.read_original_data` is used.
25 min_preprocessed_file_size: int
26 The minimum file size of a saved preprocessd data in bytes. If the saved
27 preprocessed data file size is smaller than this, it is assumed to be a
28 corrupt file and the data will be processed again.
30 kwargs : ``key=value`` pairs
31 A list of keyword arguments to be written in the `__str__` function.
32 """
34 def __init__(
35 self,
36 writes_data=True,
37 read_original_data=None,
38 min_preprocessed_file_size=1000,
39 **kwargs,
40 ):
41 # Each class needs to have a constructor taking
42 # all the parameters that are required for the preprocessing as arguments
43 self.writes_data = writes_data
44 if read_original_data is None:
45 read_original_data = utils.read_original_data
46 self.read_original_data = read_original_data
47 self.min_preprocessed_file_size = min_preprocessed_file_size
48 self._kwargs = kwargs
50 warnings.warn(
51 "`bob.bio.base.preprocessor.Preprocessor` will be deprecated in 01/01/2021. "
52 "Please, implement your biometric algorithm using `bob.pipelines` (https://gitlab.idiap.ch/bob/bob.pipelines).",
53 DeprecationWarning,
54 )
56 # The call function (i.e. the operator() in C++ terms)
57 def __call__(self, data, annotations):
58 """__call__(data, annotations) -> data
60 This is the call function that you have to overwrite in the derived class.
61 The parameters that this function will receive are:
63 **Parameters:**
65 data : object
66 The original data that needs preprocessing, usually a :py:class:`numpy.ndarray`, but might be different.
68 annotations : {} or None
69 The annotations (if any) that belongs to the given ``data``; as a dictionary.
70 The type of the annotation depends on your kind of problem.
72 **Returns:**
74 data : object
75 The *preprocessed* data, usually a :py:class:`numpy.ndarray`, but might be different.
76 """
77 raise NotImplementedError(
78 "Please overwrite this function in your derived class"
79 )
81 def __str__(self):
82 """__str__() -> info
84 This function returns all parameters of this class (and its derived class).
86 **Returns:**
88 info : str
89 A string containing the full information of all parameters of this (and the derived) class.
90 """
91 return utils.pretty_print(self, self._kwargs)
93 ############################################################
94 # Special functions that might be overwritten on need
95 ############################################################
97 def write_data(self, data, data_file):
98 """Writes the given *preprocessed* data to a file with the given name.
99 In this base class implementation, we simply use :py:func:`bob.bio.base.save` for that.
100 If you have a different format (e.g. not images), please overwrite this function.
102 **Parameters:**
104 data : object
105 The preprocessed data, i.e., what is returned from `__call__`.
107 data_file : str or :py:class:`h5py.File`
108 The file open for writing, or the name of the file to write.
109 """
110 utils.save(data, data_file)
112 def read_data(self, data_file):
113 """read_data(data_file) -> data
115 Reads the *preprocessed* data from file.
116 In this base class implementation, it uses :py:func:`bob.bio.base.load` to do that.
117 If you have different format, please overwrite this function.
119 **Parameters:**
121 data_file : str or :py:class:`h5py.File`
122 The file open for reading or the name of the file to read from.
124 **Returns:**
126 data : object (usually :py:class:`numpy.ndarray`)
127 The preprocessed data read from file.
128 """
129 return utils.load(data_file)