1。coco数据集简介,共标注了90类
1:person
2:bicycle
3:car
4:motorcycle
5:airplane
6:bus
7:train
8:truck
9:boat
10:traffic light
11:fire hydrant
13:stop sign
14:parking meter
15:bench
16:bird
17:cat
18:dog
19:horse
20:sheep
21:cow
22:elephant
23:bear
24:zebra
25:giraffe
27:backpack
28:umbrella
31:handbag
32:tie
33:suitcase
34:frisbee
35:skis
36:snowboard
37:sports ball
38:kite
39:baseball bat
40:baseball glove
41:skateboard
42:surfboard
43:tennis racket
44:bottle
46:wine glass
47:cup
48:fork
49:knife
50:spoon
51:bowl
52:banana
53:apple
54:sandwich
55:orange
56:broccoli
57:carrot
58:hot dog
59:pizza
60:donut
61:cake
62:chair
63:couch
64:potted plant
65:bed
67:dining table
70:toilet
72:tv
73:laptop
74:mouse
75:remote
76:keyboard
77:cell phone
78:microwave
79:oven
80:toaster
81:sink
82:refrigerator
84:book
85:clock
86:vase
87:scissors
88:teddy bear
89:hair drier
90:toothbrush
将coco数据转换成voc格式coco_to_voc.py
'''
把coco数据集合的所有标注转换到voc格式,不改变图片命名方式,
注意,原来有一些图片是黑白照片,检测出不是 RGB 图像,这样的图像不会被放到新的文件夹中
更新日期:2019-11-19
'''
from pycocotools.coco import COCO #这个包可以从git上下载https://github.com/cocodataset/cocoapi/tree/master/PythonAPI,也可以直接用修改后的coco.py
import os, cv2, shutil
from lxml import etree, objectify
from tqdm import tqdm
from PIL import Image
CKimg_dir ='/home/jerry/data/data/object_detect/coco2017_voc/images'
CKanno_dir = '/home/jerry/data/data/object_detect/coco2017_voc/annotations'
#若模型保存文件夹不存在,创建模型保存文件夹,若存在,删除重建
def mkr(path):
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
else:
os.mkdir(path)
def save_annotations(filename, objs,filepath):
annopath = CKanno_dir + "/" + filename[:-3] + "xml" #生成的xml文件保存路径
dst_path = CKimg_dir + "/" + filename
img_path=filepath
img = cv2.imread(img_path)
im = Image.open(img_path)
if im.mode != "RGB":
print(filename + " not a RGB image")
im.close()
return
im.close()
shutil.copy(img_path, dst_path)#把原始图像复制到目标文件夹
E = objectify.ElementMaker(annotate=False)
anno_tree = E.annotation(
E.folder('1'),
E.filename(filename),
E.source(
E.database('CKdemo'),
E.annotation('VOC'),
E.image('CK')
),
E.size(
E.width(img.shape[1]),
E.height(img.shape[0]),
E.depth(img.shape[2])
),
E.segmented(0)
)
for obj in objs:
E2 = objectify.ElementMaker(annotate=False)
anno_tree2 = E2.object(
E.name(obj[0]),
E.pose(),
E.truncated("0"),
E.difficult(0),
E.bndbox(
E.xmin(obj[2]),
E.ymin(obj[3]),
E.xmax(obj[4]),
E.ymax(obj[5])
)
)
anno_tree.append(anno_tree2)
etree.ElementTree(anno_tree).write(annopath, pretty_print=True)
def showbycv(coco, dataType, img, classes,origin_image_dir,verbose=False):
filename = img['file_name']
filepath=os.path.join(origin_image_dir,dataType,filename)
I = cv2.imread(filepath)
annIds = coco.getAnnIds(imgIds=img['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
objs = []
for ann in anns:
name = classes[ann['category_id']]
if 'bbox' in ann:
bbox = ann['bbox']
xmin = (int)(bbox[0])
ymin = (int)(bbox[1])
xmax = (int)(bbox[2] + bbox[0])
ymax = (int)(bbox[3] + bbox[1])
obj = [name, 1.0, xmin, ymin, xmax, ymax]
objs.append(obj)
if verbose:
cv2.rectangle(I, (xmin, ymin), (xmax, ymax), (255, 0, 0))
cv2.putText(I, name, (xmin, ymin), 3, 1, (0, 0, 255))
save_annotations(filename, objs,filepath)
if verbose:
cv2.imshow("img", I)
cv2.waitKey(0)
def catid2name(coco):#将名字和id号建立一个字典
classes = dict()
for cat in coco.dataset['categories']:
classes[cat['id']] = cat['name']
# print(str(cat['id'])