-
Notifications
You must be signed in to change notification settings - Fork 0
/
phenobench2coco_panpotic.py
218 lines (177 loc) · 7.45 KB
/
phenobench2coco_panpotic.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
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
import json
from PIL import Image
import os
import numpy as np
from pycocotools import mask
import datetime
##########################################################################3
# from mode='L' format generate 'RGB' image : annotation_seg
def generate_rgb_from_L(orgin_path, save_path):
img_name_list = sorted(os.listdir(orgin_path))
img_path_list = [os.path.join(orgin_path, img_name) for img_name in img_name_list]
save_path_list = [os.path.join(save_path, img_name) for img_name in img_name_list]
for i in range(len(img_path_list)):
img_L = Image.open(img_path_list[i])
img_rgb = img_L.convert(mode='RGB')
img_rgb.save(save_path_list[i], quality=95)
##########################################################################
ROOT_DIR = '/home/zhongzhou/Documents/mmlab_cuda/mmdetection/data/PhenoBench/train'
IMAGE_DIR = os.path.join(ROOT_DIR, 'images')
INSTANCES_DIR = os.path.join(ROOT_DIR, 'leaf_instances')
ANNOTATION_DIR = os.path.join(ROOT_DIR, 'leaf_annoations')
SAVE_JSON_DIR = '/home/zhongzhou/Documents/mmlab_cuda/mmdetection/data/PhenoBench_coco/annotations'
SAVE_JOSN_NAME = 'panoptic_leaf_train.json'
# create img info for annotations
def create_image_info(image_id, file_name, image_size,
date_captured=datetime.datetime.utcnow().isoformat(' '),
license_id=1, coco_url="", flickr_url=""):
image_info = {
"id": image_id,
"file_name": file_name,
"width": image_size[0],
"height": image_size[1],
"date_captured": date_captured,
"license": license_id,
"coco_url": coco_url,
"flickr_url": flickr_url
}
return image_info
## masks_generator accroding every instance for image
def masks_generator(img_name_list, mask_name_list):
# check the data integrity
for img_name in img_name_list:
assert img_name in mask_name_list, "exist some img is not in the mask list"
for mask_name in mask_name_list:
idx = 0
all_instances_image = np.array(Image.open(os.path.join(INSTANCES_DIR, mask_name)))
height, width = all_instances_image.shape[:2]
# find the unique elements of an array, return the sorted elements of an array.
index_array = np.unique(all_instances_image)
# every instance: 0:background, other:thing
for index in index_array:
if index == 0:
continue
instance_id = index
instance_class = 'leaf'
instance_mask = np.zeros((height, width), dtype=np.uint8)
mask = all_instances_image == instance_id
instance_mask[mask] = 255
# 1.path
mask_save_path = os.path.join(ANNOTATION_DIR, mask_name.split('.')[0])
# 2.name
instance_name = instance_class + '_' + str(idx) + '.png'
# save
if not os.path.exists(mask_save_path):
os.mkdir(mask_save_path)
instance_mask = Image.fromarray(instance_mask)
instance_mask.save(os.path.join(mask_save_path, instance_name), mode='L')
########
idx += 1
# create segmentation_info fo annotation
def create_segmentation_info(segmentation_id, category_id, iscrowd, bounding_box=None, area=None):
segmentation_info = {
"id": segmentation_id,
"category_id":category_id,
"is_crowd": iscrowd,
"bbox": list(map(int, bounding_box)),
'area': int(area)
}
# print(type(segmentation_info["bbox"]))
# print(type(segmentation_info["bbox"][0]))
# print(type(segmentation_info["area"]))
return segmentation_info
INFO = {
"description": "leaf panoptic segmentation dataset",
"url": "http",
"version" : "0.1.0",
"year": 2023,
"contributor": "zhongzhou zhou",
"date_created": datetime.datetime.utcnow().isoformat(' ')
}
LICENSES = [
{
"id": 1,
"name": "license_001",
"url" : "http://zhongzhou.xidian.edu.cn"
},
{
"id": 2,
"name": "license_002",
"url" : "http://zhongzhou.xidian.edu.cn"
}
]
CATEGORIES = [
{
'supercategory':"object", 'isthing':1, 'id':1, 'name': 'leaf'
},
{
'supercategory':"background", 'isthing':0, 'id':2, 'name':'soil'
}
]
# generate annotation panoptic_leaf_train.json and panoptic_leaf_val.json
def generate_panoptic_anno_json():
coco_output = {
"info" : INFO,
"licenses": LICENSES,
"images":[],
"annotations":[],
"categories":CATEGORIES
}
# image id
image_id = 1
# instance id
segmentation_id = 1
# push all image, in the img path. sort all img.....according the img name take the img and mask.
img_name_list = sorted(os.listdir(IMAGE_DIR))
# go through each image
for img_name in img_name_list:
img_path = os.path.join(IMAGE_DIR, img_name)
#########################################################
img = Image.open(img_path)
img_info = create_image_info(image_id, img_name, img.size)
coco_output["images"].append(img_info)
#########################################################\
annotation_info = {
"segments_info" : [],
"file_name": img_name,
"image_id": image_id
}
instace_path = os.path.join(ANNOTATION_DIR, img_name.split('.')[0])
instance_names_list = sorted(os.listdir(instace_path))
areas = 0
for instance_name in instance_names_list:
instance = Image.open(os.path.join(instace_path, instance_name))
instance = np.array(instance, dtype=np.uint8)
binary_mask_encoded = mask.encode(np.asfortranarray(instance))
area = mask.area(binary_mask_encoded)
bounding_box = mask.toBbox(binary_mask_encoded)
category_id = 1
segmentation_info = create_segmentation_info(segmentation_id=segmentation_id, category_id=category_id , iscrowd=0, bounding_box=bounding_box, area=area)
annotation_info["segments_info"].append(segmentation_info)
segmentation_id += 1
areas += area
# generate the last background segmentation_info
binary_mask_encoded = mask.encode(np.asfortranarray(instance))
area = 1024*1024 - areas
bounding_box = [0, 0, 1024, 1024]
category_id = 2
segmentation_info = create_segmentation_info(segmentation_id=segmentation_id, category_id=category_id , iscrowd=0, bounding_box=bounding_box, area=area)
annotation_info["segments_info"].append(segmentation_info)
segmentation_id += 1
coco_output["annotations"].append(annotation_info)
image_id += 1
# save coco_output
panopatic_json = json.dumps(coco_output)
with open(os.path.join(SAVE_JSON_DIR, SAVE_JOSN_NAME), 'w') as f:
f.write(panopatic_json)
f.close()
# return coco_output
if __name__=='__main__':
## L -> rgb
# generate_rgb_from_L(orgin_path="data/PhenoBench/train/leaf_instances", save_path="data/PhenoBench_coco/annotations/panoptic_leaf_train")
# generate_rgb_from_L(orgin_path="data/PhenoBench/val/leaf_instances", save_path="data/PhenoBench_coco/annotations/panoptic_leaf_val")
# create annotation mask
# img_name_list = sorted(os.listdir(IMAGE_DIR))
# mask_name_lsit = sorted(os.listdir(INSTANCES_DIR))
# masks_generator(img_name_list, mask_name_lsit)
generate_panoptic_anno_json()