def apply_quasicrystal(self): """ Create a background with quasicrystal (https://en.wikipedia.org/wiki/Quasicrystal) """ width = Random.random_int(self._width_range[0], self._width_range[1]) height = Random.random_int(self._height_range[0], self._height_range[1]) image = np.zeros((height, width, 3), dtype=np.uint8) rotation_count = Random.random_int(10, 20) y_vec = np.arange(start=0, stop=width, dtype=np.float32) x_vec = np.arange(start=0, stop=height, dtype=np.float32) grid = np.meshgrid(y_vec, x_vec) y_matrix = np.reshape(np.asarray(grid[0]) / (width - 1) * 4 * math.pi - 2 * math.pi, (height, width, 1)) x_matrix = np.reshape(np.asarray(grid[1]) / (height - 1) * 4 * math.pi - 2 * math.pi, (height, width, 1)) y_matrix_3d = np.repeat(y_matrix, rotation_count, axis=-1) x_matrix_3d = np.repeat(x_matrix, rotation_count, axis=-1) rotation_vec = np.arange(start=0, stop=rotation_count, dtype=np.float32) rotation_vec = np.reshape(rotation_vec, newshape=(1, 1, rotation_count)) for k in range(3): frequency = Random.random_float(0, 1) * 30 + 20 # frequency phase = Random.random_float(0, 1) * 2 * math.pi # phase r = np.hypot(x_matrix_3d, y_matrix_3d) a = np.arctan2(y_matrix_3d, x_matrix_3d) + (rotation_vec * math.pi * 2.0 / rotation_count) z = np.cos(r * np.sin(a) * frequency + phase) z = np.sum(z, axis=-1) c = 255 - np.round(255 * z / rotation_count) c = np.asarray(c, dtype=np.uint8) image[:, :, k] = c return image
def gen_one_box(): l = Random.random_int(0, w) t = Random.random_int(0, h) r = int(l + min_w * Random.random_float(1, 3)) b = int(t + min_h * Random.random_float(1, 2)) box = (l, t, r, b) if r > w or b > h: return None return box
def get_fontcolor(self, bg_img): """ get font color by mean :param bg_img: :return: """ char_common_color_list = self.char_common_color_list if Random.random_float( 0, 1 ) <= self.use_char_common_color_probability and char_common_color_list: return eval(Random.random_choice_list(char_common_color_list)) else: image = np.asarray(bg_img) lab_image = cv2.cvtColor(image, cv2.COLOR_RGB2Lab) bg = lab_image[:, :, 0] l_mean = np.mean(bg) new_l = Random.random_int( 0, 127 - 80) if l_mean > 127 else Random.random_int(127 + 80, 255) new_a = Random.random_int(0, 255) new_b = Random.random_int(0, 255) lab_rgb = np.asarray([[[new_l, new_a, new_b]]], np.uint8) rbg = cv2.cvtColor(lab_rgb, cv2.COLOR_Lab2RGB) r = rbg[0, 0, 0] g = rbg[0, 0, 1] b = rbg[0, 0, 2] return (r, g, b, 255)
def apply_gauss_blur(self, ks=None): """ :param ks: guass kernal window size :return: """ bg_high = Random.random_float(220, 255) bg_low = bg_high - Random.random_float(1, 60) width = Random.random_int(self._width_range[0], self._width_range[1]) height = Random.random_int(self._height_range[0], self._height_range[1]) img = np.random.randint(bg_low, bg_high, (height, width)).astype(np.uint8) if ks is None: ks = [7, 9, 11, 13] k_size = Random.random_choice_list(ks) sigmas = [0, 1, 2, 3, 4, 5, 6, 7] sigma = 0 if k_size <= 3: sigma = Random.random_choice_list(sigmas) img = cv2.GaussianBlur(img, (k_size, k_size), sigma) return img