一、简述
1、使用到的软件是labelme,对图像数据进行标注,生成标注文件.json文件。
2、针对.json文件,在终端输入指令:labelme_json_to_dataset 路径/文件名.json 即可生成dataset文件夹。
3、为文件夹下的label.png进行颜色填充,得到24位的着色的标注图。
二、安装labelme
下载并安装labelme。windows下安装labelme(在虚拟环境下安装的)
pip install labelme -i https://pypi.douban.com/simple/
在当前环境下输入labelme即可运行labelme
三、标注图片
点击save ,就可以保存成**.json**格式的文件。
三、将json文件转换成分割的可视化图像
安装labelme文件下有一个json_to_dataset.py文件,跳转到这个文件所在的位置
执行以下python指令:
python json_to_dataset.py F:\weather_recognition\data\train\label\1.json -0 F:\json\output
F:\weather_recognition\data\train\label\1.json是我保存的json文件路径。F:\json\output 是我要保存输出的文件路径。最后生成的结果如下
四、批量转换json文件
import argparse
import json
import os
import os.path as osp
import warnings
import copy
import numpy as np
import PIL.Image
from skimage import io
import yaml
from labelme import utils
def main():
parser = argparse.ArgumentParser()
parser.add_argument('json_file')
parser.add_argument('-o', '--out', default=None)
args = parser.parse_args()
json_file = args.json_file
list = os.listdir(json_file)
for i in range(0, len(list)):
path = os.path.join(json_file, list[i])
filename = list[i][:-5] # .json
if os.path.isfile(path):
data = json.load(open(path))
img = utils.image.img_b64_to_arr(data['imageData'])
lbl, lbl_names = utils.shape.labelme_shapes_to_label(img.shape, data['shapes']) # labelme_shapes_to_label
captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
lbl_viz = utils.draw.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, '{}.png'.format(filename)))
#PIL.Image.fromarray(lbl).save(osp.join(out_dir, '{}_gt.png'.format(filename)))
utils.lblsave(osp.join(out_dir, '{}_gt.png'.format(filename)), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, '{}_viz.png'.format(filename)))
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()
将这个代码替换到你的json_to_dataset.py里的内容,然后保存。
其中:
#PIL.Image.fromarray(lbl).save(osp.join(out_dir, ‘{}_gt.png’.format(filename))) 是生成灰度图
utils.lblsave(osp.join(out_dir, ‘{}_gt.png’.format(filename)), lbl)是生成彩色图
具体操作:
1.打开cmd,激活anaconda的虚拟环境。并进入到你想要保存的路径位置。
2.输入指令 E:\software_setting\anaconda\envs\ctpn\Scripts\labelme_json_to_dataset.exe ,如下图。
五、对生成的文件进行处理
为文件夹下的label.png进行颜色填充
import colorlabel
import PIL.Image
import yaml
import os
import glob
label_color = {
# 这个是你自己的类别和对应的颜色
'bulesky': 'blue',
'graysky': 'yellow',
'shadow': 'red',
'whitecloud':'green',
'snow':'maroon'
# 注意:这里的颜色需要是下面颜色列表color中的颜色
}
path = 'F:/weather_recognition/data/train/output/' # 你执行第二步时的存储路径
label_path = os.listdir(path)
print(label_path)
for label_class in label_path:
#print(label_class)
img = PIL.Image.open('%s\\label.png' % (path + label_class))
import numpy as np
from skimage import io, data, color
label = np.array(img)
yaml_file = path + label_class + '\\' + 'info.yaml'
print(yaml_file)
with open(yaml_file) as f:
f = yaml.load(f)
classes = f['label_names']
#print(classes)
color = [] # 颜色列表
c = 0
for i in classes:
print(i)
if c == 0:
c = c + 1
continue
color.append(label_color[i])
# print (color) #颜色列表
dst = colorlabel.label2rgb(label, colors=(tuple)(color), bg_label=0, bg_color=(0, 0, 0))
# final = PIL.Image.fromarray(np.uint8(dst * 255))
# final.show()
# final.save('000001.png')
save_path = 'F:/weather_recognition/data/train/output' + '\\' + label_class[:-5] + '.png' # 你希望将标注图片存储的路径
# print('11111%s' % save_path)
io.imsave(save_path, dst)
执行这个文件,便会生成对应图片的标注图片,结果如下:(白云是绿色,蓝天是蓝色,阴影是红色)
注:在这里有个问题,就是当我一张图片有三个类别时,批量转换的lable.png,最后颜色标注是相反的 如下图:
但是我用json_to_dataset.py这个文件转换出来的lable.png颜色标注就是正常的,如下图:
希望有能懂的小伙伴能给博主讲一下。将感激不尽!