-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_annotations.py
148 lines (108 loc) · 4.25 KB
/
create_annotations.py
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
import os
import glob
import cv2
from PIL import Image
import numpy as np
import h5py
from pdb import set_trace as st
from tqdm import *
datasets = 'meibo' #CVPPP
data_path = "/home/peterw/repo/instance-segmentation-pytorch/data/dataset/meibo/save_img/labeled_data/LL/train/glands"
raw_dir = "/home/peterw/repo/instance-segmentation-pytorch/data/dataset/meibo/raw_img/LL/train"
DATA_DIR = os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir, os.path.pardir))
ANN_DIR = os.path.join(DATA_DIR, 'raw', datasets, datasets+'2017_LSC_training',
'training', 'A1')
IMG_DIR = os.path.join(DATA_DIR, 'raw', datasets, datasets+'2017_LSC_training',
'training', 'A1')
subfolders = [ f.path for f in os.scandir(data_path) if f.is_dir() ]
SEMANTIC_OUTPUT_DIR = os.path.join(DATA_DIR, 'processed', datasets,
'semantic-annotations')
INSTANCE_OUTPUT_DIR = os.path.join(DATA_DIR, 'processed', datasets,
'instance-annotations')
#INSTANCE_OUTPUT_DIR = "/home/peterw/temp"
try:
os.makedirs(SEMANTIC_OUTPUT_DIR)
except BaseException:
pass
'''try:
os.makedirs(INSTANCE_OUTPUT_DIR)
except BaseException:
pass'''
image_paths = glob.glob(os.path.join(IMG_DIR, '*_rgb.png'))
output = {}
output['images'] = []
output['semantics'] = []
output['instances'] = []
output['n_objects'] = []
output['names'] = []
for image_path in tqdm(subfolders): #[:380]): #image_paths:
identifier = image_path.split('/')[-1]
num, whichone = identifier.split("_")
raw_path = os.path.join( raw_dir, whichone+num+".png")
img = cv2.imread(raw_path)
filelist = [os.path.join(image_path, file) for file in os.listdir(image_path) if file.endswith('.png')]
filelist.sort()
instance_mask = [(cv2.imread(i, cv2.IMREAD_GRAYSCALE) !=0).astype(int) for i in filelist]
instance_mask = np.array(instance_mask)
instance_mask = np.transpose(instance_mask, (1,2,0))
semantic_mask = instance_mask.sum(2)
semantic_mask[semantic_mask != 0] = 1
semantic_mask = semantic_mask.astype(np.uint8)
output['images'].append(img)
output['semantics'].append(semantic_mask)
output['instances'].append(instance_mask)
output['n_objects'].append(instance_mask.shape[-1])
#output['names'].append(identifier)
np.save("train", output)
"""if False:
img = Image.open(raw_path)
img_width, img_height = img.size
image_name = os.path.splitext(os.path.basename(image_path))[
0].split('_rgb')[0]
# print(os.path.splitext(os.path.basename(image_path))[0])
# exit(0)
annotation_path = os.path.join(ANN_DIR, image_name + '_label.png')
if not os.path.isfile(annotation_path):
continue
anno_img = Image.open(annotation_path)
#anno_img = anno_img.convert('L')
#anno_img.save('{}.png'.format(image_name))
annotation = np.array(anno_img)
assert len(annotation.shape) == 2
assert np.array(img).shape[:2] == annotation.shape[:2]
instance_values = set(np.unique(annotation)).difference([0])
# # Define a dictionary to map instance values to labels
# instance_mapping = {
# 225: 1,
# 104: 2,
# 105: 2,
# 75: 3,
# 76: 3,
# 178: 4,
# 149: 5,
# 28: 6,
# 29: 6,
# 254: 7
# }
# # Transform instance values to labels using the dictionary
# for instance_value, label in instance_mapping.items():
# annotation[annotation == instance_value] = label
# if
# print(instance_values)
#exit()
n_instances = len(instance_values)
if n_instances == 0:
continue
instance_mask = np.zeros(
(img_height, img_width, n_instances), dtype=np.uint8)
for i, v in enumerate(instance_values):
_mask = np.zeros((img_height, img_width), dtype=np.uint8)
_mask[annotation == v] = 1
instance_mask[:, :, i] = _mask
semantic_mask = instance_mask.sum(2)
semantic_mask[semantic_mask != 0] = 1
semantic_mask = semantic_mask.astype(np.uint8)
np.save(os.path.join(INSTANCE_OUTPUT_DIR, image_name + '.npy'),
instance_mask)
np.save(os.path.join(SEMANTIC_OUTPUT_DIR, image_name + '.npy'),
semantic_mask)"""