(img_pils, new_bkgd=(0., 0., 0.), device="cuda")
| 300 | return input_image |
| 301 | |
| 302 | def init_target(img_pils, new_bkgd=(0., 0., 0.), device="cuda"): |
| 303 | # Convert the background color to a PyTorch tensor |
| 304 | new_bkgd = torch.tensor(new_bkgd, dtype=torch.float32).view(1, 1, 3).to(device) |
| 305 | |
| 306 | # Convert all images to PyTorch tensors and process them |
| 307 | imgs = torch.stack([torch.from_numpy(np.array(img, dtype=np.float32)) for img in img_pils]).to(device) / 255 |
| 308 | img_nps = imgs[..., :3] |
| 309 | alpha_nps = imgs[..., 3] |
| 310 | ori_bkgds = img_nps[:, :1, :1] |
| 311 | |
| 312 | # Avoid divide by zero and calculate the original image |
| 313 | alpha_nps_clamp = torch.clamp(alpha_nps, 1e-6, 1) |
| 314 | ori_img_nps = (img_nps - ori_bkgds * (1 - alpha_nps.unsqueeze(-1))) / alpha_nps_clamp.unsqueeze(-1) |
| 315 | ori_img_nps = torch.clamp(ori_img_nps, 0, 1) |
| 316 | img_nps = torch.where(alpha_nps.unsqueeze(-1) > 0.05, ori_img_nps * alpha_nps.unsqueeze(-1) + new_bkgd * (1 - alpha_nps.unsqueeze(-1)), new_bkgd) |
| 317 | |
| 318 | rgba_img_np = torch.cat([img_nps, alpha_nps.unsqueeze(-1)], dim=-1) |
| 319 | return rgba_img_np |
no outgoing calls
no test coverage detected