数据集互转目录
在做目标检测时,我个人比较喜欢VOC格式的数据集,所以遇到COCO格式的数据集习惯转为VOC的,再进行处理。
coco转voc格式
需要修改的只有路径以及jsonFileName 这个列表,都已经标明了比较清晰的注释。
'''
Author: TuZhou
Version: 1.0
Date: 2021-08-29 16:32:52
LastEditTime: 2021-08-29 17:44:57
LastEditors: TuZhou
Description:
FilePath: \My_Yolo\datasets\coco_to_voc.py
'''
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
#要生成的voc格式图片路径
image_dir = './datasets/RoadSignsPascalVOC_Voc/images'
#要生成的voc格式xml标注路径
anno_dir = './datasets/RoadSignsPascalVOC_Voc/annotations'
#---------------------------------------------#
#你的coco格式的json类型和文件名,前者表示是train类型的json,后者表示该json文件的名字,类型名最好是与你的对应类型图片保存文件夹名一致
#我的json目录只有一个train类型,如果你有测试集的json文件,则可写成[['train', 'instance_train'], ['test', 'instance_test']]
jsonFileName = [['train', 'instance_train']]
#----------------------------------------------#
'''
Author: TuZhou
Description: 若模型保存文件夹不存在,创建模型保存文件夹,若存在,删除重建
param {*} path 文件夹路径
return {*}
'''
def mkr(path):
if os.path.exists(path):
shutil.rmtree(path)
os.mkdir(path)
else:
os.mkdir(path)
'''
Author: TuZhou
Description: 保存xml文件
param {*} filename
param {*} objs
param {*} filepath
return {*}
'''
def save_annotations(filename, objs, filepath):
annopath = anno_dir + "/" + filename[:-3] + "xml" # 生成的xml文件保存路径
#print("filename", filename)
dst_path = image_dir + "/" + filename
img_path = filepath
img = cv2.imread(img_path)
#此处时对非RGB图像筛选,可注释
# 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']
#NOTE:dataType表示coco格式中训练集或者测试集的图片文件夹名,但是我所有图片都放在JPEGImages文件夹中,所以此处为空,有需要的可以修改
#dataType就是jsonFileName中的json类型,如果你的类型名和你的图片文件夹名一致,则可注释下行
dataType = ''
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'])+":"+cat['name'])
return classes
'''
Author: TuZhou
Description:
param {*} origin_anno_dir 原始coco的json文件目录
param {*} origin_image_dir 原始coco的图片保寸目录
param {*} verbose
return {*}
'''
def get_CK5(origin_anno_dir, origin_image_dir, verbose=False):
for dataType, annoName in jsonFileName:
#annFile = 'instances_{}.json'.format(dataType)
annFile = annoName + '.json'
annpath = os.path.join(origin_anno_dir, annFile)
coco = COCO(annpath)
classes = catid2name(coco)
imgIds = coco.getImgIds()
# imgIds=imgIds[0:1000]#测试用,抽取10张图片,看下存储效果
for imgId in tqdm(imgIds):
img = coco.loadImgs(imgId)[0]
showbycv(coco, dataType,