augment.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # copyright (c) 2020 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. """
  15. This code is refer from:
  16. https://github.com/RubanSeven/Text-Image-Augmentation-python/blob/master/augment.py
  17. """
  18. import numpy as np
  19. from .warp_mls import WarpMLS
  20. def tia_distort(src, segment=4):
  21. img_h, img_w = src.shape[:2]
  22. cut = img_w // segment
  23. thresh = cut // 3
  24. src_pts = list()
  25. dst_pts = list()
  26. src_pts.append([0, 0])
  27. src_pts.append([img_w, 0])
  28. src_pts.append([img_w, img_h])
  29. src_pts.append([0, img_h])
  30. dst_pts.append([np.random.randint(thresh), np.random.randint(thresh)])
  31. dst_pts.append([img_w - np.random.randint(thresh), np.random.randint(thresh)])
  32. dst_pts.append(
  33. [img_w - np.random.randint(thresh), img_h - np.random.randint(thresh)]
  34. )
  35. dst_pts.append([np.random.randint(thresh), img_h - np.random.randint(thresh)])
  36. half_thresh = thresh * 0.5
  37. for cut_idx in np.arange(1, segment, 1):
  38. src_pts.append([cut * cut_idx, 0])
  39. src_pts.append([cut * cut_idx, img_h])
  40. dst_pts.append(
  41. [
  42. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  43. np.random.randint(thresh) - half_thresh,
  44. ]
  45. )
  46. dst_pts.append(
  47. [
  48. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  49. img_h + np.random.randint(thresh) - half_thresh,
  50. ]
  51. )
  52. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  53. dst = trans.generate()
  54. return dst
  55. def tia_stretch(src, segment=4):
  56. img_h, img_w = src.shape[:2]
  57. cut = img_w // segment
  58. thresh = cut * 4 // 5
  59. src_pts = list()
  60. dst_pts = list()
  61. src_pts.append([0, 0])
  62. src_pts.append([img_w, 0])
  63. src_pts.append([img_w, img_h])
  64. src_pts.append([0, img_h])
  65. dst_pts.append([0, 0])
  66. dst_pts.append([img_w, 0])
  67. dst_pts.append([img_w, img_h])
  68. dst_pts.append([0, img_h])
  69. half_thresh = thresh * 0.5
  70. for cut_idx in np.arange(1, segment, 1):
  71. move = np.random.randint(thresh) - half_thresh
  72. src_pts.append([cut * cut_idx, 0])
  73. src_pts.append([cut * cut_idx, img_h])
  74. dst_pts.append([cut * cut_idx + move, 0])
  75. dst_pts.append([cut * cut_idx + move, img_h])
  76. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  77. dst = trans.generate()
  78. return dst
  79. def tia_perspective(src):
  80. img_h, img_w = src.shape[:2]
  81. thresh = img_h // 2
  82. src_pts = list()
  83. dst_pts = list()
  84. src_pts.append([0, 0])
  85. src_pts.append([img_w, 0])
  86. src_pts.append([img_w, img_h])
  87. src_pts.append([0, img_h])
  88. dst_pts.append([0, np.random.randint(thresh)])
  89. dst_pts.append([img_w, np.random.randint(thresh)])
  90. dst_pts.append([img_w, img_h - np.random.randint(thresh)])
  91. dst_pts.append([0, img_h - np.random.randint(thresh)])
  92. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  93. dst = trans.generate()
  94. return dst