Advertisement

目标检测-AnyLabeling标注格式转换成YOLO格式

阅读量:

Anylabel可以极大的增加数据的标注效率,但是其标注格式如何能转换成YOLO标注格式,具体内容如下所示。


关于AnyLabeling的其它详细介绍如下链接所示

<>

Github链接

https://github.com/vietanhdev/anylabeling

python代码

复制代码
  
    
 import json
    
 import os
    
  
    
  
    
 def labelme_to_yolo(label_me_json_file, cls2id_dict):
    
     label_me_json = json.load(open(label_me_json_file, mode='r', encoding='UTF-8'))
    
     shapes = label_me_json['shapes']
    
     img_width, img_height = label_me_json['imageWidth'], label_me_json['imageHeight']
    
     img_path = label_me_json['imagePath']
    
     img_data = label_me_json['imageData'] if 'imageData' in label_me_json else ''
    
  
    
     labels = []
    
     for s in shapes:
    
     s_type = s['shape_type']
    
     s_type = s_type.lower()
    
     if s_type == 'rectangle':
    
         pts = s['points']
    
         x1, y1 = pts[0]  # left corner
    
         x2, y2 = pts[1]  # right corner
    
         x = (x1 + x2) / 2 / img_width
    
         y = (y1 + y2) / 2 / img_height
    
         w = abs(x2 - x1) / img_width
    
         h = abs(y2 - y1) / img_height
    
         cid = cls2id_dict[s['label']]
    
         labels.append(f'{cid} {x} {y} {w} {h}')
    
  
    
     return labels
    
  
    
 def write_label2txt(save_txt_path,label_list):
    
     f=open(save_txt_path,"w",encoding="UTF-8")
    
  
    
     for label in label_list:
    
     temp_list=label.split(" ")
    
     f.write(temp_list[0])
    
     f.write(" ")
    
     f.write(temp_list[1])
    
     f.write(" ")
    
     f.write(temp_list[2])
    
     f.write(" ")
    
     f.write(temp_list[3])
    
     f.write(" ")
    
     f.write(temp_list[4])
    
     f.write("\n")
    
  
    
 if __name__ == '__main__':
    
     # 原始图片文件夹路径
    
     img_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\imgs"
    
     # 原始JSON标签文件夹路径
    
     json_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\labels"
    
     # 生成保存TXT文件夹路径
    
     save_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\txt"
    
     # 类别和序号的映射字典
    
     cls2id_dict={"building1":"0"}
    
  
    
     if not os.path.exists(save_dir):
    
     os.makedirs(save_dir)
    
  
    
     for json_name in os.listdir(json_dir):
    
     json_path=os.path.join(json_dir,json_name)
    
     txt_name=json_name.split(".")[0]+".txt"
    
     save_txt_path=os.path.join(save_dir,txt_name)
    
     labels = labelme_to_yolo(json_path,cls2id_dict)
    
     write_label2txt(save_txt_path,labels)
    
    
    
    
    python
    
    
![](https://ad.itadn.com/c/weblog/blog-img/images/2025-08-16/c6JtypEzwLKusmjqnDQdfGPMhSka.png)

具体修改

复制代码

改为自己的图片路径

复制代码

改为自己的JSON文件夹路径

复制代码

改为自己的保存生成的yolo文件夹路径

复制代码

改为自己的标签映射

开始实验

实验准备



代码


实验验证

实验验证可视化代码相关链接

<>

完美!

全部评论 (0)

还没有任何评论哟~