34 lines
1.2 KiB
Python

from typing import Mapping, List, Union, Tuple
import numpy as np
from PIL.Image import Image
from image_prediction.estimator.adapter.adapter import EstimatorAdapter
from image_prediction.utils import get_logger
logger = get_logger()
class Classifier:
def __init__(self, estimator_adapter: EstimatorAdapter, classes: Mapping[int, str]):
"""Abstraction layer over different estimator backends (e.g. keras or scikit-learn). For each backend to be used
an EstimatorAdapter must be implemented.
Args:
estimator_adapter: adapter for a given estimator backend; expected to be a classifier that returns numeric
labels as predictions
classes: mapping from a numerical label to a human-readable label for classes
"""
self.__estimator_adapter = estimator_adapter
self._classes = classes
def predict(self, batch: Union[np.array, Tuple[Image]]) -> List[str]:
if not isinstance(batch, tuple) and batch.shape[0] == 0:
return []
return [self._classes[numeric_label] for numeric_label in self.__estimator_adapter.predict(batch)]
def __call__(self, batch: np.array) -> List[str]:
return self.predict(batch)