提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
由于之前使用的是LabelImg进行的图像检测的标注工作,后来有需要进行关键点标注,最初采用的方法是LabelImg矩形框的左下点坐标和右下点坐标来代替关键点的坐标,发现标注的不是很准确。就想着用labelme软件来进行相应的标注,但是之前标注了太多的图片,重新标注的话费时费力,就采用代码将之前LabelImg标注的VOC格式转化为labelme的json格式。
本人使用的MAC电脑,在anaconda的环境下面首先创建一个虚拟环境:
conda create–name=labelme python=3.6
激活环境:
source activate labelme
安装labelme:
pip install labelme==4.5.6
打开labelme:
labelme
打开后界面如下
import argparse
import glob
import os
import xml.etree.ElementTree as ET
import json
from tqdm import tqdm
def parse_args():
"""
参数配置
"""
parser = argparse.ArgumentParser(description='xml2json')
parser.add_argument('--raw_label_dir', help='the path of raw label', default='')
parser.add_argument('--pic_dir', help='the path of picture', default='')
parser.add_argument('--save_dir', help='the path of new label', default='')
args = parser.parse_args()
return args
def read_xml_gtbox_and_label(xml_path):
"""
读取xml内容
"""
tree = ET.parse(xml_path)
root = tree.getroot()
size = root.find('size')
width = int(size.find('width').text)
height = int(size.find('height').text)
depth = int(size.find('depth').text)
points = []
for obj in root.iter('object'):
cls = obj.find('name').text
pose = obj.find('pose').text
xmlbox = obj.find('bndbox')
xmin = float(xmlbox.find('xmin').text)
xmax = float(xmlbox.find('xmax').text)
ymin = float(xmlbox.find('ymin').text)
ymax = float(xmlbox.find('ymax').text)
box = [xmin, ymin, xmax, ymax]
point = [cls, box]
points.append(point)
return points, width, height
def main():
"""
主函数
"""
args = parse_args()
labels = glob.glob(args.raw_label_dir + '/*.xml')
for i, label_abs in tqdm(enumerate(labels), total=len(labels)):
_, label = os.path.split(label_abs)
label_name = label.rstrip('.xml')
# img_path = os.path.join(args.pic_dir, label_name + '.jpg')
img_path = label_name + '.jpg'
points, width, height = read_xml_gtbox_and_label(label_abs)
json_str = {}
json_str['version'] = '4.5.6'
json_str['flags'] = {}
shapes = []
for i in range(len(points)):
# 判断是否是左下角的点为关键点
if points[i][0] == "left head":
shape = {}
shape['label'] = 'head'
shape['points'] = [[points[i][1][0], points[i][1][3]]]
shape['group_id'] = None
# 类型为点
shape['shape_type'] = 'point'
shape['flags'] = {}
shapes.append(shape)
# 判断是否是右下角的点是关键点
elif points[i][0] == "right head":
shape = {}
shape['label'] = 'head'
shape['points'] = [[points[i][1][2], points[i][1][3]]]
shape['group_id'] = None
shape['shape_type'] = 'point'
shape['flags'] = {}
shapes.append(shape)
# 其余的情况
else:
shape = {}
shape['label'] = points[i][0]
shape['points'] = [[points[i][1][0], points[i][1][1]],
[points[i][1][2], points[i][1][3]]]
shape['group_id'] = None
# labelIMG的标注类型基本都为长方形
shape['shape_type'] = 'rectangle'
shape['flags'] = {}
shapes.append(shape)
json_str['shapes'] = shapes
json_str['imagePath'] = img_path
json_str['imageData'] = None
json_str['imageHeight'] = height
json_str['imageWidth'] = width
with open(os.path.join(args.save_dir, label_name + '.json'), 'w') as f:
json.dump(json_str, f, indent=2)
if __name__ == '__main__':
main()
转化完成的json文件和图片放在一个文件下,目的是使得在json文件里面的imagePath要对应的上。
特别注意的是,使用labelme查看jason文件的时候必须加上 --nodata这个参数,即:
labelme --nodata
不然imageData的参数无法对应上会报错。如下所示:
成功打开之后图片如下所示
转化前的图片:
转化后的图片: