Labelme2Lable_Studio.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. if shape['label'] == 'Table_std':
  47. vale_dict['rectanglelabels'] = ['standard_table']
  48. elif shape['label'] == 'Table_0':
  49. vale_dict['rectanglelabels'] = ['unstandard_table']
  50. else:
  51. vale_dict['rectanglelabels'] = ['figure']
  52. result_dict['value'] = copy.deepcopy(vale_dict)
  53. result_list.append(copy.deepcopy(result_dict))
  54. annotations_dict['result'] = copy.deepcopy(result_list)
  55. if 'updated_by' in info:
  56. annotations_dict['updated_by'] = info['updated_by']
  57. annotations_list.append(copy.deepcopy(annotations_dict))
  58. label_dict['annotations'] = copy.deepcopy(annotations_list)
  59. # data_dict['image'] = '/data/upload/' + str(project_id) + '/' + os.path.basename(info['imagePath'])
  60. data_dict['image'] = os.path.join('http://192.168.10.18:10088', labelme_dir, os.path.basename(info['imagePath']))
  61. label_dict['data'] = copy.deepcopy(data_dict)
  62. if 'updated_by' in info:
  63. label_dict['updated_by'] = info['updated_by']
  64. all_label_list.append(copy.deepcopy(label_dict))
  65. f.close()
  66. # print(all_label_list)
  67. if not all_label_list:
  68. print('无有效json数据')
  69. else:
  70. json.dump(all_label_list, open(project_id + '.json', 'w', encoding='utf-8'),
  71. ensure_ascii=False, indent=2)
  72. if __name__ == '__main__':
  73. parser = argparse.ArgumentParser()
  74. parser.add_argument('--labelme_dirs', type=str, nargs="+", default=[])
  75. parser.add_argument('--project_id', type=str)
  76. args = parser.parse_args()
  77. Labelme2Label_Studio(args.labelme_dirs, args.project_id)