当前位置:   article > 正文

将labelme标记的数据转为coco格式_labelme to coco

labelme to coco

将labelme标记的数据转为coco格式

将labelme标记的数据转为coco格式

这次转化主要是为了使用官方的mask_rcnn源码。
coco数据集主要是images,categories,annotations(数据格式都是list)
coco是把所有图片的信息都整合在一起了,成了一个dict

images:主要有height,width,id,file_name#id是图片id
categories:主要有supercategory,id,name#id是category的id
annotations:主要有segmentation,iscrowd,image_id,bbox,area,category_id,id
要注意在annotations里id之间的关系

coco的格式是
{
images:[{height,width,id,file_name}, {height,width,id,file_name}…]
categories:[{supercategory,id,name},{supercategory,id,name}…]
annotations:[{segmentation,iscrowd,image_id,bbox,area,category_id,id},{segmentation,iscrowd,image_id,bbox,area,category_id,id}]

转为coco格式

import numpy as np
import json
import glob
class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)
class tococo(object):
    def __init__(self,jsonfile,save_path):
        self.images = []
        self.categories = []
        self.annotations = []
        self.jsonfile = jsonfile
        self.save_path = save_path#保存json的路径
        self.class_ids = {"BG":0}
        self.class_id = 0
        self.coco = {}
        

    def labelme_to_coco(self):
        annid = 0
        for num,json_file in enumerate(self.jsonfile):
            data = open(json_file,"r")
            data = json.load(data)
            self.images.append(self.get_images(data["imagePath"],data["imageHeight"],data["imageWidth"],num))
            shapes = data["shapes"]
            for shape in shapes:
                
                if shape["label"] in self.class_ids:
                    
                    
                    pass
                if shape["label"] not in self.class_ids and shape["label"] == "car":
                    self.class_id = self.class_id +1
                      
                    self.class_ids[shape["label"]] = self.class_id
                    self.categories.append(self.get_categories(shape["label"],self.class_id))
                  
                self.annotations.append(self.get_annotations(shape["points"],num,annid,shape["label"]))
                
                  
                annid = annid+1


              
 
        
        
        self.coco["images"] = self.images
        self.coco["categories"] = self.categories
        self.coco["annotations"] = self.annotations
        
        
    def get_images(self,filename,height,width,image_id):
        image = {}
        image["height"] = height
        image['width'] = width
        image["id"] = image_id
        image["file_name"] = filename
        return image
    def get_categories(self,name,class_id):
        category = {}
        category["supercategory"] = "Cancer"
        category['id'] = class_id
        category['name'] = name

        return category
    def get_annotations(self,points,image_id,ann_id,calss_name):
        annotation = {}
        mins = np.amin(points,axis = 0)
        maxs = np.amax(points,axis = 0)
        wh = maxs -mins
        x = mins[0]
        y = mins[1]
        w = wh[0]
        h = wh[1]
        area = w*h
        annotation['segmentation'] = [list(np.asarray(points).flatten())]
        annotation['iscrowd'] = 0
        annotation['image_id'] = image_id
        annotation['bbox'] = [x,y,w,h]
        annotation['area'] = area
        annotation['category_id'] = self.class_ids[calss_name]
        annotation['id'] = ann_id
        return annotation
    
    def save_json(self):
        self.labelme_to_coco()
        coco_data = self.coco
        # 保存json文件
        json.dump(coco_data, open(self.save_path, 'w'), indent=4, cls=MyEncoder)  # indent=4 更加美观显示
import glob      


labelme_json = glob.glob('G:/train/*.json')
c = tococo(labelme_json, 'G:/data/parking/cars//annotations/train232.json')      
c.save_json()  
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/542950
推荐阅读
相关标签
  

闽ICP备14008679号