# This Python file uses the following encoding: utf-8 import re import cv2 import numpy as np from PIL import Image def random_normal_distribution_int(a, b, n=3): """Generate a normal distribution int within the interval. Use the average value of several random numbers to simulate normal distribution. Args: a (int): The minimum of the interval. b (int): The maximum of the interval. n (int): The amount of numbers in simulation. Default to 3. Returns: int """ if a < b: output = np.mean(np.random.randint(a, b, size=n)) return int(output.round()) else: return b def ensure_time(second, n=3, precision=3): """Ensure to be time. Args: second (int, float, tuple): time, such as 10, (10, 30), '10, 30' n (int): The amount of numbers in simulation. Default to 5. precision (int): Decimals. Returns: float: """ if isinstance(second, tuple): multiply = 10 ** precision result = random_normal_distribution_int(second[0] * multiply, second[1] * multiply, n) / multiply return round(result, precision) elif isinstance(second, str): if ',' in second: lower, upper = second.replace(' ', '').split(',') lower, upper = int(lower), int(upper) return ensure_time((lower, upper), n=n, precision=precision) if '-' in second: lower, upper = second.replace(' ', '').split('-') lower, upper = int(lower), int(upper) return ensure_time((lower, upper), n=n, precision=precision) else: return int(second) else: return second