将labelme标注的数据转化为coco格式

如图,已经有了labelme标注好的所有的照片和json文件,

创建coco格式的数据集:

首先建一个文件夹:

包含以下4个文件夹:

 其中train2014里面放我们刚才所有的数据,然后运行labelme2coco.py文件,如下

# -*- coding:utf-8 -*-

import argparse
import json
import matplotlib.pyplot as plt
import skimage.io as io
# import cv2
from labelme import utils
import numpy as np
import glob
import PIL.Image


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 labelme2coco(object):
    def __init__(self, labelme_json=[], save_json_path='./tran.json'):
        self.labelme_json = labelme_json
        self.save_json_path = save_json_path
        self.images = []
        self.categories = []
        self.annotations = []
        # self.data_coco = {}
        self.label = []
        self.an
  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: Labelme是一种常用的图像标注工具,它可以帮助用户对图像进行标注和分割。而COCO是一种流行的图像数据格式,许多深度学习模型都使用COCO格式作为输入数据。因此,将Labelme标注的图像转化COCO格式是很有用的。 在实现这一目标时,有许多开源的工具和代码可供选择。其中,常用的是cocoapi和labelme2cocococoapi是由COCO团队开发的,它提供了Python API来处理COCO格式数据。而labelme2coco是一个第三方库,它可以将Labelme标注的图像转换为COCO格式。 要使用labelme2coco库,需要首先安装它。可以使用pip来安装它,只需在终端输入以下命令: ``` pip install labelme2coco ``` 安装成功后,就可以将Labelme标注的图像转换为COCO格式。首先需要导出Labelme的标注文件,即JSON格式的文件。然后使用以下Python代码: ``` from labelme2coco import labelme2coco labelme_json = "/path/to/labelme.json" # Labelme标注文件的路径 output_json = "/path/to/output.json" # 转换后输出文件的路径 labelme2coco(labelme_json, output_json) ``` 这样就可以将Labelme标注的图像转换为COCO格式,方便后续使用COCO格式数据进行训练和测试。 ### 回答2: Labelme是一个常用的Python工具,可用于生成用于对象检测任务的标注数据集。它使用JSON格式来记录标注信息。而COCO是一种广泛应用于计算机视觉领域的数据格式,也经常被用于图像分割和对象检测数据集的记录。 如果您需要将Labelme标注数据集转换为COCOJSON格式,可以使用以下两种方式: 1. 使用Python脚本:我们可以编写一个Python脚本,使用Labelme提供的工具函数将JSON文件转换为COCOJSON格式。 您需要提前下载以下软件包:Labelme、numpy和COCOAPI。这里提供一个参考代码,供您参考: ```python import os import json import numpy as np from pycocotools import mask as maskUtils from skimage import measure def labelme2coco(labelme_json): coco_output = {} coco_output['info'] = { 'year': 2021, 'version': '1.0', 'description': 'labelme to coco json format', 'contributor': 'anonymous', 'url': 'http://cocodataset.org', 'date_created': '2021-10-15 00:00:00.000000' } coco_output['licenses'] = [ { 'id': 1, 'name': 'Unknown License', 'url': 'http://creativecommons.org/licenses/by-nc-sa/2.0/' } ] coco_output['categories'] = [ { 'id': 0, 'name': 'background', 'supercategory': 'background', }, { 'id': 1, 'name': 'object', 'supercategory': 'object', } ] with open(labelme_json, 'r') as f: labelme = json.load(f) coco_output['images'] = [] coco_output['annotations'] = [] for i in range(len(labelme['images'])): filename = labelme['images'][i]['file_name'] height = labelme['images'][i]['height'] width = labelme['images'][i]['width'] image_id = i coco_output['images'].append({ 'file_name': filename, 'height': height, 'width': width, 'id': image_id }) for j in range(len(labelme['annotations'])): if labelme['annotations'][j]['image_id'] == i: segmentations = labelme['annotations'][j]['segmentation'] bbox = labelme['annotations'][j]['bbox'] class_id = 1 area = maskUtils.area(maskUtils.frPyObjects(segmentations, height, width)) annotation_id = len(coco_output['annotations']) coco_output['annotations'].append({ 'segmentation': segmentations, 'area': area.tolist()[0], 'iscrowd': 0, 'image_id': image_id, 'bbox': bbox, 'category_id': class_id, 'id': annotation_id }) return coco_output if __name__ == '__main__': labelme_json_file = 'labelme.json' output_file = os.path.splitext(labelme_json_file)[0] + '.coco.json' coco = labelme2coco(labelme_json_file) with open(output_file, 'w') as f: json.dump(coco, f) ``` 上述代码用于将`labelme.json`文件转换为`labelme.coco.json`文件。生成的COCOJSON文件将保存注释标记数据,以及图像和类别信息。 2. 使用网上提供的转换工具:目前也有一些通过网站提供转换服务,帮助用户将Labelme转换为COCOJSON格式的工具。常见的有convertcsv网站和人工智能vip网站,使用方法也很简单,只需上传labelme的JSON文件,并选择需要的输出格式即可。 总之,以上两种方法都适用于将Labelme标注数据集转换为COCOJSON格式。具体选择哪一种方法,根据自己的需求和熟悉程度自行决定即可。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值