Prechádzať zdrojové kódy

上传文件至 'Labelme_LabelStudio'

Labelme与Label_Studio标注文件相互转换
zhuzezhou 1 rok pred
rodič
commit
66c0786ff6

+ 68 - 0
Labelme_LabelStudio/Label_Studio2Labelme.py

@@ -0,0 +1,68 @@
+import copy
+import json
+import argparse
+import numpy
+import glob
+import os
+from tqdm import tqdm
+
+def Label_Studio2Labelme(label_Studio_json_dirs, labelme_json_dir):
+    labelme_json = {}
+    shape_dic = {}
+    jsons_path = []
+
+    for labelme_dir in label_Studio_json_dirs:
+        list_path = glob.glob(labelme_dir + "/*.json")
+        jsons_path.extend(list_path)
+
+    for Label_Studio_json in jsons_path:
+        with open(Label_Studio_json, 'r', encoding='utf-8') as f:
+            info = json.load(f)
+            for json_dic in info:
+                if not json_dic['annotations'][0]['result']:
+                    continue
+                shapes_list = []
+                labelme_json['version'] = '5.0.1'
+                labelme_json['flags'] = {}
+                for result_dic in tqdm(json_dic['annotations'][0]['result']):
+                    # print(len(result_dic))
+                    points_list = []
+                    # 进行标签改动
+                    if result_dic['value']['rectanglelabels'][0] == 'Pseudocode':
+                        shape_dic['label'] = 'Code'
+                    else:
+                        shape_dic['label'] = copy.deepcopy(result_dic['value']['rectanglelabels'][0])
+                    x1, y1 = result_dic['value']['x'] * result_dic['original_width'] / 100, \
+                             result_dic['value']['y'] * result_dic['original_height'] / 100
+                    points_list.append([x1, y1])
+                    x2, y2 = x1 + result_dic['value']['width'] * result_dic['original_width'] / 100, \
+                             y1 + result_dic['value']['height'] * result_dic['original_height'] / 100
+                    points_list.append([x2, y2])
+                    shape_dic['points'] = points_list
+                    shape_dic['group_id'] = None
+                    shape_dic['shape_type'] = result_dic['type'][:9]
+                    shape_dic['flags'] = {}
+                    shapes_list.append(copy.deepcopy(shape_dic))
+                labelme_json['shapes'] = shapes_list
+                labelme_json['imagePath'] = os.path.basename(json_dic['data']['image'])
+                labelme_json['imageData'] = None
+                labelme_json['imageHeight'] = json_dic['annotations'][0]['result'][0]['original_height']
+                labelme_json['imageWidth'] = json_dic['annotations'][0]['result'][0]['original_width']
+                labelme_json['updated_by'] = json_dic['updated_by']
+                json.dump(labelme_json,
+                          open(labelme_json_dir + '/' +
+                               str(os.path.splitext(os.path.basename(json_dic['data']['image']))[0]) + '.json',
+                               'w', encoding='utf-8'),
+                          ensure_ascii=False, indent=2)
+        f.close()
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--Label_Studio_json_dirs', type=str,
+                        nargs="+", default=[])
+    parser.add_argument('--Labelme_json_dir', type=str, default='')
+    args = parser.parse_args()
+
+    Label_Studio2Labelme(args.Label_Studio_json_dirs, args.Labelme_json_dir)
+
+

+ 82 - 0
Labelme_LabelStudio/Labelme2Lable_Studio.py

@@ -0,0 +1,82 @@
+import copy
+import json
+import argparse
+import numpy
+import glob
+import os
+from tqdm import tqdm
+
+def Labelme2Label_Studio(labelme_dirs, project_id):
+    all_label_list = []
+    label_dict = {}
+    data_dict = {}
+    annotations_dict = {}
+    result_dict = {}
+    vale_dict = {}
+    labelme_path = []
+
+    for labelme_dir in labelme_dirs:
+        list_path = glob.glob(labelme_dir + "/*.json")
+        labelme_path.extend(list_path)
+    # print(label_path)
+    # 进行必要信息的json转换
+    for label in tqdm(labelme_path):
+        with open(label, 'r', encoding='utf-8') as f:
+            info = json.load(f)
+            if not info['shapes']:
+                continue
+            annotations_list = []  # 会把之前的也添加进来,必须重置
+            result_list = []  # 同上
+            width = info['imageWidth']
+            height = info['imageHeight']
+            result_dict['original_width'] = copy.deepcopy(width)
+            result_dict['original_height'] = copy.deepcopy(height)
+            result_dict['image_rotation'] = 0
+            result_dict['from_name'] = 'label'
+            result_dict['to_name'] = 'image'
+            result_dict['type'] = 'rectanglelabels'
+            result_dict['origin'] = 'manual'
+            # 标注信息写入
+            for shape in info['shapes']:
+                # print(shape)
+                # print(shape['points'][0][0])
+                vale_dict['x'] = shape['points'][0][0] * 100 / copy.deepcopy(width)
+                vale_dict['y'] = shape['points'][0][1] * 100 / copy.deepcopy(height)
+                vale_dict['width'] = (shape['points'][1][0] - shape['points'][0][0]) * 100 / copy.deepcopy(width)
+                vale_dict['height'] = (shape['points'][1][1] - shape['points'][0][1]) * 100 / copy.deepcopy(height)
+                vale_dict['rotation'] = 0
+                vale_dict['rectanglelabels'] = [copy.deepcopy(shape['label'])]
+                result_dict['value'] = copy.deepcopy(vale_dict)
+                result_list.append(copy.deepcopy(result_dict))
+            annotations_dict['result'] = copy.deepcopy(result_list)
+            if 'updated_by' in info:
+                annotations_dict['updated_by'] = info['updated_by']
+            annotations_list.append(copy.deepcopy(annotations_dict))
+            label_dict['annotations'] = copy.deepcopy(annotations_list)
+            data_dict['image'] = '/data/upload/' + str(project_id) + '/' + os.path.basename(info['imagePath'])
+            label_dict['data'] = copy.deepcopy(data_dict)
+            if 'updated_by' in info:
+                label_dict['updated_by'] = info['updated_by']
+            all_label_list.append(copy.deepcopy(label_dict))
+        f.close()
+
+    # print(all_label_list)
+    if not all_label_list:
+        print('无有效json数据')
+    else:
+        json.dump(all_label_list, open('project' + str(project_id) + '.json', 'w', encoding='utf-8'),
+              ensure_ascii=False, indent=2)
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--labelme_dirs', type=str, nargs="+", default=[])
+    parser.add_argument('--project_id', type=int)
+    args = parser.parse_args()
+
+    Labelme2Label_Studio(args.labelme_dirs, args.project_id)
+
+
+
+
+
+