注意:使用 3.16.2版本的labelme, 博主试过4.2.10版本的labelme,会报错,没有draw.py文件。
且博主试过了,将3.16.2版本的draw.py文件拷贝到4.2.10文件夹下的utils文件中,自己构建也是不行的,会报错。 应该得改动draw.py文件中的内容吧。
以下代码为全部代码,可直接运行。
# -*- encoding: utf-8 -*-
"""
@File : json_to_dataset.py.py
@Time : 2020/5/28 18:09
@Author : ligang
@WeChat : by15188607997
@Software: PyCharm
@explain:本文件为将json文件批量转为dataset
"""
import argparse
import json
import os
import os.path as osp
import warnings
import PIL.Image
import yaml
from labelme import utils
import base64
def main(frompath, outputpath):
count = os.listdir(frompath)
for i in range(0, len(count)):
path = os.path.join(frompath, 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'])
print(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)
captions = ['{}: {}'.format(lv, ln)
for ln, lv in label_name_to_value.items()]
lbl_viz = utils.draw_label(lbl, img, captions)
out_dir = osp.basename(count[i]).replace('.', '_')
out_dir = osp.join(osp.dirname(count[i]), out_dir)
out_dir = osp.join(outputpath, out_dir)
if not osp.exists(out_dir):
os.mkdir(out_dir)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
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 label_names:
f.write(lbl_name + '\n')
warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=label_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__':
# 源图片、json 文件路径
frompath = "./before/"
# 生成数据保存路径
outputpath = "./output"
if not osp.exists(outputpath):
os.mkdir(outputpath)
main(frompath, outputpath)
结果:保存在输出的output文件中,此文件作用同 labelme_json_to_dataset <**.json> 作用。上面代码是批量的。
注意:class_name.txt文件中的类需要手动填写。
# -*- encoding: utf-8 -*-
"""
@File : get_jpg_and_png.py
@Time : 2020/5/28 18:07
@Author : ligang
@WeChat : by15188607997
@Software: PyCharm
"""
import os
from PIL import Image
import numpy as np
def main(frompath_jpg, outputpath_json, output_jpg, output_png, path_allclass):
# 读取原文件夹
count = os.listdir(frompath_jpg)
for i in range(0, len(count)):
# 如果里的文件以jpg结尾
# 则寻找它对应的png
if count[i].endswith("jpg"):
path = os.path.join(frompath_jpg, count[i])
img = Image.open(path)
img.save(os.path.join(output_jpg, count[i]))
# 找到对应的png
path = outputpath_json + count[i].split(".")[0] + '_' + count[i].split(".")[1] + "_json/label.png"
img = Image.open(path)
# 找到全局的类
class_txt = open(path_allclass, "r")
class_name = class_txt.read().splitlines()
# ["bk","cat","dog"]
# 打开json文件里面存在的类,称其为局部类
with open(outputpath_json + count[i].split(".")[0] + '_' + count[i].split(".")[1] + "_json/label_names.txt", "r") as f:
names = f.read().splitlines()
# ["bk","dog"]
new = Image.new("RGB", [np.shape(img)[1], np.shape(img)[0]])
for name in names:
# index_json是json文件里存在的类,局部类
index_json = names.index(name)
# index_all是全局的类
index_all = class_name.index(name)
# 将局部类转换成为全局类
new = new + np.expand_dims(index_all * (np.array(img) == index_json), -1)
new = Image.fromarray(np.uint8(new))
new.save(os.path.join(output_png, count[i].replace("jpg", "png")))
print(np.max(new), np.min(new))
if __name__ == '__main__':
# 全局类(所有标签总共有多少类) 如:
# _background_(不可少)
# Albatross
# _Yellowthroat
path_allclass = "./before/class_name.txt"
# 源图片、json 文件路径
frompath_jpg = "./before/"
# 生成数据保存路径
outputpath_json = "./output/"
# 生成jpg数据的保存位置
output_jpg = "./traindata/jpg/"
# 生成jpg数据的保存位置
output_png = "./traindata/png/"
if not os.path.exists(output_jpg):
os.makedirs(output_jpg)
if not os.path.exists(output_png):
os.makedirs(output_png)
main(frompath_jpg, outputpath_json, output_jpg, output_png, path_allclass)
结果:成果保存在traindata文件夹下。生成的png图片为二值化图。像素变化从 (1,1,1)开始的眼是看不来的,可以用取色器进行取色验证下。