utils.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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