1. 前言
coco数据库的标签用一个json保存,所以我们需要将它转化成一个个和图片对应的txt标签文件。
coco类别id从1到90,但其中有10个id没有,所以是80类,我们也将id调整到0到79
标签文件有两个instances_val2017.json和instances_train2017.json
2.生成txt是用python2写的,其他用的python3,注意版本,我这里3的代码区别也就在print吧
2. 生成txt
- 借鉴这篇博客,编写split.py
- 先安装模块:`sudo pip2 install pycocotools
- 使用方法:
python split.py --out-dir=val2017 --imgset-file=./test.txt instances_val2017.json
- out-dir是txt的保存路径,imgset-file我没有用到,只是不想改代码了,哈哈
from pycocotools.coco import COCO
import argparse
from collections import OrderedDict
import json
import os
from pprint import pprint
import sys
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
sys.path.append(os.path.dirname(sys.path[0]))
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
from pycocotools.coco import COCO
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = "Split whole json annotation to individual files.")
parser.add_argument("annofile",
help = "The file which contains all the annotations for a dataset in json format.")
parser.add_argument("--out-dir", default = "",
help = "The output directory where we store the annotation per image.")
parser.add_argument("--imgset-file", default = "",
help = "A file where we store all the image names of a dataset.")
args = parser.parse_args()
annofile = args.annofile
if not os.path.exists(annofile):
print "{} does not exist!".format(annofile)
sys.exit()
out_dir = args.out_dir
if out_dir:
if not os.path.exists(out_dir):
os.makedirs(out_dir)
imgset_file = args.imgset_file
if imgset_file:
imgset_dir = os.path.dirname(imgset_file)
if not os.path.exists(imgset_dir):
os.makedirs(imgset_dir)
# initialize COCO api.
coco = COCO(annofile)
img_ids = coco.getImgIds()
img_names = []
for img_id in img_ids:
# get image info
img = coco.loadImgs(img_id)
file_name = img[0]["file_name"]
name = os.path.splitext(file_name)[0]
if out_dir:
# get annotation info
anno_ids = coco.getAnnIds(imgIds=img_id, iscrowd=None)
anno = coco.loadAnns(anno_ids)
# save annotation to file
img_anno = dict()
img_anno["image"] = img[0]
img_anno["annotation"] = anno
width = img[0]["width"]
height = img[0]["height"]
dw =