当前位置:   article > 正文

labelme的json文件转换为yolo标签格式_labelme json 转换为yolov8格式

labelme json 转换为yolov8格式

1. json格式说明

  1. {
  2. "shapes": [
  3. {
  4. "points": [
  5. [
  6. 463.23809523809524,
  7. 380.8095238095238
  8. ],
  9. [
  10. 550.8571428571428,
  11. 425.57142857142856
  12. ]
  13. ],
  14. "shape_type": "rectangle",
  15. "flags": {},
  16. "label": "TT",
  17. "group_id": null
  18. },
  19. {
  20. "points": [
  21. [
  22. 329.90476190476187,
  23. 397.0
  24. ],
  25. [
  26. 378.4761904761905,
  27. 431.2857142857143
  28. ]
  29. ],
  30. "shape_type": "rectangle",
  31. "flags": {},
  32. "label": "car",
  33. "group_id": null
  34. }
  35. ],
  36. "imageData":......,
  37. "imagePath": "0073.jpg",
  38. "imageHeight": 720,
  39. "version": "4.2.10",
  40. "flags": {},
  41. "imageWidth": 1280
  42. }

其中重要的参数有shapes shape_type imagePath

    shapes: 其中包含了标注的具体信息,由列表组成,每一个元素是一个字典,一个字典包含了一个标注框的相关信息;

label是标签;

points是标注的点,points的取值与shape_type相关,这里取的是rectangle即直接由矩形的对角线上两点确定一个矩形框。因此points中也只有两个点的信息,点的坐标为(x,y) ,坐标轴原点在左上角,原点向下为y正向,向右为x正向。

imagePath: 标注图片的文件名
shape_type: 记录了标记时选择的方式。

2. 格式转化

  1. import json
  2. import os
  3. classes = ['car', 'Truck', 'person', 'bicycle', 'bus']
  4. def convert(img_size, box):
  5. x1 = box[0]
  6. y1 = box[1]
  7. x2 = box[2]
  8. y2 = box[3]
  9. x_center = ((x2 + x1) / 2 - 1) / img_size[0]
  10. y_center = ((y2 + y1) / 2 - 1) / img_size[1]
  11. w = (x2 - x1) / img_size[0]
  12. h = (y2 - y1) / img_size[1]
  13. return (x_center, y_center, w, h)
  14. def decode_json(json_floder_path, json_name):
  15. txt_name = '/home/zhy/Documents/智能驾驶项目/标注/txt/' + json_name[0:-5] + '.txt'
  16. txt_file = open(txt_name, 'w')
  17. json_path = os.path.join(json_floder_path, json_name)
  18. data = json.load(open(json_path, 'r', encoding='utf-8'))
  19. img_w = data['imageWidth']
  20. img_h = data['imageHeight']
  21. for i in data['shapes']:
  22. if i['shape_type'] == 'rectangle':
  23. x1 = int(i['points'][0][0])
  24. y1 = int(i['points'][0][1])
  25. x2 = int(i['points'][1][0])
  26. y2 = int(i['points'][1][1])
  27. classname = i['label']
  28. cls_id = classes.index(classname)
  29. bb = (x1, y1, x2, y2)
  30. bbox = convert((img_w, img_h), bb)
  31. txt_file.write(str(cls_id) + " " + " ".join([str(a) for a in bbox]) + '\n')
  32. if __name__ == "__main__":
  33. json_floder_path = '/home/zhy/Documents/智能驾驶项目/标注/json/'
  34. json_names = os.listdir(json_floder_path)
  35. for json_name in json_names:
  36. decode_json(json_floder_path, json_name)


 

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

闽ICP备14008679号