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 = 1. / (width)
dh = 1. / (height)
yoloanno_filename = "{}/{}.txt".format(out_dir, name)
yoloanno_file = open(yoloanno_filename, 'w')
if(len(anno) > 0):
for s_anno in anno:
x_center = s_anno["bbox"][0]+s_anno["bbox"][2]/2.0
y_center = s_anno["bbox"][1]+s_anno["bbox"][3]/2.0
w = s_anno["bbox"][2]
h = s_anno["bbox"][3]
x_center = x_center * dw
y_center = y_center * dh
w = w * dw
h = h * dh
cat_id = s_anno["category_id"]
bb = (x_center, y_center, w, h)
yoloanno_file.write(str(cat_id) + " " + " ".join([str(a) for a in bb]) + '\n')
if imgset_file:
img_names.append(name)
if img_names:
img_names.sort()
with open(imgset_file, "w") as f:
f.write("\n".join(img_names))
3. 调整编号
- 没有参数,python调用就行
- path是输入txt的路径,输出路径是path加8,比如path=val,则输出文件夹是val8,需要先新建val8。嗯,就是这么懒。
- cls没有用到,贴出来方便其他地方用
import os,sys
cls=["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light",
"fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep",
"cow","elephant","bear","zebra","giraffe","backpack","umbrella",
"handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove",
"skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon",
"bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut",
"cake","chair","couch","potted plant","bed","dining table","toilet",
"tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster"
,"sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"]
l=[]
for i in range(12):
l.append(i-1)
#12
l.append(99)
#13~25
for i in range(13,26):
l.append(i-2)
#26
l.append(99)
#27,28
l.append(24)
l.append(25)
#29,30
l.append(99)
l.append(99)
#31~44
for i in range(31,45):
l.append(i-5)
#45
l.append(99)
#46~65
for i in range(46,66):
l.append(i-6)
#66
l.append(99)
#67
l.append(60)
#68,69
l.append(99)
l.append(99)
#70
l.append(61)
#71
l.append(99)
#72~82
for i in range(72,83):
l.append(i-10)
#83
l.append(99)
#84~90
for i in range(84,91):
l.append(i-11)
print(l[0])
print(len(l))
path='./val'
labels=os.listdir(path)
for it in labels:
f = open(path+'8/'+it,"w")
tmp=[]
for line in open(path+'/'+it):
tmp.append(line)
for j in range(len(tmp)):
ind=tmp[j].split()
t=l[int(ind[0])]
if t==99:
print(it, ind)
f.write(str(t)+' ')
f.write(ind[1]+' ')
f.write(ind[2]+' ')
f.write(ind[3]+' ')
if j==len(tmp)-1:
f.write(ind[4])
else:
f.write(ind[4]+'\n')
f.close()
4. 验证
- 同样需要先新建输出文件夹,叫什么自己看吧
import cv2,os
'''
classes=["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light",
"fire hydrant","unkown","stop sign","parking meter","bench","bird","cat","dog","horse","sheep",
"cow","elephant","bear","zebra","giraffe","unkown","backpack","umbrella","unkown","unkown",
"handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove",
"skateboard","surfboard","tennis racket","bottle","unkown","wine glass","cup","fork","knife","spoon",
"bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut",
"cake","chair","couch","potted plant","bed","unkown","dining table","unkown","unkown","toilet",
"unkown","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster"
,"sink","refrigerator","unkown","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"]
'''
classes=["person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light",
"fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep",
"cow","elephant","bear","zebra","giraffe","backpack","umbrella",
"handbag","tie","suitcase","frisbee","skis","snowboard","sports ball","kite","baseball bat","baseball glove",
"skateboard","surfboard","tennis racket","bottle","wine glass","cup","fork","knife","spoon",
"bowl","banana","apple","sandwich","orange","broccoli","carrot","hot dog","pizza","donut",
"cake","chair","couch","potted plant","bed","dining table","toilet",
"tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven","toaster"
,"sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"]
hh="train"
def draw(image, label):
im = cv2.imread(image)
sz = im.shape
name = image.split('/')[-1]
for line in open(label):
item=line.split();
cx=float(item[1])*sz[1]
cy=float(item[2])*sz[0]
w=float(item[3])*sz[1]
h=float(item[4])*sz[0]
x = int(cx-w/2)
y = int(cy-h/2)
xx = x+int(w)
yy = y+int(h)
cv2.rectangle(im,(x,y),(xx,yy),(255,0,0),2)
l=int(item[0])
cv2.putText(im, classes[l], (int(cx), int(cy)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
cv2.imwrite(hh+"/"+name,im)
image_path="/home/data/coco/"+hh+"2017"
label_path="/home/data/coco/"+hh+"8"
images=os.listdir(image_path)
images.sort()
labels=os.listdir(label_path)
labels.sort()
for i in range(0, len(images)):
draw(image_path+"/"+images[i],label_path+"/"+labels[i])
5. 备注
- 生成train.txt的方法在这里找呦
版权声明:本文为random_repick原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。