解决coco转voc,object消失问题
step1 coco转voc代码
此代码借鉴于此博客:link
需要修改的地方
savepath
填写“提取出的类别”保存的地方,会生成两个子文件夹(images、annotations)
datasets_list
修改为自己的存放coco数据集的文件夹的名称
classes_names
修改为自己需要类的名字
dataDir
原coco数据集保存的目录
代码内容
from pycocotools.coco import COCO
import os
import shutil
from tqdm import tqdm
# import skimage.io as io
import matplotlib.pyplot as plt
import cv2
from PIL import Image, ImageDraw
# 存储路径
savepath = "/home/boni/Downloads/coco_extract/"
img_dir = savepath + 'images/'
anno_dir = savepath + 'Annotations/'
# datasets_list=['train2014', 'val2014']
datasets_list = ['train2017']
# 选择自己的类
classes_names = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'train', 'traffic', 'fire hydrant', 'stop sign']
# 原数据存储
dataDir = '/home/boni/Downloads/coco_extract'
headstr = """\
<annotation>
<folder>VOC</folder>
<filename>%s</filename>
<source>
<database>My Database</database>
<annotation>COCO</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>company</name>
</owner>
<size>
<width>%d</width>
<height>%d</height>
<depth>%d</depth>
</size>
<segmented>0</segmented>
"""
objstr = """\
<object>
<name>%s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%d</xmin>
<ymin>%d</ymin>
<xmax>%d</xmax>
<ymax>%d</ymax>
</bndbox>
</object>
"""
tailstr = '''\
</annotation>
'''
# if the dir is not exists,make it,else delete it
def mkr(path):
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
else:
os.mkdir(path)
mkr(img_dir)
mkr(anno_dir)
def id2name(coco):
classes = dict()
for cls in coco.dataset['categories']:
classes[cls['id']] = cls['name']
return classes
def write_xml(anno_path, head, objs, tail):
f = open(anno_path, "w")
f.write(head)
for obj in objs:
f.write(objstr % (obj[0], obj[1], obj[2], obj[3], obj[4]))
f.write(tail)
def save_annotations_and_imgs(coco, dataset, filename, objs):
# eg:COCO_train2014_000000196610.jpg-->COCO_train2014_000000196610.xml
anno_path = anno_dir + filename[:-3] + 'xml'
# img_path = dataDir + dataset + '/' + filename
img_path = dataDir + '/' + dataset + '/' + filename
print(img_path)
dst_imgpath = img_dir + filename
img = cv2.imread(img_path)
# if (img.shape[2] == 1):
# print(filename + " not a RGB image")
# return
shutil.copy(img_path, dst_imgpath)
head = headstr % (filename, img.shape[1], img.shape[0], img.shape[2])
tail = tailstr
write_xml(anno_path, head, objs, tail)
def showimg(coco, dataset, img, classes, cls_id, show=True):
global dataDir
I = Image.open('%s/%s/%s' % (dataDir, dataset, img['file_name']))
# Íš¹ýid£¬µÃµœ×¢Ê͵ÄÐÅÏ¢
annIds = coco.getAnnIds(imgIds=img['id'], catIds=cls_id, iscrowd=None)
# print(annIds)
anns = coco.loadAnns(annIds)
# print(anns)
# coco.showAnns(anns)
objs = []
for ann in anns:
class_name = classes[ann['category_id']]
if class_name in classes_names:
print(class_name)
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 = [class_name, xmin, ymin, xmax, ymax]
objs.append(obj)
draw = ImageDraw.Draw(I)
draw.rectangle([xmin, ymin, xmax, ymax])
if show:
plt.figure()
plt.axis('off')
plt.imshow(I)
plt.show()
return objs
for dataset in datasets_list:
# ./COCO/annotations/instances_train2014.json
annFile = '{}/annotations/instances_{}.json'.format(dataDir, dataset)
# COCO API for initializing annotated data
coco = COCO(annFile)
# show all classes in coco
classes = id2name(coco)
print(classes)
# [1, 2, 3, 4, 6, 8]
classes_ids = coco.getCatIds(catNms=classes_names)
print(classes_ids)
for cls in classes_names:
# Get ID number of this class
cls_id = coco.getCatIds(catNms=[cls])
img_ids = coco.getImgIds(catIds=cls_id)
print(cls, len(img_ids))
# imgIds=img_ids[0:10]
for imgId in tqdm(img_ids):
img = coco.loadImgs(imgId)[0]
filename = img['file_name']
# print(filename)
objs = showimg(coco, dataset, img, classes, classes_ids, show=False)
print(objs)
save_annotations_and_imgs(coco, dataset, filename, objs)
文档结构显示
step2:删除无object项的xml文件及对应的图片
# coding=utf-8
import xml.dom.minidom
import os
# 未连接前xml,images路径
file_path = '/home/boni/Downloads/coco_extract/Annotations/'
img_path = '/home/boni/Downloads/coco_extract/images/'
# 获取路径下所有的xml文档,并保存在列表xml_file中(xml_file是list类型)
xml_file = os.listdir(file_path)
# 获取list长度
# print(len(xml_file))
"""
获取每个元素并与之前的路径连接,对每个xml进行object标签遍历,
xml中有object标签就直接跳过,没有就直接删除对应的xml文档,
并删除JEPGImages下对应的图片
"""
for i in range(len(xml_file)):
# 连接并保存为新的路径
new_file_path = os.path.join(file_path, xml_file[i])
new_img_path = os.path.join(img_path, xml_file[i][:-3]+'jpg')
# print(new_file_path)
# print(new_img_path)
# 打开xml文档
dom = xml.dom.minidom.parse(new_file_path)
# 得到文档元素对象
root = dom.documentElement
bb = root.getElementsByTagName('object')
if bb.length == 0:
# print("no object")
# 删除无object的xml
os.remove(new_file_path)
# print("has remove this xml")
# 删除无object的xml对应的照片
os.remove(new_img_path)
# print("has remove this image")
else:
# print("have object")
print(bb.length)
如有不清楚的地方可关注留言