| 285 | |
| 286 | |
| 287 | class RandomSizedEarser(object): |
| 288 | def __init__(self, sl=0.02, sh=0.4, asratio=0.3, p=0.5): |
| 289 | self.sl = sl |
| 290 | self.sh = sh |
| 291 | self.asratio = asratio |
| 292 | self.p = p |
| 293 | |
| 294 | def __call__(self, img): |
| 295 | p1 = random.uniform(-1, 1.0) |
| 296 | H = img.size[0] |
| 297 | W = img.size[1] |
| 298 | area = H * W |
| 299 | |
| 300 | if p1 > self.p: |
| 301 | return img |
| 302 | else: |
| 303 | gen = True |
| 304 | while gen: |
| 305 | Se = random.uniform(self.sl, self.sh)*area |
| 306 | re = random.uniform(self.asratio, 1/self.asratio) |
| 307 | He = np.sqrt(Se*re) |
| 308 | We = np.sqrt(Se/re) |
| 309 | xe = random.uniform(0, W-We) |
| 310 | ye = random.uniform(0, H-He) |
| 311 | if xe+We <= W and ye+He <= H and xe>0 and ye>0: |
| 312 | x1 = int(np.ceil(xe)) |
| 313 | y1 = int(np.ceil(ye)) |
| 314 | x2 = int(np.floor(x1+We)) |
| 315 | y2 = int(np.floor(y1+He)) |
| 316 | part1 = img.crop((x1, y1, x2, y2)) |
| 317 | Rc = random.randint(0, 255) |
| 318 | Gc = random.randint(0, 255) |
| 319 | Bc = random.randint(0, 255) |
| 320 | I = Image.new('RGB', part1.size, (Rc, Gc, Bc)) |
| 321 | img.paste(I, (x1, y1)) |
| 322 | return img |
| 323 | |
| 324 | # class RandomSizedEarserCV2(object): |
| 325 | # def __init__(self, sl=0.02, sh=0.2, asratio=0.3, p=0.8): |