当前位置:   article > 正文

yolov5训练自己的数据集——labelme生成的json文件转txt文件_labelme保存的只有json文件怎么保存txt

labelme保存的只有json文件怎么保存txt

搜了好多json改txt的文章,有的看不懂自己用需要改哪里,有的只能单张转换,下面记录一下整个文件夹的json文件改txt文件的方法吧。

labelme对jpg文件进行标注后保存为json格式,json内主要信息如下:

{
  "version": "5.1.1",
  "flags": {},
  "shapes": [
{ "label": "SmallCar",
      "points": [        [          2036.0,          548.0        ],        [          2756.0,          1126.0        ]      ],
      "group_id": null,
      "shape_type": "rectangle",
      "flags": {}
    },.......
],

"imagePath": "118.jpg",

"imageHeight": 1860,

"imageWidth": 2880
}

shapes:保存着bbox的类别和坐标(左上角,右下角);

imagePath:json文件对应的原图名称;

imageHeight:原图的高度;

imageWidth:原图的宽度;

将json文件中的信息提取至txt文件内,代码实现如下:

  1. import os
  2. import json
  3. import numpy as np
  4. # 类和索引
  5. CLASSES = ["SmallCar", "BigCar", "NonMotorVeh", "Cyclist", "R", "RG", "RR", "RY", "LR", "Pedestrain", "LY", "G", "B", "FR", "FG"] # 改这
  6. def convert(size, box):
  7. '''
  8. input:
  9. size:(width,height);
  10. box:(x1,x2,y1,y2)
  11. output:
  12. (x,y,w,h)
  13. '''
  14. dw = 1. / size[0]
  15. dh = 1. / size[1]
  16. x = (box[0] + box[1]) / 2.0
  17. y = (box[2] + box[3]) / 2.0
  18. w = box[1] - box[0]
  19. h = box[3] - box[2]
  20. x = x * dw
  21. w = w * dw
  22. y = y * dh
  23. h = h * dh
  24. return (x, y, w, h)
  25. # json -> txt
  26. def json2txt(path_json, path_txt):
  27. with open(path_json, "r") as path_json:
  28. jsonx = json.load(path_json)
  29. width = int(jsonx["imageWidth"]) # 原图的宽
  30. height = int(jsonx["imageHeight"]) # 原图的高
  31. with open(path_txt, "w+") as ftxt:
  32. # 遍历每一个bbox对象
  33. for shape in jsonx["shapes"]:
  34. obj_cls = str(shape["label"]) # 获取类别
  35. cls_id = CLASSES.index(obj_cls) # 获取类别索引
  36. points = np.array(shape["points"]) # 获取(x1,y1,x2,y2)
  37. x1 = int(points[0][0])
  38. y1 = int(points[0][1])
  39. x2 = int(points[1][0])
  40. y2 = int(points[1][1])
  41. # (左上角,右下角) -> (中心点,宽高) 归一化
  42. bb = convert((width, height), (x1, x2, y1, y2))
  43. ftxt.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + "\n")
  44. if __name__ == "__main__":
  45. # json文件夹 不同的json文件夹记得换路径
  46. dir_json = "/home/lyn/Desktop/mydatasets/labels/val/"
  47. # txt文件夹 想让生成的txt文件放哪就改到哪个路径
  48. dir_txt = "/home/lyn/Desktop/mydatasets/labels/val/"
  49. if not os.path.exists(dir_txt):
  50. os.makedirs(dir_txt)
  51. # 得到所有json文件
  52. list_json = os.listdir(dir_json)
  53. # 遍历每一个json文件,转成txt文件
  54. for cnt, json_name in enumerate(list_json):
  55. print("cnt=%d,name=%s" % (cnt, json_name))
  56. path_json = dir_json + json_name
  57. path_txt = dir_txt + json_name.replace(".json", ".txt")
  58. # (x1,y1,x2,y2)->(x,y,w,h)
  59. json2txt(path_json, path_txt)

转换不同文件夹内的json文件只需将文件夹路径传入dir_json;

dir_txt后为生成的txt文件存放位置。

注意CLASSES=[ ]内要放置json文件对应含有的类。

小改一下,源码来自YouOnly_LiveOnce https://www.bilibili.com/read/cv18350780 出处bilibili

方法二:

  1. import os
  2. import numpy as np
  3. import json
  4. from glob import glob
  5. import cv2
  6. from sklearn.model_selection import train_test_split
  7. from os import getcwd
  8. classes = ["SmallCar", "BigCar", "G", "R", "RG", "Cyclist", "B", "NonMotorVeh"] # 输入json文件内含有的类
  9. labelme_path = "/home/lyn/Desktop/yolov5-master/datasets/" # 这个文件夹内同时放了jpg文件和json文件,生成的txt文件也会存放其中
  10. isUseTest = True # 是否创建test集
  11. # 3.获取待处理文件
  12. files = glob(labelme_path + "*.json")
  13. files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]
  14. print(files)
  15. if isUseTest:
  16. trainval_files, test_files = train_test_split(files, test_size=0.1, random_state=55)
  17. else:
  18. trainval_files = files
  19. # split
  20. train_files, val_files = train_test_split(trainval_files, test_size=0.1, random_state=55)
  21. def convert(size, box):
  22. dw = 1. / (size[0])
  23. dh = 1. / (size[1])
  24. x = (box[0] + box[1]) / 2.0 - 1
  25. y = (box[2] + box[3]) / 2.0 - 1
  26. w = box[1] - box[0]
  27. h = box[3] - box[2]
  28. x = x * dw
  29. w = w * dw
  30. y = y * dh
  31. h = h * dh
  32. return (x, y, w, h)
  33. wd = getcwd()
  34. print(wd)
  35. def ChangeToYolo5(files, txt_Name):
  36. if not os.path.exists('tmp/'):
  37. os.makedirs('tmp/')
  38. list_file = open('tmp/%s.txt' % (txt_Name), 'w')
  39. for json_file_ in files:
  40. json_filename = labelme_path + json_file_ + ".json"
  41. imagePath = labelme_path + json_file_ + ".jpg"
  42. list_file.write('%s/%s\n' % (wd, imagePath))
  43. out_file = open('%s/%s.txt' % (labelme_path, json_file_), 'w')
  44. json_file = json.load(open(json_filename, "r", encoding="utf-8"))
  45. height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape
  46. for multi in json_file["shapes"]:
  47. points = np.array(multi["points"])
  48. xmin = min(points[:, 0]) if min(points[:, 0]) > 0 else 0
  49. xmax = max(points[:, 0]) if max(points[:, 0]) > 0 else 0
  50. ymin = min(points[:, 1]) if min(points[:, 1]) > 0 else 0
  51. ymax = max(points[:, 1]) if max(points[:, 1]) > 0 else 0
  52. label = multi["label"]
  53. if xmax <= xmin:
  54. pass
  55. elif ymax <= ymin:
  56. pass
  57. else:
  58. cls_id = classes.index(label)
  59. b = (float(xmin), float(xmax), float(ymin), float(ymax))
  60. bb = convert((width, height), b)
  61. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  62. print(json_filename, xmin, ymin, xmax, ymax, cls_id)
  63. ChangeToYolo5(train_files, "train")
  64. ChangeToYolo5(val_files, "val")
  65. ChangeToYolo5(test_files, "test")

尝试后发现两个生成的txt文件点的坐标值略微有点区别,具体用哪一个本人水平不够看不出来哪个更好,能跑哪个是哪个吧。

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

闽ICP备14008679号