forked from guchengxi1994/mask2json
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson2mask.py
111 lines (97 loc) · 3.94 KB
/
json2mask.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
'''
@lanhuage: python
@Descripttion: Deprecated. Just for test. For more information,see convertmask.utils.json2mask.convert
@version: beta
@Author: xiaoshuyui
@Date: 2020-06-12 09:32:14
LastEditors: xiaoshuyui
LastEditTime: 2020-10-20 09:31:57
'''
import sys
sys.path.append('..')
import json
import os
import os.path as osp
import warnings
import numpy as np
import PIL.Image
import yaml
isInstalled = True
# from labelme import utils
try:
from labelme.utils import draw_label
except:
isInstalled = False
if isInstalled:
import labelme.utils as lUtils # solve conflict
else:
from convertmask.labelme_sub import utils as lUtils
def main():
json_file = 'D:\\testALg\\mask2json\\mask2json\\static\\' #json path
list = os.listdir(json_file)
for i in range(0, len(list)):
path = os.path.join(json_file, list[i])
if os.path.isfile(path) and path.endswith('.json'):
data = json.load(open(path))
img = lUtils.img_b64_to_arr(data['imageData'])
lbl, lbl_names = lUtils.labelme_shapes_to_label(
img.shape, data['shapes'])
# lbl[lbl>0] = 255
# print(lbl)
captions = [
'%d: %s' % (l, name) for l, name in enumerate(lbl_names)
]
lbl_viz = lUtils.draw_label(lbl, img, captions)
out_dir = osp.basename(list[i]).replace('.', '_')
out_dir = osp.join(osp.dirname(list[i]), out_dir)
if not osp.exists(out_dir):
os.mkdir(out_dir)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
print(np.max(lbl))
lbl = np.array(lbl, dtype=np.uint8)
lbl[lbl > 0] = 255
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label_255.png'))
PIL.Image.fromarray(lbl_viz).save(
osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in lbl_names:
f.write(lbl_name + '\n')
warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=lbl_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
print('Saved to: %s' % out_dir)
def singleFile(filePath):
if os.path.isfile(filePath) and filePath.endswith('.json'):
data = json.load(open(filePath))
img = lUtils.img_b64_to_arr(data['imageData'])
lbl, lbl_names = lUtils.labelme_shapes_to_label(
img.shape, data['shapes'])
# lbl[lbl>0] = 255
# print(lbl)
captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
lbl_viz = lUtils.draw_label(lbl, img, captions)
out_dir = osp.basename(filePath).replace('.', '_')
out_dir = osp.join(osp.dirname(filePath), out_dir)
if not osp.exists(out_dir):
os.mkdir(out_dir)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
print(np.max(lbl))
lbl = np.array(lbl, dtype=np.uint8)
lbl[lbl > 0] = 255
PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label_255.png'))
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in lbl_names:
f.write(lbl_name + '\n')
warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=lbl_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
print('Saved to: %s' % out_dir)
if __name__ == '__main__':
# main()
singleFile(
'D:\\testALg\\mask2json\\mask2json\\static\\multi_objs_sameclass.json')