• labelme标注的json数据集转换成coco数据集


    labelme软件标注的json文件,把图像和json单个文件生成coco训练的格式

    import os
    import json
    import numpy as np
    import glob
    import shutil
    import cv2
    from sklearn.model_selection import train_test_split
    
    np.random.seed(41)
    
    classname_to_id = {
        "ok2": 0,  # 改成自己的类别
        "ok10": 1
    }
    
    
    class Lableme2CoCo:
    
        def __init__(self):
            self.images = []
            self.annotations = []
            self.categories = []
            self.img_id = 0
            self.ann_id = 0
    
        def save_coco_json(self, instance, save_path):
            json.dump(instance, open(save_path, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)  # indent=2 更加美观显示
    
        # 由json文件构建COCO
        def to_coco(self, json_path_list):
            self._init_categories()
            for json_path in json_path_list:
                obj = self.read_jsonfile(json_path)
                self.images.append(self._image(obj, json_path))
                shapes = obj['shapes']
                for shape in shapes:
                    annotation = self._annotation(shape)
                    self.annotations.append(annotation)
                    self.ann_id += 1
                self.img_id += 1
            instance = {}
            instance['info'] = 'spytensor created'
            instance['license'] = ['license']
            instance['images'] = self.images
            instance['annotations'] = self.annotations
            instance['categories'] = self.categories
            return instance
    
        # 构建类别
        def _init_categories(self):
            for k, v in classname_to_id.items():
                category = {}
                category['id'] = v
                category['name'] = k
                self.categories.append(category)
    
        # 构建COCO的image字段
        def _image(self, obj, path):
            image = {}
            from labelme import utils
            img_x = utils.img_b64_to_arr(obj['imageData'])
            h, w = img_x.shape[:-1]
            image['height'] = h
            image['width'] = w
            image['id'] = self.img_id
            image['file_name'] = os.path.basename(path).replace(".json", ".jpg")
            return image
    
        # 构建COCO的annotation字段
        def _annotation(self, shape):
            # print('shape', shape)
            label = shape['label']
            points = shape['points']
            annotation = {}
            annotation['id'] = self.ann_id
            annotation['image_id'] = self.img_id
            annotation['category_id'] = int(classname_to_id[label])
            annotation['segmentation'] = [np.asarray(points).flatten().tolist()]
            annotation['bbox'] = self._get_box(points)
            annotation['iscrowd'] = 0
            annotation['area'] = 1.0
            return annotation
    
        # 读取json文件,返回一个json对象
        def read_jsonfile(self, path):
            with open(path, "r", encoding='utf-8') as f:
                return json.load(f)
    
        # COCO的格式: [x1,y1,w,h] 对应COCO的bbox格式
        def _get_box(self, points):
            min_x = min_y = np.inf
            max_x = max_y = 0
            for x, y in points:
                min_x = min(min_x, x)
                min_y = min(min_y, y)
                max_x = max(max_x, x)
                max_y = max(max_y, y)
            return [min_x, min_y, max_x - min_x, max_y - min_y]
    
    
    # 训练过程中,如果遇到Index put requires the source and destination dtypes match, got Long for the destination and Int for the source
    # 参考:https://github.com/open-mmlab/mmdetection/issues/6706
    if __name__ == '__main__':
        labelme_path = r"G:\bsh\dataset\flame_mini_dataset\train\images"  # json和图片的存放目录
        saved_coco_path = r"G:\bsh\dataset\flame_mini_dataset\train\labelmeannotations"  # 生成coco格式数据的保存文件夹名字
        print('reading...')
        # 创建文件
        if not os.path.exists("%scoco/annotations/" % saved_coco_path):
            os.makedirs("%scoco/annotations/" % saved_coco_path)
        if not os.path.exists("%scoco/images/train/" % saved_coco_path):
            os.makedirs("%scoco/images/train" % saved_coco_path)
        if not os.path.exists("%scoco/images/val/" % saved_coco_path):
            os.makedirs("%scoco/images/val" % saved_coco_path)
        # 获取images目录下所有的joson文件列表
        print(labelme_path + "/*.json")
        json_list_path = glob.glob(labelme_path + "/*.json")
        print('json_list_path: ', len(json_list_path))
        # 数据划分,这里没有区分val2017和tran2017目录,所有图片都放在images目录下
        train_path, val_path = train_test_split(json_list_path, test_size=0.2, train_size=0.8)
        print("train_n:", len(train_path), 'val_n:', len(val_path))
    
        # 把训练集转化为COCO的json格式
        l2c_train = Lableme2CoCo()
        train_instance = l2c_train.to_coco(train_path)
        l2c_train.save_coco_json(train_instance, '%scoco/annotations/instances_train.json' % saved_coco_path)
        for file in train_path:
            # shutil.copy(file.replace("json", "jpg"), "%scoco/images/train2017/" % saved_coco_path)
            img_name = file.replace('json', 'jpg')
            temp_img = cv2.imread(img_name)
            try:
                cv2.imwrite(
                    "{}coco/images/train/{}".format(saved_coco_path, img_name.split('\\')[-1].replace('png', 'jpg')),
                    temp_img)
            except Exception as e:
                print(e)
                print('Wrong Image:', img_name)
                continue
            print(img_name + '-->', img_name.replace('png', 'jpg'))
    
        for file in val_path:
            # shutil.copy(file.replace("json", "jpg"), "%scoco/images/val2017/" % saved_coco_path)
            img_name = file.replace('json', 'jpg')
            temp_img = cv2.imread(img_name)
            try:
                cv2.imwrite("{}coco/images/val/{}".format(saved_coco_path, img_name.split('\\')[-1].replace('png', 'jpg')),
                            temp_img)
            except Exception as e:
                print(e)
                print('Wrong Image:', img_name)
                continue
            print(img_name + '-->', img_name.replace('png', 'jpg'))
    
        # 把验证集转化为COCO的json格式
        l2c_val = Lableme2CoCo()
        val_instance = l2c_val.to_coco(val_path)
        l2c_val.save_coco_json(val_instance, '%scoco/annotations/instances_val.json' % saved_coco_path)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
  • 相关阅读:
    理解Spring原理 - 手写IOC和DI
    PostgreSQL的学习心得和知识总结(九十六)|深入理解PostgreSQL数据库开源MPP扩展Citus 分片表隐藏及显示 的实现原理
    RHCE学习 --- 第四次作业
    面试:activity生命周期
    Pytorch入门实战(8):小样本学习实现图片分类(Few-shot Learning, Meta Learning)
    [庆国庆 迎国庆 发文]云计算的概念
    什么是网站SSL证书?SSL证书重要吗?
    基于PSO粒子群算法的MPPT最大功率跟踪Simulink仿真,PSO采用S函数实现
    039_小驰私房菜_Camera perfermance debug
    uni-app中返回顶部的方法
  • 原文地址:https://blog.csdn.net/weixin_44298961/article/details/134054609