general_recognition.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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. import numpy as np
  15. from ...utils.func_register import FuncRegister
  16. from ...modules.general_recognition.model_list import MODELS
  17. from ..components import *
  18. from ..results import BaseResult
  19. from ..utils.process_hook import batchable_method
  20. from .base import BasicPredictor
  21. class ShiTuRecPredictor(BasicPredictor):
  22. entities = MODELS
  23. _FUNC_MAP = {}
  24. register = FuncRegister(_FUNC_MAP)
  25. def _check_args(self, kwargs):
  26. assert set(kwargs.keys()).issubset(set(["batch_size"]))
  27. return kwargs
  28. def _build_components(self):
  29. ops = {}
  30. ops["ReadImage"] = ReadImage(
  31. batch_size=self.kwargs.get("batch_size", 1), format="RGB"
  32. )
  33. for cfg in self.config["PreProcess"]["transform_ops"]:
  34. tf_key = list(cfg.keys())[0]
  35. func = self._FUNC_MAP.get(tf_key)
  36. args = cfg.get(tf_key, {})
  37. op = func(self, **args) if args else func(self)
  38. ops[tf_key] = op
  39. predictor = ImagePredictor(
  40. model_dir=self.model_dir,
  41. model_prefix=self.MODEL_FILE_PREFIX,
  42. option=self.pp_option,
  43. )
  44. ops["predictor"] = predictor
  45. post_processes = self.config["PostProcess"]
  46. for key in post_processes:
  47. func = self._FUNC_MAP.get(key)
  48. args = post_processes.get(key, {})
  49. op = func(self, **args) if args else func(self)
  50. ops[key] = op
  51. return ops
  52. @register("ResizeImage")
  53. # TODO(gaotingquan): backend & interpolation
  54. def build_resize(
  55. self,
  56. resize_short=None,
  57. size=None,
  58. backend="cv2",
  59. interpolation="LINEAR",
  60. return_numpy=False,
  61. ):
  62. assert resize_short or size
  63. if resize_short:
  64. op = ResizeByShort(
  65. target_short_edge=resize_short, size_divisor=None, interp="LINEAR"
  66. )
  67. else:
  68. op = Resize(target_size=size)
  69. return op
  70. @register("CropImage")
  71. def build_crop(self, size=224):
  72. return Crop(crop_size=size)
  73. @register("NormalizeImage")
  74. def build_normalize(
  75. self,
  76. mean=[0.485, 0.456, 0.406],
  77. std=[0.229, 0.224, 0.225],
  78. scale=1 / 255,
  79. order="",
  80. channel_num=3,
  81. ):
  82. assert channel_num == 3
  83. return Normalize(mean=mean, std=std)
  84. @register("ToCHWImage")
  85. def build_to_chw(self):
  86. return ToCHWImage()
  87. @register("NormalizeFeatures")
  88. def build_normalize_features(self):
  89. return NormalizeFeatures()
  90. @batchable_method
  91. def _pack_res(self, data):
  92. keys = ["img_path", "rec_feature"]
  93. return {"result": BaseResult({key: data[key] for key in keys})}