davidvgilmore commited on
Commit
f277092
·
verified ·
1 Parent(s): 78e5430

Upload hy3dgen/texgen/utils/dehighlight_utils.py with huggingface_hub

Browse files
hy3dgen/texgen/utils/dehighlight_utils.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Open Source Model Licensed under the Apache License Version 2.0
2
+ # and Other Licenses of the Third-Party Components therein:
3
+ # The below Model in this distribution may have been modified by THL A29 Limited
4
+ # ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
5
+
6
+ # Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
7
+ # The below software and/or models in this distribution may have been
8
+ # modified by THL A29 Limited ("Tencent Modifications").
9
+ # All Tencent Modifications are Copyright (C) THL A29 Limited.
10
+
11
+ # Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
12
+ # except for the third-party components listed below.
13
+ # Hunyuan 3D does not impose any additional limitations beyond what is outlined
14
+ # in the repsective licenses of these third-party components.
15
+ # Users must comply with all terms and conditions of original licenses of these third-party
16
+ # components and must ensure that the usage of the third party components adheres to
17
+ # all relevant laws and regulations.
18
+
19
+ # For avoidance of doubts, Hunyuan 3D means the large language models and
20
+ # their software and algorithms, including trained model weights, parameters (including
21
+ # optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
22
+ # fine-tuning enabling code and other elements of the foregoing made publicly available
23
+ # by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
24
+
25
+ import cv2
26
+ import numpy as np
27
+ import torch
28
+ from PIL import Image
29
+ from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDiscreteScheduler
30
+
31
+
32
+ class Light_Shadow_Remover():
33
+ def __init__(self, config):
34
+ self.device = config.device
35
+ self.cfg_image = 1.5
36
+ self.cfg_text = 1.0
37
+
38
+ pipeline = StableDiffusionInstructPix2PixPipeline.from_pretrained(
39
+ config.light_remover_ckpt_path,
40
+ torch_dtype=torch.float16,
41
+ safety_checker=None,
42
+ )
43
+ pipeline.scheduler = EulerAncestralDiscreteScheduler.from_config(pipeline.scheduler.config)
44
+ pipeline.set_progress_bar_config(disable=True)
45
+
46
+ self.pipeline = pipeline.to(self.device, torch.float16)
47
+
48
+ @torch.no_grad()
49
+ def __call__(self, image):
50
+
51
+ image = image.resize((512, 512))
52
+
53
+ if image.mode == 'RGBA':
54
+ image_array = np.array(image)
55
+ alpha_channel = image_array[:, :, 3]
56
+ erosion_size = 3
57
+ kernel = np.ones((erosion_size, erosion_size), np.uint8)
58
+ alpha_channel = cv2.erode(alpha_channel, kernel, iterations=1)
59
+ image_array[alpha_channel == 0, :3] = 255
60
+ image_array[:, :, 3] = alpha_channel
61
+ image = Image.fromarray(image_array)
62
+
63
+ image_tensor = torch.tensor(np.array(image) / 255.0).to(self.device)
64
+ alpha = image_tensor[:, :, 3:]
65
+ rgb_target = image_tensor[:, :, :3]
66
+ else:
67
+ image_tensor = torch.tensor(np.array(image) / 255.0).to(self.device)
68
+ alpha = torch.ones_like(image_tensor)[:, :, :1]
69
+ rgb_target = image_tensor[:, :, :3]
70
+
71
+ image = image.convert('RGB')
72
+
73
+ image = self.pipeline(
74
+ prompt="",
75
+ image=image,
76
+ generator=torch.manual_seed(42),
77
+ height=512,
78
+ width=512,
79
+ num_inference_steps=50,
80
+ image_guidance_scale=self.cfg_image,
81
+ guidance_scale=self.cfg_text,
82
+ ).images[0]
83
+
84
+ return image