MCPcopy Create free account
hub / github.com/AiuniAI/Unique3D / init_target

Function init_target

scripts/utils.py:302–319  ·  view source on GitHub ↗
(img_pils, new_bkgd=(0., 0., 0.), device="cuda")

Source from the content-addressed store, hash-verified

300 return input_image
301
302def 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

Callers 2

reconstruct_stage1Function · 0.90
run_mesh_refineFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected