-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_dataset.py
83 lines (68 loc) · 3.56 KB
/
json_to_dataset.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
# @Note : 生成标签图
import base64
import json
import os
import os.path as osp
import numpy as np
import PIL.Image
from labelme import utils
'''
制作自己的语义分割数据集需要注意以下几点:
1、我使用的labelme版本是3.16.7,建议使用该版本的labelme,有些版本的labelme会发生错误,
具体错误为:Too many dimensions: 3 > 2
安装方式为命令行pip install labelme==3.16.7
2、此处生成的标签图是8位彩色图,与视频中看起来的数据集格式不太一样。
虽然看起来是彩图,但事实上只有8位,此时每个像素点的值就是这个像素点所属的种类。
所以其实和视频中VOC数据集的格式一样。因此这样制作出来的数据集是可以正常使用的。也是正常的。
'''
if __name__ == '__main__':
image_and_json_path = 'image_and_json3' # 图片和json文件所在路径
jpgs_path = 'Dataset3/image' # 训练图保存路径
pngs_path = 'Dataset3/seg_image' # 标签图保存路径
classes = ["_background_", "1cm", "2cm", "3cm", "4cm", "5cm", "6cm", "7cm", "8cm"] # 标签中的类别
count = os.listdir(image_and_json_path)
for i in range(0, len(count)):
path = os.path.join(image_and_json_path, count[i])
if os.path.isfile(path) and path.endswith('json'):
data = json.load(open(path))
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
with open(imagePath, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
# label_values must be dense
label_values, label_names = [], []
for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
label_values.append(lv)
label_names.append(ln)
assert label_values == list(range(len(label_values)))
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
# 给生成的数据命名
"""原码
PIL.Image.fromarray(img).save(osp.join(jpgs_path, count[i].split(".")[0] + '.jpg'))
"""
jpg_name = count[i].rsplit(".", 1)[0]
PIL.Image.fromarray(img).save(osp.join(jpgs_path, jpg_name + '.jpg')) # 数据图命名
new = np.zeros([np.shape(img)[0], np.shape(img)[1]])
for name in label_names:
index_json = label_names.index(name)
index_all = classes.index(name)
new = new + index_all * (np.array(lbl) == index_json)
"""原码
utils.lblsave(osp.join(pngs_path, count[i].split(".")[0] + '.png'), new)
print('Saved ' + count[i].split(".")[0] + '.jpg and ' + count[i].split(".")[0] + '.png')
"""
png_name = count[i].rsplit(".", 1)[0]
utils.lblsave(osp.join(pngs_path, png_name + '.png'), new) # 标签图命名
print(jpg_name+'.jpg', ' / ', png_name+'.png', 'saved!')