utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # This Python file uses the following encoding: utf-8
  2. import re
  3. import cv2
  4. import numpy as np
  5. from PIL import Image
  6. def random_normal_distribution_int(a, b, n=3):
  7. """Generate a normal distribution int within the interval. Use the average value of several random numbers to
  8. simulate normal distribution.
  9. Args:
  10. a (int): The minimum of the interval.
  11. b (int): The maximum of the interval.
  12. n (int): The amount of numbers in simulation. Default to 3.
  13. Returns:
  14. int
  15. """
  16. if a < b:
  17. output = np.mean(np.random.randint(a, b, size=n))
  18. return int(output.round())
  19. else:
  20. return b
  21. def ensure_time(second, n=3, precision=3):
  22. """Ensure to be time.
  23. Args:
  24. second (int, float, tuple): time, such as 10, (10, 30), '10, 30'
  25. n (int): The amount of numbers in simulation. Default to 5.
  26. precision (int): Decimals.
  27. Returns:
  28. float:
  29. """
  30. if isinstance(second, tuple):
  31. multiply = 10 ** precision
  32. result = random_normal_distribution_int(second[0] * multiply, second[1] * multiply, n) / multiply
  33. return round(result, precision)
  34. elif isinstance(second, str):
  35. if ',' in second:
  36. lower, upper = second.replace(' ', '').split(',')
  37. lower, upper = int(lower), int(upper)
  38. return ensure_time((lower, upper), n=n, precision=precision)
  39. if '-' in second:
  40. lower, upper = second.replace(' ', '').split('-')
  41. lower, upper = int(lower), int(upper)
  42. return ensure_time((lower, upper), n=n, precision=precision)
  43. else:
  44. return int(second)
  45. else:
  46. return second
  47. def point2str(x, y, length=4):
  48. """
  49. Args:
  50. x (int, float):
  51. y (int, float):
  52. length (int): Align length.
  53. Returns:
  54. str: String with numbers right aligned, such as '( 100, 80)'.
  55. """
  56. return '(%s, %s)' % (str(int(x)).rjust(length), str(int(y)).rjust(length))
  57. def random_rectangle_point(area, n=3):
  58. """Choose a random point in an area.
  59. Args:
  60. area: (upper_left_x, upper_left_y, bottom_right_x, bottom_right_y).
  61. n (int): The amount of numbers in simulation. Default to 3.
  62. Returns:
  63. tuple(int): (x, y)
  64. """
  65. x = random_normal_distribution_int(area[0], area[2], n=n)
  66. y = random_normal_distribution_int(area[1], area[3], n=n)
  67. return x, y
  68. def random_line_segments(p1, p2, n, random_range=(0, 0, 0, 0)):
  69. """Cut a line into multiple segments.
  70. Args:
  71. p1: (x, y).
  72. p2: (x, y).
  73. n: Number of slice.
  74. random_range: Add a random_range to points.
  75. Returns:
  76. list[tuple]: [(x0, y0), (x1, y1), (x2, y2)]
  77. """
  78. return [tuple((((n - index) * p1 + index * p2) / n).astype(int) + random_rectangle_point(random_range))
  79. for index in range(0, n + 1)]