Labelme2Lable_Studio.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import copy
  2. import json
  3. import argparse
  4. import numpy
  5. import glob
  6. import os
  7. from tqdm import tqdm
  8. def Labelme2Label_Studio(labelme_dirs, project_id):
  9. all_label_list = []
  10. label_dict = {}
  11. data_dict = {}
  12. annotations_dict = {}
  13. result_dict = {}
  14. vale_dict = {}
  15. labelme_path = []
  16. for labelme_dir in labelme_dirs:
  17. list_path = glob.glob(labelme_dir + "/*.json")
  18. labelme_path.extend(list_path)
  19. # print(label_path)
  20. # 进行必要信息的json转换
  21. for label in tqdm(labelme_path):
  22. with open(label, 'r', encoding='utf-8') as f:
  23. info = json.load(f)
  24. if not info['shapes']:
  25. continue
  26. annotations_list = [] # 会把之前的也添加进来,必须重置
  27. result_list = [] # 同上
  28. width = info['imageWidth']
  29. height = info['imageHeight']
  30. result_dict['original_width'] = copy.deepcopy(width)
  31. result_dict['original_height'] = copy.deepcopy(height)
  32. result_dict['image_rotation'] = 0
  33. result_dict['from_name'] = 'label'
  34. result_dict['to_name'] = 'image'
  35. result_dict['type'] = 'rectanglelabels'
  36. result_dict['origin'] = 'manual'
  37. # 标注信息写入
  38. for shape in info['shapes']:
  39. # print(shape)
  40. # print(shape['points'][0][0])
  41. vale_dict['x'] = shape['points'][0][0] * 100 / copy.deepcopy(width)
  42. vale_dict['y'] = shape['points'][0][1] * 100 / copy.deepcopy(height)
  43. vale_dict['width'] = (shape['points'][1][0] - shape['points'][0][0]) * 100 / copy.deepcopy(width)
  44. vale_dict['height'] = (shape['points'][1][1] - shape['points'][0][1]) * 100 / copy.deepcopy(height)
  45. vale_dict['rotation'] = 0
  46. vale_dict['rectanglelabels'] = [copy.deepcopy(shape['label'])]
  47. result_dict['value'] = copy.deepcopy(vale_dict)
  48. result_list.append(copy.deepcopy(result_dict))
  49. annotations_dict['result'] = copy.deepcopy(result_list)
  50. if 'updated_by' in info:
  51. annotations_dict['updated_by'] = info['updated_by']
  52. annotations_list.append(copy.deepcopy(annotations_dict))
  53. label_dict['annotations'] = copy.deepcopy(annotations_list)
  54. data_dict['image'] = '/data/upload/' + str(project_id) + '/' + os.path.basename(info['imagePath'])
  55. label_dict['data'] = copy.deepcopy(data_dict)
  56. if 'updated_by' in info:
  57. label_dict['updated_by'] = info['updated_by']
  58. all_label_list.append(copy.deepcopy(label_dict))
  59. f.close()
  60. # print(all_label_list)
  61. if not all_label_list:
  62. print('无有效json数据')
  63. else:
  64. json.dump(all_label_list, open('project' + str(project_id) + '.json', 'w', encoding='utf-8'),
  65. ensure_ascii=False, indent=2)
  66. if __name__ == '__main__':
  67. parser = argparse.ArgumentParser()
  68. parser.add_argument('--labelme_dirs', type=str, nargs="+", default=[])
  69. parser.add_argument('--project_id', type=int)
  70. args = parser.parse_args()
  71. Labelme2Label_Studio(args.labelme_dirs, args.project_id)