1. labelme标注完的json 

  Labelme标注工具的JSON文件包含了标注信息、图片路径、以及图片的高度和宽度等信息,

2. coco的json

  COCO数据集格式包含了多个JSON文件,包括标注信息、图片信息、类别信息、图片与类别的关联信息等

3.怎么转换

有两种方式

  1. 使用 Labelme 自带的转换工具
  2. 自己编写 Python 脚本进行转换

 

在windows上出了好多问题

步骤:

  1. 下载coco api

    git clone https://github.com/cocodataset/cocoapi.git

    cd cocoapi/PythonAPI

    python setup.py install

  2. 数据集划分

from pycocotools.coco import COCO
import numpy as np
import os
import shutil

annFile = 'path/to/annotation/file'
saveDir = 'path/to/save/directory'

coco = COCO(annFile)

ids = list(coco.imgs.keys())
np.random.shuffle(ids)

train_ids = ids[:int(0.8*len(ids))]
val_ids = ids[int(0.8*len(ids)):]

os.makedirs(os.path.join(saveDir, 'train'), exist_ok=True)
os.makedirs(os.path.join(saveDir, 'val'), exist_ok=True)

for img_id in train_ids:
    img_info = coco.loadImgs(ids=[img_id])[0]
    img_file_name = img_info['file_name']
    src_file_path = os.path.join(os.path.dirname(annFile), img_file_name)
    dst_file_path = os.path.join(saveDir, 'train', img_file_name)
    shutil.copy(src_file_path, dst_file_path)
    ann_ids = coco.getAnnIds(imgIds=[img_id])
    anns = coco.loadAnns(ann_ids)
    ann_dict = dict()
    ann_dict['images'] = [img_info]
    ann_dict['annotations'] = anns
    save_path = os.path.join(saveDir, 'train', os.path.splitext(img_file_name)[0]+'.json')
    with open(save_path, 'w') as f:
        json.dump(ann_dict, f)

for img_id in val_ids:
    img_info = coco.loadImgs(ids=[img_id])[0]
    img_file_name = img_info['file_name']
    src_file_path = os.path.join(os.path.dirname(annFile), img_file_name)
    dst_file_path = os.path.join(saveDir, 'val', img_file_name)
    shutil.copy(src_file_path, dst_file_path)
    ann_ids = coco.getAnnIds(imgIds=[img_id])
    anns = coco.loadAnns(ann_ids)
    ann_dict = dict()
    ann_dict['images'] = [img_info]
    ann_dict['annotations'] = anns
    save_path = os.path.join(saveDir, 'val', os.path.splitext(img_file_name)[0]+'.json')
    with open(save_path, 'w') as f:
        json.dump(ann_dict, f)

  3.  labelme 方式转换

  

  3.2 自己写脚本转换

posted on   黑逍逍  阅读(234)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!



点击右上角即可分享
微信分享提示