jhj0517 commited on
Commit
f195437
1 Parent(s): cfa5142

Add mask utils

Browse files
Files changed (1) hide show
  1. modules/mask_utils.py +102 -0
modules/mask_utils.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from typing import Dict, List
4
+ from pycocotools import mask as coco_mask
5
+ from pytoshop import layers
6
+ import pytoshop
7
+ from pytoshop.enums import BlendMode
8
+
9
+
10
+ def generate_random_color():
11
+ return np.random.randint(0, 256), np.random.randint(0, 256), np.random.randint(0, 256)
12
+
13
+
14
+ def create_base_layer(image):
15
+ rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
16
+ return [rgba_image]
17
+
18
+
19
+ def create_mask_layers(image, masks):
20
+ layer_list = []
21
+
22
+ for result in masks:
23
+ rle = result['segmentation']
24
+ mask = coco_mask.decode(rle).astype(np.uint8)
25
+ rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
26
+ rgba_image[..., 3] = cv2.bitwise_and(rgba_image[..., 3], rgba_image[..., 3], mask=mask)
27
+
28
+ layer_list.append(rgba_image)
29
+
30
+ return layer_list
31
+
32
+
33
+ def create_mask_gallery(image, masks):
34
+ mask_array_list = []
35
+ label_list = []
36
+
37
+ for index, result in enumerate(masks):
38
+ rle = result['segmentation']
39
+ mask = coco_mask.decode(rle).astype(np.uint8)
40
+
41
+ rgba_image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
42
+ rgba_image[..., 3] = cv2.bitwise_and(rgba_image[..., 3], rgba_image[..., 3], mask=mask)
43
+
44
+ mask_array_list.append(rgba_image)
45
+ label_list.append(f'Part {index}')
46
+
47
+ return [[img, label] for img, label in zip(mask_array_list, label_list)]
48
+
49
+
50
+ def create_mask_combined_images(image, masks):
51
+ final_result = np.zeros_like(image)
52
+
53
+ for result in masks:
54
+ rle = result['segmentation']
55
+ mask = coco_mask.decode(rle).astype(np.uint8)
56
+
57
+ color = generate_random_color()
58
+ colored_mask = np.zeros_like(image)
59
+ colored_mask[mask == 1] = color
60
+
61
+ final_result = cv2.addWeighted(final_result, 1, colored_mask, 0.5, 0)
62
+
63
+ combined_image = cv2.addWeighted(image, 1, final_result, 0.5, 0)
64
+ return [combined_image, "masked"]
65
+
66
+
67
+ def insert_psd_layer(psd, image_data, layer_name, blending_mode):
68
+ channel_data = [layers.ChannelImageData(image=image_data[:, :, i], compression=1) for i in range(4)]
69
+
70
+ layer_record = layers.LayerRecord(
71
+ channels={-1: channel_data[3], 0: channel_data[0], 1: channel_data[1], 2: channel_data[2]},
72
+ top=0, bottom=image_data.shape[0], left=0, right=image_data.shape[1],
73
+ blend_mode=blending_mode,
74
+ name=layer_name,
75
+ opacity=255,
76
+ )
77
+ psd.layer_and_mask_info.layer_info.layer_records.append(layer_record)
78
+ return psd
79
+
80
+
81
+ def save_psd(input_image_data, layer_data, layer_names, blending_modes, output_path):
82
+ psd_file = pytoshop.core.PsdFile(num_channels=3, height=input_image_data.shape[0], width=input_image_data.shape[1])
83
+ psd_file.layer_and_mask_info.layer_info.layer_records.clear()
84
+
85
+ for index, layer in enumerate(layer_data):
86
+ psd_file = insert_psd_layer(psd_file, layer, layer_names[index], blending_modes[index])
87
+
88
+ with open(output_path, 'wb') as output_file:
89
+ psd_file.write(output_file)
90
+
91
+
92
+ def save_psd_with_masks(
93
+ image: np.ndarray,
94
+ masks: Dict,
95
+ output_path: str
96
+ ):
97
+ original_layer = create_base_layer(image)
98
+ mask_layers = create_mask_layers(image, masks)
99
+ names = [f'Part {i}' for i in range(len(mask_layers))]
100
+ modes = [BlendMode.normal] * (len(mask_layers)+1)
101
+ save_psd(image, original_layer+mask_layers, ['Original_Image']+names, modes, output_path)
102
+