| 268 | self.hat_ratio = hat_ratio |
| 269 | |
| 270 | def __call__(self, img): |
| 271 | is_sunglass = False |
| 272 | is_hat = False |
| 273 | is_mask = False |
| 274 | print_flag = False |
| 275 | |
| 276 | if self.mask_spec == True: |
| 277 | LEx = 70.7 |
| 278 | LEy = 113.0 |
| 279 | REx = 108.23 |
| 280 | REy = 113.0 |
| 281 | Mx = 89.43 |
| 282 | My = 153.51 |
| 283 | |
| 284 | totaltemprand = np.random.rand() |
| 285 | if totaltemprand < self.total_ratio: |
| 286 | temprand = np.random.rand() |
| 287 | |
| 288 | workpath = os.path.abspath('.') |
| 289 | |
| 290 | if self.sunglass > 0.0 and temprand <= self.sunglass_ratio: |
| 291 | radious = 15 + np.random.rand()*15 |
| 292 | #radious = self.final_size/2.0/4.0 + np.random.rand()*(self.final_size/2.0/4.0/2.0) |
| 293 | cv2.circle(img,(int(LEx),int(LEy)),int(radious),(0,0,0),-1) |
| 294 | cv2.circle(img,(int(REx),int(REy)),int(radious),(0,0,0),-1) |
| 295 | if print_flag: |
| 296 | # cv2.imwrite('sunglasses.jpg',img) |
| 297 | is_sunglass = True |
| 298 | |
| 299 | elif self.hat>0.0 and (temprand - self.sunglass_ratio) <= self.hat_ratio: |
| 300 | dirpath = os.path.join(workpath, 'mask_templates/hat/') |
| 301 | hatpaths = os.listdir(dirpath) |
| 302 | hatpath = dirpath + random.sample(hatpaths,1)[0] |
| 303 | hat = Image.open(hatpath) |
| 304 | t_width = hat.width |
| 305 | t_height = hat.height |
| 306 | totalx = 0.0 |
| 307 | totaly = 0.0 |
| 308 | count = 1 |
| 309 | r,g,b,alpha = hat.split() |
| 310 | for y in range(t_height): |
| 311 | for x in range(t_width): |
| 312 | pixel = alpha.getpixel((x,y)) |
| 313 | if pixel > 0: |
| 314 | totalx = totalx + x |
| 315 | totaly = totaly + y |
| 316 | count = count + 1 |
| 317 | avrx = int(totalx/count) |
| 318 | avry = int(totaly/count) |
| 319 | gap = 89 - avrx |
| 320 | xstart = int(0 + gap) |
| 321 | ystart = int(np.random.rand()*20) |
| 322 | xend = xstart + 178 |
| 323 | yend = ystart + 218 |
| 324 | tmpimg = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB)) |
| 325 | tmpimg.paste(hat,(xstart,ystart,xend,yend),mask=alpha) |
| 326 | img = cv2.cvtColor(np.array(tmpimg),cv2.COLOR_RGB2BGR) |
| 327 | if print_flag: |