augment.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(
  32. [img_w - np.random.randint(thresh), np.random.randint(thresh)])
  33. dst_pts.append(
  34. [img_w - np.random.randint(thresh), img_h - np.random.randint(thresh)])
  35. dst_pts.append(
  36. [np.random.randint(thresh), img_h - np.random.randint(thresh)])
  37. half_thresh = thresh * 0.5
  38. for cut_idx in np.arange(1, segment, 1):
  39. src_pts.append([cut * cut_idx, 0])
  40. src_pts.append([cut * cut_idx, img_h])
  41. dst_pts.append([
  42. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  43. np.random.randint(thresh) - half_thresh
  44. ])
  45. dst_pts.append([
  46. cut * cut_idx + np.random.randint(thresh) - half_thresh,
  47. img_h + np.random.randint(thresh) - half_thresh
  48. ])
  49. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  50. dst = trans.generate()
  51. return dst
  52. def tia_stretch(src, segment=4):
  53. img_h, img_w = src.shape[:2]
  54. cut = img_w // segment
  55. thresh = cut * 4 // 5
  56. src_pts = list()
  57. dst_pts = list()
  58. src_pts.append([0, 0])
  59. src_pts.append([img_w, 0])
  60. src_pts.append([img_w, img_h])
  61. src_pts.append([0, img_h])
  62. dst_pts.append([0, 0])
  63. dst_pts.append([img_w, 0])
  64. dst_pts.append([img_w, img_h])
  65. dst_pts.append([0, img_h])
  66. half_thresh = thresh * 0.5
  67. for cut_idx in np.arange(1, segment, 1):
  68. move = np.random.randint(thresh) - half_thresh
  69. src_pts.append([cut * cut_idx, 0])
  70. src_pts.append([cut * cut_idx, img_h])
  71. dst_pts.append([cut * cut_idx + move, 0])
  72. dst_pts.append([cut * cut_idx + move, img_h])
  73. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  74. dst = trans.generate()
  75. return dst
  76. def tia_perspective(src):
  77. img_h, img_w = src.shape[:2]
  78. thresh = img_h // 2
  79. src_pts = list()
  80. dst_pts = list()
  81. src_pts.append([0, 0])
  82. src_pts.append([img_w, 0])
  83. src_pts.append([img_w, img_h])
  84. src_pts.append([0, img_h])
  85. dst_pts.append([0, np.random.randint(thresh)])
  86. dst_pts.append([img_w, np.random.randint(thresh)])
  87. dst_pts.append([img_w, img_h - np.random.randint(thresh)])
  88. dst_pts.append([0, img_h - np.random.randint(thresh)])
  89. trans = WarpMLS(src, src_pts, dst_pts, img_w, img_h)
  90. dst = trans.generate()
  91. return dst