Spaces:
Running
Running
jhj0517
commited on
Commit
•
237a214
1
Parent(s):
a2c5114
Add docstring
Browse files- modules/mask_utils.py +102 -14
modules/mask_utils.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
-
from typing import Dict, List
|
4 |
import colorsys
|
5 |
from pytoshop import layers
|
6 |
from pytoshop.enums import BlendMode
|
@@ -10,14 +10,15 @@ from modules.constants import DEFAULT_COLOR, DEFAULT_PIXEL_SIZE
|
|
10 |
|
11 |
|
12 |
def decode_to_mask(seg: np.ndarray[np.bool_] | np.ndarray[np.uint8]) -> np.ndarray[np.uint8]:
|
13 |
-
|
14 |
if isinstance(seg, np.ndarray) and seg.dtype == np.bool_:
|
15 |
return seg.astype(np.uint8) * 255
|
16 |
else:
|
17 |
return seg.astype(np.uint8)
|
18 |
|
19 |
|
20 |
-
def generate_random_color():
|
|
|
21 |
h = np.random.randint(0, 360)
|
22 |
s = np.random.randint(70, 100) / 100
|
23 |
v = np.random.randint(70, 100) / 100
|
@@ -25,15 +26,26 @@ def generate_random_color():
|
|
25 |
return int(r * 255), int(g * 255), int(b * 255)
|
26 |
|
27 |
|
28 |
-
def create_base_layer(image: np.ndarray):
|
|
|
29 |
rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
|
30 |
return [rgba_image]
|
31 |
|
32 |
|
33 |
def create_mask_layers(
|
34 |
image: np.ndarray,
|
35 |
-
masks: List
|
36 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
layer_list = []
|
38 |
|
39 |
sorted_masks = sorted(masks, key=lambda x: x['area'], reverse=True)
|
@@ -52,8 +64,19 @@ def create_mask_layers(
|
|
52 |
|
53 |
def create_mask_gallery(
|
54 |
image: np.ndarray,
|
55 |
-
masks: List
|
56 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
mask_array_list = []
|
58 |
label_list = []
|
59 |
|
@@ -74,8 +97,18 @@ def create_mask_gallery(
|
|
74 |
|
75 |
def create_mask_combined_images(
|
76 |
image: np.ndarray,
|
77 |
-
masks: List
|
78 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
final_result = np.zeros_like(image)
|
80 |
used_colors = set()
|
81 |
|
@@ -106,9 +139,21 @@ def create_mask_combined_images(
|
|
106 |
|
107 |
def create_mask_pixelized_image(
|
108 |
image: np.ndarray,
|
109 |
-
masks: List,
|
110 |
pixel_size: int = DEFAULT_PIXEL_SIZE
|
111 |
) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
final_result = image.copy()
|
113 |
|
114 |
def pixelize(img: np.ndarray, mask: np.ndarray[np.uint8], pixel_size: int):
|
@@ -132,9 +177,20 @@ def create_mask_pixelized_image(
|
|
132 |
|
133 |
def create_solid_color_mask_image(
|
134 |
image: np.ndarray,
|
135 |
-
masks: List,
|
136 |
color_hex: str = DEFAULT_COLOR
|
137 |
) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
final_result = image.copy()
|
139 |
|
140 |
def hex_to_bgr(hex_color: str):
|
@@ -160,7 +216,20 @@ def insert_psd_layer(
|
|
160 |
image_data: np.ndarray,
|
161 |
layer_name: str,
|
162 |
blending_mode: BlendMode
|
163 |
-
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
channel_data = [layers.ChannelImageData(image=image_data[:, :, i], compression=1) for i in range(4)]
|
165 |
|
166 |
layer_record = layers.LayerRecord(
|
@@ -181,6 +250,17 @@ def save_psd(
|
|
181 |
blending_modes: List,
|
182 |
output_path: str
|
183 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
psd_file = PsdFile(num_channels=3, height=input_image_data.shape[0], width=input_image_data.shape[1])
|
185 |
psd_file.layer_and_mask_info.layer_info.layer_records.clear()
|
186 |
|
@@ -193,9 +273,17 @@ def save_psd(
|
|
193 |
|
194 |
def save_psd_with_masks(
|
195 |
image: np.ndarray,
|
196 |
-
masks: List,
|
197 |
output_path: str
|
198 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
original_layer = create_base_layer(image)
|
200 |
mask_layers = create_mask_layers(image, masks)
|
201 |
names = [f'Part {i}' for i in range(len(mask_layers))]
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
from typing import Dict, List, Tuple
|
4 |
import colorsys
|
5 |
from pytoshop import layers
|
6 |
from pytoshop.enums import BlendMode
|
|
|
10 |
|
11 |
|
12 |
def decode_to_mask(seg: np.ndarray[np.bool_] | np.ndarray[np.uint8]) -> np.ndarray[np.uint8]:
|
13 |
+
"""Decode to uint8 mask from bool to deal with as images"""
|
14 |
if isinstance(seg, np.ndarray) and seg.dtype == np.bool_:
|
15 |
return seg.astype(np.uint8) * 255
|
16 |
else:
|
17 |
return seg.astype(np.uint8)
|
18 |
|
19 |
|
20 |
+
def generate_random_color() -> Tuple[int, int, int]:
|
21 |
+
"""Generate random color in RGB format"""
|
22 |
h = np.random.randint(0, 360)
|
23 |
s = np.random.randint(70, 100) / 100
|
24 |
v = np.random.randint(70, 100) / 100
|
|
|
26 |
return int(r * 255), int(g * 255), int(b * 255)
|
27 |
|
28 |
|
29 |
+
def create_base_layer(image: np.ndarray) -> List[np.ndarray]:
|
30 |
+
"""Create a base layer from the image. Used to keep original image"""
|
31 |
rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
|
32 |
return [rgba_image]
|
33 |
|
34 |
|
35 |
def create_mask_layers(
|
36 |
image: np.ndarray,
|
37 |
+
masks: List[Dict]
|
38 |
+
) -> List[np.ndarray]:
|
39 |
+
"""
|
40 |
+
Create list of images with mask data. Masks are sorted by area in descending order.
|
41 |
+
|
42 |
+
Args:
|
43 |
+
image: Original image
|
44 |
+
masks: List of mask data
|
45 |
+
|
46 |
+
Returns:
|
47 |
+
List of RGBA images
|
48 |
+
"""
|
49 |
layer_list = []
|
50 |
|
51 |
sorted_masks = sorted(masks, key=lambda x: x['area'], reverse=True)
|
|
|
64 |
|
65 |
def create_mask_gallery(
|
66 |
image: np.ndarray,
|
67 |
+
masks: List[Dict]
|
68 |
+
) -> List[List[np.ndarray, str]]:
|
69 |
+
"""
|
70 |
+
Create list of images with mask data. Masks are sorted by area in descending order. Specially used for gradio
|
71 |
+
Gallery component. each element has image and label, where label is the part number.
|
72 |
+
|
73 |
+
Args:
|
74 |
+
image: Original image
|
75 |
+
masks: List of mask data
|
76 |
+
|
77 |
+
Returns:
|
78 |
+
List of [image, label] pairs
|
79 |
+
"""
|
80 |
mask_array_list = []
|
81 |
label_list = []
|
82 |
|
|
|
97 |
|
98 |
def create_mask_combined_images(
|
99 |
image: np.ndarray,
|
100 |
+
masks: List[Dict]
|
101 |
+
) -> List[np.ndarray, str]:
|
102 |
+
"""
|
103 |
+
Create an image with colored masks. Each mask is colored with a random color and blended with the original image.
|
104 |
+
|
105 |
+
Args:
|
106 |
+
image: Original image
|
107 |
+
masks: List of mask data
|
108 |
+
|
109 |
+
Returns:
|
110 |
+
List of [image, label] pairs
|
111 |
+
"""
|
112 |
final_result = np.zeros_like(image)
|
113 |
used_colors = set()
|
114 |
|
|
|
139 |
|
140 |
def create_mask_pixelized_image(
|
141 |
image: np.ndarray,
|
142 |
+
masks: List[Dict],
|
143 |
pixel_size: int = DEFAULT_PIXEL_SIZE
|
144 |
) -> np.ndarray:
|
145 |
+
"""
|
146 |
+
Create a pixelized image with mask.
|
147 |
+
|
148 |
+
Args:
|
149 |
+
image: Original image
|
150 |
+
masks: List of mask data
|
151 |
+
pixel_size: Pixel size for pixelization
|
152 |
+
|
153 |
+
Returns:
|
154 |
+
Pixelized image
|
155 |
+
"""
|
156 |
+
|
157 |
final_result = image.copy()
|
158 |
|
159 |
def pixelize(img: np.ndarray, mask: np.ndarray[np.uint8], pixel_size: int):
|
|
|
177 |
|
178 |
def create_solid_color_mask_image(
|
179 |
image: np.ndarray,
|
180 |
+
masks: List[Dict],
|
181 |
color_hex: str = DEFAULT_COLOR
|
182 |
) -> np.ndarray:
|
183 |
+
"""
|
184 |
+
Create an image with solid color masks.
|
185 |
+
|
186 |
+
Args:
|
187 |
+
image: Original image
|
188 |
+
masks: List of mask data
|
189 |
+
color_hex: Hex color code
|
190 |
+
|
191 |
+
Returns:
|
192 |
+
Image with solid color masks
|
193 |
+
"""
|
194 |
final_result = image.copy()
|
195 |
|
196 |
def hex_to_bgr(hex_color: str):
|
|
|
216 |
image_data: np.ndarray,
|
217 |
layer_name: str,
|
218 |
blending_mode: BlendMode
|
219 |
+
) -> PsdFile:
|
220 |
+
"""
|
221 |
+
Insert a layer into the PSD file using pytoshop
|
222 |
+
|
223 |
+
Args:
|
224 |
+
psd: PSD file object from the pytoshop
|
225 |
+
image_data: Image data
|
226 |
+
layer_name: Layer name
|
227 |
+
blending_mode: Blending mode from pytoshop
|
228 |
+
|
229 |
+
Returns:
|
230 |
+
Updated PSD file object
|
231 |
+
"""
|
232 |
+
|
233 |
channel_data = [layers.ChannelImageData(image=image_data[:, :, i], compression=1) for i in range(4)]
|
234 |
|
235 |
layer_record = layers.LayerRecord(
|
|
|
250 |
blending_modes: List,
|
251 |
output_path: str
|
252 |
):
|
253 |
+
"""
|
254 |
+
Save the image with multiple layers as a PSD file
|
255 |
+
|
256 |
+
Args:
|
257 |
+
input_image_data: Original image data
|
258 |
+
layer_data: List of images to be saved as layers
|
259 |
+
layer_names: List of layer names
|
260 |
+
blending_modes: List of blending modes
|
261 |
+
output_path: Output path for the PSD file
|
262 |
+
"""
|
263 |
+
|
264 |
psd_file = PsdFile(num_channels=3, height=input_image_data.shape[0], width=input_image_data.shape[1])
|
265 |
psd_file.layer_and_mask_info.layer_info.layer_records.clear()
|
266 |
|
|
|
273 |
|
274 |
def save_psd_with_masks(
|
275 |
image: np.ndarray,
|
276 |
+
masks: List[Dict],
|
277 |
output_path: str
|
278 |
):
|
279 |
+
"""
|
280 |
+
Save the psd file with masks data.
|
281 |
+
|
282 |
+
Args:
|
283 |
+
image: Original image
|
284 |
+
masks: List of mask data
|
285 |
+
output_path: Output path for the PSD file
|
286 |
+
"""
|
287 |
original_layer = create_base_layer(image)
|
288 |
mask_layers = create_mask_layers(image, masks)
|
289 |
names = [f'Part {i}' for i in range(len(mask_layers))]
|