def gen_noise_samples(self, imgwh, noise_type, num, **kwargs): center_xy = self.xy + self.wh * 0.5 mean_wh = sum(self.wh) / 2.0 gaussian_translation_f = kwargs.get('gaussian_translation_f', 0.1) uniform_translation_f = kwargs.get('uniform_translation_f', 1) uniform_scale_f = kwargs.get('uniform_scale_f', 10) samples = [] if noise_type == 'whole': #print('{}, {}, {}, {}'.format(imgwh.x, imgwh.y, self.wh.x, self.wh.y)) grid_x = range(self.wh.x // 2, imgwh.x - self.wh.x // 2, self.wh.x // 5) grid_y = range(self.wh.y // 2, imgwh.y - self.wh.y // 2, self.wh.y // 5) samples_tmp = [] #print(grid_x, ":", grid_y) for dx, dy, ds in itertools.product(grid_x, grid_y, range(-5, 5, 1)): box = BoundingBox(dx, dy, self.wh.x * (1.05**ds), self.wh.y * (1.05**ds)) box.fit_image(imgwh) samples_tmp.append(box) #print('len samples_temp: ', len(samples_tmp)) for _ in range(num): samples.append(random.choice(samples_tmp)) else: for _ in range(num): if noise_type == 'gaussian': dx = gaussian_translation_f * mean_wh * minmax( 0.5 * random.normalvariate(0, 1), -1, 1) dy = gaussian_translation_f * mean_wh * minmax( 0.5 * random.normalvariate(0, 1), -1, 1) dwh = 1.05**( 3 * minmax(0.5 * random.normalvariate(0, 1), -1, 1)) elif noise_type == 'uniform': dx = uniform_translation_f * mean_wh * random.uniform( -1.0, 1.0) dy = uniform_translation_f * mean_wh * random.uniform( -1.0, 1.0) dwh = 1.05**(uniform_scale_f * random.uniform(-1.0, 1.0)) else: raise new_cxy = center_xy + (dx, dy) new_wh = self.wh * dwh box = BoundingBox(new_cxy.x - new_wh.x / 2.0, new_cxy.y - new_wh.y / 2.0, new_wh.x, new_wh.y) box.fit_image(imgwh) samples.append(box) return samples
def fit_image(self, imgwh, margin=0): # Make sure the bbox is within the imgwh # margin is the max padding size if not isinstance(imgwh, Coordinate): imgwh = Coordinate(*imgwh) x = minmax(self.xy.x, -margin/2, imgwh.x-margin/2) y = minmax(self.xy.y, -margin/2, imgwh.y-margin/2) w = minmax(self.wh.x, margin, imgwh.x+margin/2-x) h = minmax(self.wh.y, margin, imgwh.y+margin/2-y) return BoundingBox(x,y,w,h)