augment.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. # copyright (c) 2022 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 os
  15. import sys
  16. import numpy as np
  17. import random
  18. from copy import deepcopy
  19. def order_by_tbyx(ocr_info):
  20. res = sorted(ocr_info, key=lambda r: (r["bbox"][1], r["bbox"][0]))
  21. for i in range(len(res) - 1):
  22. for j in range(i, 0, -1):
  23. if abs(res[j + 1]["bbox"][1] - res[j]["bbox"][1]) < 20 and \
  24. (res[j + 1]["bbox"][0] < res[j]["bbox"][0]):
  25. tmp = deepcopy(res[j])
  26. res[j] = deepcopy(res[j + 1])
  27. res[j + 1] = deepcopy(tmp)
  28. else:
  29. break
  30. return res