当前位置:   article > 正文

labelme标注的json文件转成coco128的格式_labelme to coco128-seg

labelme to coco128-seg

用Labelme标注的实例分割的label,用于训练实例分割模型,但是后来想用Yolov5训练检测模型,不想重新标注数据,就用实例分割取出外接矩形框,转成coco128的那种格式,用于yolov5的训练。

转换代码如下,其中主要是涉及到一些json格式的处理,自己主要是用的是jupyter notebook单步调试的,所以代码格式看起来稍微有点乱。

  1. # +
  2. #encoding: utf-8
  3. # -
  4. import json
  5. import cv2
  6. import base64
  7. import os
  8. def readJson(jsonfile):
  9. with open(jsonfile,'r') as f:
  10. jsonData = json.load(f)
  11. return jsonData
  12. def writeToJson(filePath,data):
  13. fb = open(filePath,'w')
  14. # json.dumps(data).decode('unicode-escape')
  15. fb.write(json.dumps(data,indent=2,ensure_ascii=False)) # ,encoding='utf-8'
  16. fb.close()
  17. # +
  18. #json_path = r'C:\Users\Administrator\Desktop\carton_data\carton_1_instance\1337_75.json'
  19. # +
  20. #contentJson = readJson(json_path)
  21. # +
  22. #print(contentJson['shapes'][0])
  23. # +
  24. #print(len(contentJson['shapes']))
  25. # -
  26. dicts = {"version":"4.2.7", "flags":{}, "shapes":[], "imagePath":"","imageData":"","imageHeight": 960, "imageWidth":1280}
  27. def getMinRect(points_coord):
  28. len_pts = len(points_coord)
  29. x_min = 10000
  30. y_min = 10000
  31. x_max = 0
  32. y_max = 0
  33. for i in range(len_pts):
  34. if points_coord[i][0] < x_min:
  35. x_min = points_coord[i][0]
  36. if points_coord[i][1] < y_min:
  37. y_min = points_coord[i][1]
  38. if points_coord[i][0] > x_max:
  39. x_max = points_coord[i][0]
  40. if points_coord[i][1] > y_max:
  41. y_max = points_coord[i][1]
  42. return x_min, y_min, x_max, y_max
  43. size = len(json_path.split('\\'))
  44. json_name = json_path.split('\\')[size - 1][:-4] + 'jpg'
  45. image_name = json_name
  46. for i in range(len(contentJson['shapes'])):
  47. x_min, y_min, x_max, y_max = getMinRect(contentJson['shapes'][i]["points"])
  48. tmp_rect_dict = {"label":"rect","points":[],"group_id":"","shape_type":"rectangle","flags":{}}
  49. pt1 = [x_min, y_min]
  50. pt2 = [x_max, y_max]
  51. tmp_rect_dict['points'].append(pt1)
  52. tmp_rect_dict['points'].append(pt2)
  53. dicts['shapes'].append(tmp_rect_dict)
  54. dicts['imagePath'] = image_name
  55. new_json = r'C:\Users\Administrator\Desktop\carton_data\test\yrdy.json'
  56. with open(new_json, "w") as dump_f:
  57. json.dump(dicts, dump_f)
  58. def file_name(file_dir):
  59. L = []
  60. for root, dirs, files in os.walk(file_dir):
  61. for file in files:
  62. if os.path.splitext(file)[1] == '.json':
  63. L.append(os.path.join(root, file))
  64. return L
  65. json_list = file_name(r'C:\Users\Administrator\Desktop\carton_data\carton_yolov5')
  66. print(json_list[0])
  67. for tmp_json in json_list:
  68. with open(tmp_json, "w+") as fp:
  69. size = len(tmp_json.split('\\'))
  70. json_name = tmp_json.split('\\')[size - 1][:-4] + 'jpg'
  71. image_name = json_name
  72. dicts = {"version":"4.2.7", "flags":{}, "shapes":[], "imagePath":"","imageData":"","imageHeight": 960, "imageWidth":1280}
  73. for i in range(len(contentJson['shapes'])):
  74. x_min, y_min, x_max, y_max = getMinRect(contentJson['shapes'][i]["points"])
  75. tmp_rect_dict = {"label":"rect","points":[],"group_id":"","shape_type":"rectangle","flags":{}}
  76. pt1 = [x_min, y_min]
  77. pt2 = [x_max, y_max]
  78. tmp_rect_dict['points'].append(pt1)
  79. tmp_rect_dict['points'].append(pt2)
  80. dicts['shapes'].append(tmp_rect_dict)
  81. dicts['imagePath'] = image_name
  82. os.remove(tmp_json)
  83. with open(tmp_json, "w+") as dump_f:
  84. json.dump(dicts, dump_f)
  85. test_json = r"C:\Users\Administrator\Desktop\carton_data\test\1337_0.json"
  86. contentJson = readJson(test_json)
  87. print(contentJson["shapes"][0])
  88. for tmp_json in json_list:
  89. contentJson = readJson(tmp_json)
  90. txt_name = tmp_json[:-4] + 'txt'
  91. with open(txt_name, 'w') as fp:
  92. for tmp_rect in contentJson["shapes"]:
  93. xmin = tmp_rect['points'][0][0]
  94. ymin = tmp_rect['points'][0][1]
  95. xmax = tmp_rect['points'][1][0]
  96. ymax = tmp_rect['points'][1][1]
  97. # image name
  98. image_name = tmp_json[:-4] + 'jpg'
  99. print(image_name)
  100. frame = cv2.imread(image_name)
  101. width = frame.shape[0]
  102. height = frame.shape[1]
  103. # normalize
  104. x0 = (xmin+xmax)*0.5/width
  105. y0 = (ymin+ymax)*0.5/height
  106. w0 = (xmax-xmin)*1.0/width
  107. h0 = (ymax-ymin)*1.0/height
  108. # save txt
  109. fp.write("0")
  110. fp.write(" ")
  111. fp.write(str(x0))
  112. fp.write(" ")
  113. fp.write(str(y0))
  114. fp.write(" ")
  115. fp.write(str(w0))
  116. fp.write(" ")
  117. fp.write(str(h0))
  118. fp.write("\n")

 

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

闽ICP备14008679号