predictor.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Dict, List, Union
  15. import numpy as np
  16. from ....modules.face_recognition.model_list import MODELS
  17. from ..image_feature import ImageFeaturePredictor
  18. class FaceFeaturePredictor(ImageFeaturePredictor):
  19. """FaceFeaturePredictor that inherits from ImageFeaturePredictor."""
  20. entities = MODELS
  21. def __init__(self, *args: List, flip: bool = False, **kwargs: Dict) -> None:
  22. """Initializes ClasPredictor.
  23. Args:
  24. *args: Arbitrary positional arguments passed to the superclass.
  25. flip: Whether to perform face flipping during inference. Default is False.
  26. **kwargs: Arbitrary keyword arguments passed to the superclass.
  27. """
  28. super().__init__(*args, **kwargs)
  29. self.flip = flip
  30. def process(self, batch_data: List[Union[str, np.ndarray]]) -> Dict[str, Any]:
  31. """
  32. Process a batch of data through the preprocessing, inference, and postprocessing.
  33. Args:
  34. batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).
  35. Returns:
  36. dict: A dictionary containing the input path, raw image, class IDs, scores, and label names for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
  37. """
  38. batch_raw_imgs = self.preprocessors["Read"](imgs=batch_data.instances)
  39. batch_imgs = self.preprocessors["Resize"](imgs=batch_raw_imgs)
  40. batch_imgs = self.preprocessors["Normalize"](imgs=batch_imgs)
  41. batch_imgs = self.preprocessors["ToCHW"](imgs=batch_imgs)
  42. x = self.preprocessors["ToBatch"](imgs=batch_imgs)
  43. batch_preds = self.infer(x=x)
  44. if self.flip:
  45. batch_preds_flipped = self.infer(x=[np.flip(data, axis=3) for data in x])
  46. for i in range(len(batch_preds)):
  47. batch_preds[i] = batch_preds[i] + batch_preds_flipped[i]
  48. features = self.postprocessors["NormalizeFeatures"](batch_preds)
  49. return {
  50. "input_path": batch_data.input_paths,
  51. "page_index": batch_data.page_indexes,
  52. "input_img": batch_raw_imgs,
  53. "feature": features,
  54. }