当前位置:   article > 正文

YOLO数据集txt格式标签转换代码

YOLO数据集txt格式标签转换代码

如题,本文提供labelme标注的json文件和labelimg标注的xml文件转成yolo txt格式的标签文件,其他数据集标签处理问题,见如下博文:

作物计数方法汇总_作物计数+聚类-CSDN博客

作物计数方法之合并信息生成json标签的方法_fsc147-CSDN博客

FSC147数据集格式解析_fsc-147-CSDN博客

(1)json文件转yolo txt标签
  1. import json
  2. import os
  3. def convert(img_size, box):
  4. x1_center = box[0] + (box[2]-box[0]) / 2.0
  5. y1_center = box[1] + (box[3]-box[1]) / 2.0
  6. w_1 = box[2] - box[0]
  7. h_1 = box[3] - box[1]
  8. x1_normal = x1_center / img_size[0]
  9. y1_normal = y1_center / img_size[1]
  10. w_1_normal = w_1 / img_size[0]
  11. y_1_normal = h_1 / img_size[1]
  12. return (x1_normal, y1_normal, w_1_normal, y_1_normal)
  13. def decode_json(json_floder_path, json_name):
  14. txt_name = 'D:/TEST/label_yolo_test/txt/' + json_name[0:-5] + '.txt' # 改为自己的txt标签存储路径
  15. txt_file = open(txt_name, 'w')
  16. json_path = os.path.join(json_floder_path, json_name)
  17. data = json.load(open(json_path, 'r', encoding='utf-8'))
  18. img_w = data['imageWidth']
  19. img_h = data['imageHeight']
  20. for i in data['shapes']:
  21. if (i['shape_type'] == 'rectangle' and i['label'] == 'cotton_flower'): # 这里cotton_flower改成自己的标签类别
  22. x1 = float(i['points'][0][0])
  23. y1 = float(i['points'][0][1])
  24. x2 = float(i['points'][1][0])
  25. y2 = float(i['points'][1][1])
  26. print(x1)
  27. print(y1)
  28. print(x2)
  29. print(y2)
  30. print(img_w)
  31. print(img_h)
  32. bb = (x1, y1, x2, y2)
  33. bbox = convert((img_w, img_h), bb)
  34. txt_file.write( '0' + " " + " ".join([str(i) for i in bbox]) + '\n')
  35. elif (i['shape_type'] == 'rectangle' and i['label'] == 'potato_flower'): # 多类别标签就加判定条件
  36. x1 = float(i['points'][0][0])
  37. y1 = float(i['points'][0][1])
  38. x2 = float(i['points'][1][0])
  39. y2 = float(i['points'][1][1])
  40. print(x1)
  41. print(y1)
  42. print(x2)
  43. print(y2)
  44. print(img_w)
  45. print(img_h)
  46. bb = (x1, y1, x2, y2)
  47. bbox = convert((img_w, img_h), bb)
  48. txt_file.write( '1' + " " + " ".join([str(i) for i in bbox]) + '\n')
  49. if __name__ == "__main__":
  50. json_floder_path = 'D:/TEST/label_yolo_test/Json' #改成自己的json文件存储路径
  51. json_names = os.listdir(json_floder_path)
  52. for json_name in json_names:
  53. decode_json(json_floder_path, json_name)
(2)xml文件转yolo txt标签
  1. import os, shutil, random
  2. from tqdm import tqdm
  3. def split_img(img_path, label_path, split_list):
  4. try :
  5. Data = r'Cotton_flower_dataset/flower'
  6. # Data是创建的文件夹路径
  7. # os.mkdir(Data) #
  8. train_img_dir = Data + '/images/train'
  9. val_img_dir = Data + '/images/val'
  10. test_img_dir = Data + '/images/test'
  11. train_label_dir = Data + '/labels/train'
  12. val_label_dir = Data + '/labels/val'
  13. test_label_dir = Data + '/labels/test'
  14. # 创建文件夹
  15. os.makedirs(train_img_dir)
  16. os.makedirs(train_label_dir)
  17. os.makedirs(val_img_dir)
  18. os.makedirs(val_label_dir)
  19. os.makedirs(test_img_dir)
  20. os.makedirs(test_label_dir)
  21. except:
  22. print('文件目录已存在')
  23. train, val, test = split_list
  24. all_img = os.listdir(img_path)
  25. all_img_path = [os.path.join(img_path, img) for img in all_img]
  26. # all_label = os.listdir(label_path)
  27. # all_label_path = [os.path.join(label_path, label) for label in all_label]
  28. train_img = random.sample(all_img_path, int(train * len(all_img_path)))
  29. train_img_copy = [os.path.join(train_img_dir, img.split('/')[-1]) for img in train_img]
  30. # print(train_img)
  31. train_label = [toLabelPath(img, label_path) for img in train_img]
  32. train_label_copy = [os.path.join(train_label_dir, label.split('/')[-1]) for label in train_label]
  33. for i in tqdm(range(len(train_img)), desc='train ', ncols=80, unit='img'):
  34. _copy(train_img[i], train_img_dir)
  35. _copy(train_label[i], train_label_dir)
  36. all_img_path.remove(train_img[i])
  37. val_img = random.sample(all_img_path, int(val / (val + test) * len(all_img_path)))
  38. val_label = [toLabelPath(img, label_path) for img in val_img]
  39. for i in tqdm(range(len(val_img)), desc='val ', ncols=80, unit='img'):
  40. _copy(val_img[i], val_img_dir)
  41. _copy(val_label[i], val_label_dir)
  42. all_img_path.remove(val_img[i])
  43. test_img = all_img_path
  44. test_label = [toLabelPath(img, label_path) for img in test_img]
  45. for i in tqdm(range(len(test_img)), desc='test ', ncols=80, unit='img'):
  46. _copy(test_img[i], test_img_dir)
  47. _copy(test_label[i], test_label_dir)
  48. def _copy(from_path, to_path):
  49. shutil.copy(from_path, to_path)
  50. def toLabelPath(img_path, label_path):
  51. img = img_path.split('/')[-1]
  52. label = img.split('\\')[1].split('.jpg')[0] + '.txt' # 注意路径问题,分割好
  53. # print(label, "***********")
  54. return os.path.join(label_path, label)
  55. def main():
  56. img_path = r"Cotton_flower_dataset/image" # 图片存放的路径
  57. label_path = r"Cotton_flower_dataset/label_txt" # txt文件存放的路径
  58. split_list = [0.7, 0.2, 0.1] # 数据集划分比例[train:val:test]
  59. split_img(img_path, label_path, split_list)
  60. if __name__ == '__main__':
  61. main()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/869506
推荐阅读
相关标签
  

闽ICP备14008679号