labelme json2dataset.pyソース解析
6886 ワード
json2dataset.py、ソースコード2つの特徴 label valueはlabel nameアルファベット順に並ぶ である. label colorはlabelmeを通過する.utils cormap生成 このような問題は、私たちがマークした画像が同じクラスに対応するlabel valueとcolorが異なる可能性があるので、まずvalueとcolorを確定します.
1. json2labelpng.py簡易版
機能:jsonファイルを着色したpngに変換
2. json2dataset.pyソース解析
1. json2labelpng.py簡易版
機能:jsonファイルを着色したpngに変換
import base64
import json
import os
import PIL.Image
from labelme import utils
import numpy as np
import csv
color_dict = {}
with open('sun37.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
for i, row in enumerate(reader):
if i > 0: #
color_dict[row[0]] = [int(row[1]), int(row[2]), int(row[3])]
# label: val, labelme val val
label_val_dict = {}
for i, key in enumerate(color_dict.keys()):
label_val_dict[key] = i
# RGB color
color_map = np.array(list(color_dict.values())) # 38,3
def json2labelpng(json_file):
data = json.load(open(json_file)) # json->dict
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(json_file), 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_val_dict, cmap
label_name_to_value = {
'background': 0 # 0
}
for shape in data['shapes']:
label_name_to_value[shape['label']] = label_val_dict[shape['label']]
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value) # np,val_img
# print(np.unique(lbl)) # [ 0 1 2 3 5 21 22 25 29] SUN
# colormap label.png
lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P') # Palette
lbl_pil.putpalette(color_map.astype(np.uint8).flatten()) # uint8
lbl_pil.save(json_file.replace('.json', '.png'))
if __name__ == '__main__':
json2labelpng(json_file='C:/Users/Shuai/PycharmProjects/Toy/labelme/0.json')
2. json2dataset.pyソース解析
import argparse
import base64
import json
import os
import os.path as osp
import PIL.Image
import yaml
from labelme.logger import logger
from labelme import utils
import numpy as np
import csv
color_dict = {}
with open('sun37.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
for i, row in enumerate(reader):
if i > 0: #
color_dict[row[0]] = [int(row[1]), int(row[2]), int(row[3])]
# label: val, labelme val val
label_val_dict = {}
for i, key in enumerate(color_dict.keys()):
label_val_dict[key] = i
# RGB color
color_map = np.array(list(color_dict.values())) # 38,3
def main(params):
logger.warning('This script is aimed to demonstrate how to convert the'
'JSON file to a single image dataset, and not to handle'
'multiple JSON files to generate a real-use dataset.')
parser = argparse.ArgumentParser()
parser.add_argument('json_file')
parser.add_argument('-o', '--out', default=None)
args = parser.parse_args(params)
json_file = args.json_file
if args.out is None: # , 0_json
out_dir = osp.basename(json_file).replace('.', '_')
out_dir = osp.join(osp.dirname(json_file), out_dir)
else:
out_dir = args.out
if not osp.exists(out_dir): # osp, os.path
os.mkdir(out_dir)
data = json.load(open(json_file)) # json->dict
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(json_file), 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
# labelme label_name_to_value
# =======================================
# label_name_to_value = {
# '_background_': 0 # 0
# }
# for shape in sorted(data['shapes'], key=lambda x: x['label']): # label_name
# 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
# label_name_to_value[label_name] = label_value
# lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
# lbl label_value 480x640 ,
# print(np.unique(lbl)) # [0 1 2 3 4 5 6 7 8], lbl SUN label value ,
# label_colormap
# utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
# =======================================
# label_val_dict, cmap
label_name_to_value = {
'background': 0 # 0
}
for shape in data['shapes']:
label_name_to_value[shape['label']] = label_val_dict[shape['label']]
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
# print(np.unique(lbl)) # [ 0 1 2 3 5 21 22 25 29] SUN
# val,
# label_names = [None] * (max(label_name_to_value.values()) + 1) #
# for name, value in label_name_to_value.items(): # dict
# label_names[value] = name
# lbl_viz = utils.draw_label(lbl, img, label_names)
# :img.png, label.png, label_viz.png
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
# colormap label.png
lbl_pil = PIL.Image.fromarray(lbl.astype(np.uint8), mode='P') # Palette
lbl_pil.putpalette(color_map.astype(np.uint8).flatten()) # uint8
lbl_pil.save(osp.join(out_dir, 'label.png'))
# label_viz.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 label_names:
# f.write(lbl_name + '
')
# logger.warning('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)
#
# logger.info('Saved to: {}'.format(out_dir))
if __name__ == '__main__':
params = [
'0.json', # 1
'-o', '0_json'
]
main(params)