warp.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 ..base import BaseComponent
  16. class DocTrPostProcess(BaseComponent):
  17. """normalize image such as substract mean, divide std"""
  18. INPUT_KEYS = ["pred"]
  19. OUTPUT_KEYS = ["doctr_img"]
  20. DEAULT_INPUTS = {"pred": "pred"}
  21. DEAULT_OUTPUTS = {"doctr_img": "doctr_img"}
  22. def __init__(self, scale=None, **kwargs):
  23. super().__init__()
  24. if isinstance(scale, str):
  25. scale = np.float32(scale)
  26. self.scale = np.float32(scale if scale is not None else 255.0)
  27. def apply(self, pred):
  28. im = pred[0]
  29. assert isinstance(im, np.ndarray), "invalid input 'im' in DocTrPostProcess"
  30. im = im.squeeze()
  31. im = im.transpose(1, 2, 0)
  32. im *= self.scale
  33. im = im[:, :, ::-1]
  34. im = im.astype("uint8", copy=False)
  35. result = {"doctr_img": im}
  36. return result