1. labelme标注完的json
Labelme标注工具的JSON文件包含了标注信息、图片路径、以及图片的高度和宽度等信息,
2. coco的json
COCO数据集格式包含了多个JSON文件,包括标注信息、图片信息、类别信息、图片与类别的关联信息等
3.怎么转换
有两种方式
- 使用 Labelme 自带的转换工具
- 自己编写 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 自己写脚本转换
标签:img,os,ids,file,coco,path,labelme,数据 From: https://www.cnblogs.com/mxleader/p/17289547.html