第四篇—json标签(labelme)转txt标签(YOLOv5专题)
YouOnly_LiveOnce
2022年08月29日 19:26
收录于文集
共21篇
  • 我们使用labelme标注工具对图片进行标注,如下图所示:

labelme标注

  • 最后以json格式保存标注信息,主要格式如下图:

json文件格式

  • shapes:保存着bbox的类别和坐标(左上角,右下角);

  • imagePath:json文件对应的原图名称;

  • imageHeight:原图的高度;

  • imageWidth:原图的宽度;

  • 我们现在要将json文件中的标注信息提取到txt文件中,代码实现如下:

代码块
Python
自动换行
复制代码
import os 
import json 
import numpy as np
# 类和索引
CLASSES=["person","horse"]
def convert(size,box):
    '''
    input:
    size:(width,height);
    box:(x1,x2,y1,y2)
    output:
    (x,y,w,h)
    '''
    dw=1./size[0]
    dh=1./size[1]
    x=(box[0]+box[1])/2.0
    y=(box[2]+box[3])/2.0
    w=box[1]-box[0]
    h=box[3]-box[2]
    x=x*dw 
    w=w*dw 
    y=y*dh 
    h=h*dh 
    return (x,y,w,h)
# json -> txt
def json2txt(path_json,path_txt):
    with open(path_json,"r") as path_json:
        jsonx=json.load(path_json)
        width=int(jsonx["imageWidth"])      # 原图的宽
        height=int(jsonx["imageHeight"])    # 原图的高
        with open(path_txt,"w+") as ftxt:
            # 遍历每一个bbox对象
            for shape in jsonx["shapes"]:   
                obj_cls=str(shape["label"])     # 获取类别
                cls_id=CLASSES.index(obj_cls)   # 获取类别索引
                points=np.array(shape["points"])    # 获取(x1,y1,x2,y2)
                x1=int(points[0][0])
                y1=int(points[0][1])
                x2=int(points[1][0])
                y2=int(points[1][1])
                # (左上角,右下角) -> (中心点,宽高) 归一化
                bb=convert((width,height),(x1,x2,y1,y2))
                ftxt.write(str(cls_id)+" "+" ".join([str(a) for a in bb])+"\n")
if __name__=="__main__":
    # json文件夹
    dir_json="C:\\Users\\26818\\Desktop\\test\\input\\"
    # txt文件夹
    dir_txt="C:\\Users\\26818\\Desktop\\test\\out\\"
    if not os.path.exists(dir_txt):
        os.makedirs(dir_txt)
    # 得到所有json文件
    list_json=os.listdir(dir_json)
    # 遍历每一个json文件,转成txt文件
    for cnt,json_name in enumerate(list_json):
        print("cnt=%d,name=%s"%(cnt,json_name))
        path_json=dir_json+json_name 
        path_txt=dir_txt+json_name.replace(".json",".txt")
        # (x1,y1,x2,y2)->(x,y,w,h)
        json2txt(path_json,path_txt)
复制成功
  • 生成的txt文件如下图所示,格式包括(类别索引,x,y,w,h):

转换后的txt标签文件