编辑labelme标注的json文件

编辑labelme标注的json文件

  • 解决的问题
  • json文件的读取
  • json文件的保存
  • 编辑labelme的json文件
  • 完整的程序

解决的问题

	label是一种进行图像标注的软件,但是不同分类任务会需要不同的标注格式,人工修改标注结果极为费时。可以根据需求采用程序自动的按照一定的规则修改标注文件。
	在使用maskrcnn程序时需要使用实例标注文件,但原始标注文件为语义分割格式,如原始语义分割标签为person,person,dog,dog,dog。实例分割的标签则为person0,person1,dog0,dog1,dog2。通过程序读取原始文件并依次对每个目标进行计数并修改标签名称。

json文件的读取

def read_json(json_path):
	f = open(json_path)  ##当文件中包含中文时,设置以utf-8解码模式读取文件open(out_path, 'w',encoding='utf-8',默认以gbk模式读取文件,
	json_file = json.load(f)
	return json_file

json文件的保存

def save_json(out_path,json_file):
	f = open(out_path, 'w',encoding='utf-8')  ##设置以utf-8解码模式读取文件,默认为gbk模式
	json.dump(json_file,f)

编辑labelme的json文件

def edit_labelme_json(labelme_json):
	# 对labelme生成的json文件进行内容编辑
	# labelme数据格式以字典形式存储,格式如下:
	# labelme_json = {'flags':{},"shapes":[],
	# 	"lineColor":[0,255,0,128],"fillColor": [255,0,0,128],
	# 	"imagePath": "",
	# 	"imageData":''}
	version = labelme_json['version']
	flags = labelme_json['flags']
	shapes = labelme_json['shapes']
	imagePath = labelme_json['imagePath']
	imageData = labelme_json['imageData']
	imageHeight = labelme_json['imageHeight']
	imageWidth = labelme_json['imageWidth']

	##################### 编辑 #############################
	label_list = [] #记录json中所有物体的标签
	label_dic = {
     } #记录每个label出现的个数

	# imageHeight = int(imageHeight/10) #图像缩放
	# imageWidth = int(imageWidth/10)
	new_shapes = list([])
	for tmp_shape in shapes:
		for shape_key,shape_values in tmp_shape.items():
			## 对每个类别进行计数并修改类别标签名,将语义分割标签转化为实例分割标签
			if shape_key == 'label':
				# shape_values = shape_values
				if shape_values not in label_list:
					label_list.append(shape_values)
					label_dic.update({
     shape_values:0})
					shape_values = shape_values+'0'
				else:
					label_dic.update({
     shape_values:label_dic[shape_values]+1})
					shape_values = shape_values+str(label_dic[shape_values])
				label_values = shape_values
			## 其他参数均不变
			elif shape_key == 'points':points_values = shape_values
			elif shape_key == 'group_id':group_id_values = shape_values
			elif shape_key == 'shape_type':shape_type_values = shape_values
			elif shape_key == 'flags':flags_values = shape_values
		new_shape = {
     'label':label_values,'points':points_values,'group_id':group_id_values,'shape_type':shape_type_values,'flags':flags_values}
		new_shapes.append(new_shape)
	new_json = {
     'version':version,
				'flags':flags,
				'shapes':new_shapes,
				'imagePath':imagePath,
				'imageData':imageData,
				'imageHeight':imageHeight,
				'imageWidth':imageWidth}

	return new_json

完整的程序

import os,json
import numpy as np

def read_json(json_path):
	f = open(json_path)  ##当文件中包含中文时,设置以utf-8解码模式读取文件open(out_path, 'w',encoding='utf-8',默认以gbk模式读取文件,
	json_file = json.load(f)
	return json_file

def save_json(out_path,json_file):
	f = open(out_path, 'w',encoding='utf-8')  ##设置以utf-8解码模式读取文件,默认为gbk模式
	json.dump(json_file,f)

def edit_labelme_json(labelme_json):
	# 对labelme生成的json文件进行内容编辑
	# labelme数据格式以字典形式存储,格式如下:
	# labelme_json = {'flags':{},"shapes":[],
	# 	"lineColor":[0,255,0,128],"fillColor": [255,0,0,128],
	# 	"imagePath": "",
	# 	"imageData":''}
	version = labelme_json['version']
	flags = labelme_json['flags']
	shapes = labelme_json['shapes']
	imagePath = labelme_json['imagePath']
	imageData = labelme_json['imageData']
	imageHeight = labelme_json['imageHeight']
	imageWidth = labelme_json['imageWidth']

	##################### 编辑 #############################
	label_list = [] #记录json中所有物体的标签
	label_dic = {
     } #记录每个label出现的个数

	# imageHeight = int(imageHeight/10) #图像缩放
	# imageWidth = int(imageWidth/10)
	new_shapes = list([])
	for tmp_shape in shapes:
		for shape_key,shape_values in tmp_shape.items():
			## 对每个类别进行计数并修改类别标签名,将语义分割标签转化为实例分割标签
			if shape_key == 'label':
				# shape_values = shape_values
				if shape_values not in label_list:
					label_list.append(shape_values)
					label_dic.update({
     shape_values:0})
					shape_values = shape_values+'0'
				else:
					label_dic.update({
     shape_values:label_dic[shape_values]+1})
					shape_values = shape_values+str(label_dic[shape_values])
				label_values = shape_values
			## 其他参数均不变
			elif shape_key == 'points':points_values = shape_values
			elif shape_key == 'group_id':group_id_values = shape_values
			elif shape_key == 'shape_type':shape_type_values = shape_values
			elif shape_key == 'flags':flags_values = shape_values
		new_shape = {
     'label':label_values,'points':points_values,'group_id':group_id_values,'shape_type':shape_type_values,'flags':flags_values}
		new_shapes.append(new_shape)
	new_json = {
     'version':version,
				'flags':flags,
				'shapes':new_shapes,
				'imagePath':imagePath,
				'imageData':imageData,
				'imageHeight':imageHeight,
				'imageWidth':imageWidth}

	return new_json

def list_json(path):
	sup_ext = ['.json']
	all_list = list(map(lambda x:os.path.join(path,x),os.listdir(path)))
	json_list = [x for x in all_list if os.path.splitext(x)[1] in sup_ext]
	return json_list

def get_outpath(out_dir,name):
	if not os.path.exists(out_dir):os.makedirs(out_dir)
	return os.path.join(out_dir,os.path.basename(name))

if __name__ == '__main__':
	json_dir = 'json_ori'
	out_dir = 'json'
	json_list = list_json(json_dir)
	for i in range(len(json_list)):
		json_r = read_json(json_list[i])
		json_ed = edit_labelme_json(json_r)
		outpath = get_outpath(out_dir,json_list[i])
		save_json(outpath,json_ed)

你可能感兴趣的:(python,深度学习,机器学习,json,labelme)