Anylabel可以极大的增加数据的标注效率,但是其标注格式如何能转换成YOLO标注格式,具体内容如下所示。
关于AnyLabeling的其它详细介绍如下链接所示
Github链接
python代码
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)
具体修改
# 原始图片文件夹路径 img_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\imgs"
改为自己的图片路径
# 原始JSON标签文件夹路径 json_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\labels"
改为自己的JSON文件夹路径
# 生成保存TXT文件夹路径 save_dir=r"D:\desk\Work\Dataset\Test\Test_Anylabeling\txt"
改为自己的保存生成的yolo文件夹路径
# 类别和序号的映射字典 cls2id_dict={"building1":"0"}
改为自己的标签映射
开始实验
实验准备
运行代码
实验验证
实验验证可视化代码相关链接
https://blog.csdn.net/weixin_49824703/article/details/134050547
完美!