当前位置:   article > 正文

labelme标注的多分类数据集转化为YOLO数据集(json转txt)_labelme转yolo

labelme转yolo

1、如何使用脚本

        (此脚本支持多分类的目标检测数据)

        1、修改dir_json为自己生成的coco数据集文件夹目录

        2、修改dir_txt为要放进去Yolo格式标签的文件夹目录

        3、直接运行代码

  1. import os
  2. import json
  3. # labelme标注的json标签文件目录和保存生成的txt标签的文件夹
  4. dir_json = r'D:/Json/'
  5. dir_txt = r'D:/txt/'
  6. # os.mkdir(dir_txt)
  7. classes2id = {}
  8. num = 0
  9. jsons = os.listdir(dir_json)
  10. for i in jsons:
  11. json_path = os.path.join(dir_json, i)
  12. with open(json_path, 'r', encoding="utf-8") as f:
  13. json_data = json.load(f)
  14. # print(json_data['shapes'])
  15. for j in json_data['shapes']:
  16. if j['label'] not in classes2id:
  17. classes2id[j['label']] = num
  18. num += 1
  19. print(classes2id)
  20. def json2txt(path_json, path_txt): # 可修改生成格式
  21. with open(path_json, 'r', encoding='utf-8') as path_json:
  22. jsonx = json.load(path_json)
  23. with open(path_txt, 'w+') as ftxt:
  24. shapes = jsonx['shapes']
  25. # 获取图片长和宽
  26. width = jsonx['imageWidth']
  27. height = jsonx['imageHeight']
  28. # print(shapes)
  29. cat=shapes[0]['label']
  30. cat=classes2id[cat]
  31. for shape in shapes:
  32. # 获取矩形框两个角点坐标
  33. x1 = shape['points'][0][0]
  34. y1 = shape['points'][0][1]
  35. x2 = shape['points'][1][0]
  36. y2 = shape['points'][1][1]
  37. dw = 1. / width
  38. dh = 1. / height
  39. x = dw * (x1 + x2) / 2
  40. y = dh * (y1 + y2) / 2
  41. w = dw * abs(x2 - x1)
  42. h = dh * abs(y2 - y1)
  43. yolo = f"{cat} {x} {y} {w} {h} \n"
  44. ftxt.writelines(yolo)
  45. list_json = os.listdir(dir_json)
  46. for cnt, json_name in enumerate(list_json):
  47. if os.path.splitext(json_name)[-1] == ".json":
  48. path_json = dir_json + json_name
  49. path_txt = dir_txt + json_name.replace('.json', '.txt')
  50. json2txt(path_json, path_txt)

2、实现过程

读取所有的.json文件,遍历后生成classes2id字典

  1. classes2id={}
  2. num=0
  3. jsons=os.listdir(dir_json)
  4. for i in jsons:
  5. json_path=os.path.join(dir_json,i)
  6. with open(json_path, 'r',encoding="utf-8") as f:
  7. json_data = json.load(f)
  8. #print(json_data['shapes'])
  9. for j in json_data['shapes']:
  10. if j['label'] not in classes2id:
  11. classes2id[j['label']]=num
  12. num+=1
  13. print(classes2id)

 将.json文件转化为.txt文件

  1. def json2txt(path_json, path_txt): # 可修改生成格式
  2. with open(path_json, 'r', encoding='utf-8') as path_json:
  3. jsonx = json.load(path_json)
  4. with open(path_txt, 'w+') as ftxt:
  5. shapes = jsonx['shapes']
  6. # 获取图片长和宽
  7. width = jsonx['imageWidth']
  8. height = jsonx['imageHeight']
  9. # print(shapes)
  10. cat=shapes[0]['label']
  11. cat=classes2id[cat]
  12. for shape in shapes:
  13. # 获取矩形框两个角点坐标
  14. x1 = shape['points'][0][0]
  15. y1 = shape['points'][0][1]
  16. x2 = shape['points'][1][0]
  17. y2 = shape['points'][1][1]
  18. dw = 1. / width
  19. dh = 1. / height
  20. x = dw * (x1 + x2) / 2
  21. y = dh * (y1 + y2) / 2
  22. w = dw * abs(x2 - x1)
  23. h = dh * abs(y2 - y1)
  24. yolo = f"{cat} {x} {y} {w} {h} \n"
  25. ftxt.writelines(yolo)

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

闽ICP备14008679号