File size: 8,262 Bytes
f195437
 
237a214
dfc0c1a
f195437
 
e00f944
 
60def5b
 
e00f944
3810a85
237a214
3810a85
 
 
 
f195437
 
237a214
 
dfc0c1a
 
 
 
 
f195437
 
237a214
 
f195437
 
 
 
e00f944
 
237a214
 
 
 
 
 
 
 
 
 
 
 
f195437
 
e00f944
 
 
 
 
 
f195437
 
 
 
 
 
 
 
e00f944
 
237a214
3d1dda1
237a214
 
 
 
 
 
 
 
 
 
 
f195437
 
 
e00f944
 
 
 
 
f195437
 
 
 
 
 
 
 
 
 
e00f944
 
237a214
3d1dda1
237a214
 
 
 
 
 
 
 
3d1dda1
237a214
f195437
dfc0c1a
f195437
e00f944
 
 
f195437
dfc0c1a
 
 
 
 
 
f195437
dfc0c1a
 
 
 
 
 
f195437
dfc0c1a
 
 
f195437
dfc0c1a
f195437
 
60def5b
 
237a214
60def5b
1c3a875
237a214
 
 
 
 
 
 
 
 
 
 
 
60def5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237a214
60def5b
1c3a875
237a214
 
 
 
 
 
 
 
 
 
 
60def5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e00f944
 
 
 
 
237a214
 
 
 
 
 
 
 
 
 
 
 
 
 
f195437
 
 
 
 
 
 
 
 
 
 
 
 
e00f944
 
 
 
 
 
 
237a214
 
 
 
 
 
 
 
 
 
 
e00f944
f195437
 
 
 
 
 
 
 
 
 
 
237a214
f195437
 
237a214
 
 
 
 
 
 
 
f195437
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import cv2
import numpy as np
from typing import Dict, List, Tuple
import colorsys
from pytoshop import layers
from pytoshop.enums import BlendMode
from pytoshop.core import PsdFile

from modules.constants import DEFAULT_COLOR, DEFAULT_PIXEL_SIZE


def decode_to_mask(seg: np.ndarray[np.bool_] | np.ndarray[np.uint8]) -> np.ndarray[np.uint8]:
    """Decode to uint8 mask from bool to deal with as images"""
    if isinstance(seg, np.ndarray) and seg.dtype == np.bool_:
        return seg.astype(np.uint8) * 255
    else:
        return seg.astype(np.uint8)


def generate_random_color() -> Tuple[int, int, int]:
    """Generate random color in RGB format"""
    h = np.random.randint(0, 360)
    s = np.random.randint(70, 100) / 100
    v = np.random.randint(70, 100) / 100
    r, g, b = colorsys.hsv_to_rgb(h/360, s, v)
    return int(r * 255), int(g * 255), int(b * 255)


def create_base_layer(image: np.ndarray) -> List[np.ndarray]:
    """Create a base layer from the image. Used to keep original image"""
    rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
    return [rgba_image]


def create_mask_layers(
    image: np.ndarray,
    masks: List[Dict]
) -> List[np.ndarray]:
    """
    Create list of images with mask data. Masks are sorted by area in descending order.

    Args:
        image: Original image
        masks: List of mask data

    Returns:
        List of RGBA images
    """
    layer_list = []

    sorted_masks = sorted(masks, key=lambda x: x['area'], reverse=True)

    for info in sorted_masks:
        rle = info['segmentation']
        mask = decode_to_mask(rle)

        rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
        rgba_image[..., 3] = cv2.bitwise_and(rgba_image[..., 3], rgba_image[..., 3], mask=mask)

        layer_list.append(rgba_image)

    return layer_list


def create_mask_gallery(
    image: np.ndarray,
    masks: List[Dict]
) -> List:
    """
    Create list of images with mask data. Masks are sorted by area in descending order. Specially used for gradio
    Gallery component. each element has image and label, where label is the part number.

    Args:
        image: Original image
        masks: List of mask data

    Returns:
        List of [image, label] pairs
    """
    mask_array_list = []
    label_list = []

    sorted_masks = sorted(masks, key=lambda x: x['area'], reverse=True)

    for index, info in enumerate(sorted_masks):
        rle = info['segmentation']
        mask = decode_to_mask(rle)

        rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
        rgba_image[..., 3] = cv2.bitwise_and(rgba_image[..., 3], rgba_image[..., 3], mask=mask)

        mask_array_list.append(rgba_image)
        label_list.append(f'Part {index}')

    return [[img, label] for img, label in zip(mask_array_list, label_list)]


def create_mask_combined_images(
    image: np.ndarray,
    masks: List[Dict]
) -> List:
    """
    Create an image with colored masks. Each mask is colored with a random color and blended with the original image.

    Args:
        image: Original image
        masks: List of mask data

    Returns:
        [image, label] pairs
    """
    final_result = np.zeros_like(image)
    used_colors = set()

    for info in masks:
        rle = info['segmentation']
        mask = decode_to_mask(rle)

        while True:
            color = generate_random_color()
            if color not in used_colors:
                used_colors.add(color)
                break

        colored_mask = np.zeros_like(image)
        colored_mask[mask > 0] = color

        blended = cv2.addWeighted(image, 0.3, colored_mask, 0.7, 0)
        final_result = np.where(mask[:, :, np.newaxis] > 0, blended, final_result)

    combined_image = np.where(final_result != 0, final_result, image)

    hsv = cv2.cvtColor(combined_image, cv2.COLOR_BGR2HSV)
    hsv[:, :, 1] = np.clip(hsv[:, :, 1] * 1.5, 0, 255)
    enhanced = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)

    return [enhanced, "Masked"]


def create_mask_pixelized_image(
    image: np.ndarray,
    masks: List[Dict],
    pixel_size: int = DEFAULT_PIXEL_SIZE
) -> np.ndarray:
    """
    Create a pixelized image with mask.

    Args:
        image: Original image
        masks: List of mask data
        pixel_size: Pixel size for pixelization

    Returns:
        Pixelized image
    """

    final_result = image.copy()

    def pixelize(img: np.ndarray, mask: np.ndarray[np.uint8], pixel_size: int):
        h, w = img.shape[:2]
        temp = cv2.resize(img, (w // pixel_size, h // pixel_size), interpolation=cv2.INTER_LINEAR)

        pixelated = cv2.resize(temp, (w, h), interpolation=cv2.INTER_NEAREST)

        return np.where(mask[:, :, np.newaxis] > 0, pixelated, img)

    for info in masks:
        rle = info['segmentation']
        mask = decode_to_mask(rle)

        pixelated_segment = pixelize(final_result, mask, pixel_size)

        final_result = np.where(mask[:, :, np.newaxis] > 0, pixelated_segment, final_result)

    return final_result


def create_solid_color_mask_image(
    image: np.ndarray,
    masks: List[Dict],
    color_hex: str = DEFAULT_COLOR
) -> np.ndarray:
    """
    Create an image with solid color masks.

    Args:
        image: Original image
        masks: List of mask data
        color_hex: Hex color code

    Returns:
        Image with solid color masks
    """
    final_result = image.copy()

    def hex_to_bgr(hex_color: str):
        hex_color = hex_color.lstrip('#')
        rgb = tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
        return rgb[::-1]

    color_bgr = hex_to_bgr(color_hex)

    for info in masks:
        rle = info['segmentation']
        mask = decode_to_mask(rle)

        solid_color_mask = np.full(image.shape, color_bgr, dtype=np.uint8)

        final_result = np.where(mask[:, :, np.newaxis] > 0, solid_color_mask, final_result)

    return final_result


def insert_psd_layer(
    psd: PsdFile,
    image_data: np.ndarray,
    layer_name: str,
    blending_mode: BlendMode
) -> PsdFile:
    """
    Insert a layer into the PSD file using pytoshop

    Args:
        psd: PSD file object from the pytoshop
        image_data: Image data
        layer_name: Layer name
        blending_mode: Blending mode from pytoshop

    Returns:
        Updated PSD file object
    """

    channel_data = [layers.ChannelImageData(image=image_data[:, :, i], compression=1) for i in range(4)]

    layer_record = layers.LayerRecord(
        channels={-1: channel_data[3], 0: channel_data[0], 1: channel_data[1], 2: channel_data[2]},
        top=0, bottom=image_data.shape[0], left=0, right=image_data.shape[1],
        blend_mode=blending_mode,
        name=layer_name,
        opacity=255,
    )
    psd.layer_and_mask_info.layer_info.layer_records.append(layer_record)
    return psd


def save_psd(
    input_image_data: np.ndarray,
    layer_data: List,
    layer_names: List,
    blending_modes: List,
    output_path: str
):
    """
    Save the image with multiple layers as a PSD file

    Args:
        input_image_data: Original image data
        layer_data: List of images to be saved as layers
        layer_names: List of layer names
        blending_modes: List of blending modes
        output_path: Output path for the PSD file
    """

    psd_file = PsdFile(num_channels=3, height=input_image_data.shape[0], width=input_image_data.shape[1])
    psd_file.layer_and_mask_info.layer_info.layer_records.clear()

    for index, layer in enumerate(layer_data):
        psd_file = insert_psd_layer(psd_file, layer, layer_names[index], blending_modes[index])

    with open(output_path, 'wb') as output_file:
        psd_file.write(output_file)


def save_psd_with_masks(
    image: np.ndarray,
    masks: List[Dict],
    output_path: str
):
    """
    Save the psd file with masks data.

    Args:
        image: Original image
        masks: List of mask data
        output_path: Output path for the PSD file
    """
    original_layer = create_base_layer(image)
    mask_layers = create_mask_layers(image, masks)
    names = [f'Part {i}' for i in range(len(mask_layers))]
    modes = [BlendMode.normal] * (len(mask_layers)+1)
    save_psd(image, original_layer+mask_layers, ['Original_Image']+names, modes, output_path)