Spaces:
Runtime error
Runtime error
Upload hy3dgen/shapegen/preprocessors.py with huggingface_hub
Browse files
hy3dgen/shapegen/preprocessors.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
|
6 |
+
# The below software and/or models in this distribution may have been
|
7 |
+
# modified by THL A29 Limited ("Tencent Modifications").
|
8 |
+
# All Tencent Modifications are Copyright (C) THL A29 Limited.
|
9 |
+
|
10 |
+
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
11 |
+
# except for the third-party components listed below.
|
12 |
+
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
13 |
+
# in the repsective licenses of these third-party components.
|
14 |
+
# Users must comply with all terms and conditions of original licenses of these third-party
|
15 |
+
# components and must ensure that the usage of the third party components adheres to
|
16 |
+
# all relevant laws and regulations.
|
17 |
+
|
18 |
+
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
19 |
+
# their software and algorithms, including trained model weights, parameters (including
|
20 |
+
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
21 |
+
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
22 |
+
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
23 |
+
|
24 |
+
import cv2
|
25 |
+
import numpy as np
|
26 |
+
import torch
|
27 |
+
from PIL import Image
|
28 |
+
from einops import repeat, rearrange
|
29 |
+
|
30 |
+
|
31 |
+
def array_to_tensor(np_array):
|
32 |
+
image_pt = torch.tensor(np_array).float()
|
33 |
+
image_pt = image_pt / 255 * 2 - 1
|
34 |
+
image_pt = rearrange(image_pt, "h w c -> c h w")
|
35 |
+
image_pts = repeat(image_pt, "c h w -> b c h w", b=1)
|
36 |
+
return image_pts
|
37 |
+
|
38 |
+
|
39 |
+
class ImageProcessorV2:
|
40 |
+
def __init__(self, size=512, border_ratio=None):
|
41 |
+
self.size = size
|
42 |
+
self.border_ratio = border_ratio
|
43 |
+
|
44 |
+
@staticmethod
|
45 |
+
def recenter(image, border_ratio: float = 0.2):
|
46 |
+
""" recenter an image to leave some empty space at the image border.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
image (ndarray): input image, float/uint8 [H, W, 3/4]
|
50 |
+
mask (ndarray): alpha mask, bool [H, W]
|
51 |
+
border_ratio (float, optional): border ratio, image will be resized to (1 - border_ratio). Defaults to 0.2.
|
52 |
+
|
53 |
+
Returns:
|
54 |
+
ndarray: output image, float/uint8 [H, W, 3/4]
|
55 |
+
"""
|
56 |
+
|
57 |
+
if image.shape[-1] == 4:
|
58 |
+
mask = image[..., 3]
|
59 |
+
else:
|
60 |
+
mask = np.ones_like(image[..., 0:1]) * 255
|
61 |
+
image = np.concatenate([image, mask], axis=-1)
|
62 |
+
mask = mask[..., 0]
|
63 |
+
|
64 |
+
H, W, C = image.shape
|
65 |
+
|
66 |
+
size = max(H, W)
|
67 |
+
result = np.zeros((size, size, C), dtype=np.uint8)
|
68 |
+
|
69 |
+
coords = np.nonzero(mask)
|
70 |
+
x_min, x_max = coords[0].min(), coords[0].max()
|
71 |
+
y_min, y_max = coords[1].min(), coords[1].max()
|
72 |
+
h = x_max - x_min
|
73 |
+
w = y_max - y_min
|
74 |
+
if h == 0 or w == 0:
|
75 |
+
raise ValueError('input image is empty')
|
76 |
+
desired_size = int(size * (1 - border_ratio))
|
77 |
+
scale = desired_size / max(h, w)
|
78 |
+
h2 = int(h * scale)
|
79 |
+
w2 = int(w * scale)
|
80 |
+
x2_min = (size - h2) // 2
|
81 |
+
x2_max = x2_min + h2
|
82 |
+
|
83 |
+
y2_min = (size - w2) // 2
|
84 |
+
y2_max = y2_min + w2
|
85 |
+
|
86 |
+
result[x2_min:x2_max, y2_min:y2_max] = cv2.resize(image[x_min:x_max, y_min:y_max], (w2, h2),
|
87 |
+
interpolation=cv2.INTER_AREA)
|
88 |
+
|
89 |
+
bg = np.ones((result.shape[0], result.shape[1], 3), dtype=np.uint8) * 255
|
90 |
+
# bg = np.zeros((result.shape[0], result.shape[1], 3), dtype=np.uint8) * 255
|
91 |
+
mask = result[..., 3:].astype(np.float32) / 255
|
92 |
+
result = result[..., :3] * mask + bg * (1 - mask)
|
93 |
+
|
94 |
+
mask = mask * 255
|
95 |
+
result = result.clip(0, 255).astype(np.uint8)
|
96 |
+
mask = mask.clip(0, 255).astype(np.uint8)
|
97 |
+
return result, mask
|
98 |
+
|
99 |
+
def __call__(self, image, border_ratio=0.15, to_tensor=True, return_mask=False, **kwargs):
|
100 |
+
if self.border_ratio is not None:
|
101 |
+
border_ratio = self.border_ratio
|
102 |
+
print(f"Using border_ratio from init: {border_ratio}")
|
103 |
+
if isinstance(image, str):
|
104 |
+
image = cv2.imread(image, cv2.IMREAD_UNCHANGED)
|
105 |
+
image, mask = self.recenter(image, border_ratio=border_ratio)
|
106 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
107 |
+
elif isinstance(image, Image.Image):
|
108 |
+
image = np.asarray(image)
|
109 |
+
image, mask = self.recenter(image, border_ratio=border_ratio)
|
110 |
+
|
111 |
+
image = cv2.resize(image, (self.size, self.size), interpolation=cv2.INTER_CUBIC)
|
112 |
+
mask = cv2.resize(mask, (self.size, self.size), interpolation=cv2.INTER_NEAREST)
|
113 |
+
mask = mask[..., np.newaxis]
|
114 |
+
|
115 |
+
if to_tensor:
|
116 |
+
image = array_to_tensor(image)
|
117 |
+
mask = array_to_tensor(mask)
|
118 |
+
if return_mask:
|
119 |
+
return image, mask
|
120 |
+
return image
|
121 |
+
|
122 |
+
|
123 |
+
IMAGE_PROCESSORS = {
|
124 |
+
"v2": ImageProcessorV2,
|
125 |
+
}
|
126 |
+
|
127 |
+
DEFAULT_IMAGEPROCESSOR = 'v2'
|