在计算机视觉的领域内,目标检测技术已经成为研究和应用的热点,其在安防监控、智能交通、人机交互等众多领域展现出广泛的应用价值。随着深度学习技术的发展,越来越多高效的目标检测算法被提出,如SSD、YOLO、Faster R-CNN等。为了训练和评估这些算法,研究者和工程师们通常需要依赖于大量带有标记的图像数据集,而COCO、VOC和YOLO便是其中最为常见的几种数据集格式。
COCO(Common Objects in Context)数据集由微软团队开发,以其丰富的标注信息和多样的场景类型而闻名;VOC(Visual Object Classes)数据集由PASCAL挑战赛推出,长期以来一直是计算机视觉研究的基石之一;而YOLO(You Only Look Once)数据集格式则源于YOLO目标检测算法的发明者,以其简洁高效的特点被广泛采用。这三种格式各有特色,适应不同的研究与应用需求,但在实际应用过程中,我们往往需要在这些格式之间进行转换,以满足特定算法的输入需求或利用不同来源的数据资源。
正确理解和掌握COCO、VOC和YOLO三种数据集格式之间的相互转换机制,不仅能够帮助研究者和开发人员提高工作效率,而且有助于深入理解各种目标检测算法对数据的不同处理方式和需求。本文将详细介绍这三种常见的数据集格式,探讨它们之间的转换方法,并提供实用的代码示例和操作指南,旨在帮助读者轻松应对目标检测中的数据集格式转换问题。
文章目录
数据集格式介绍
1.VOC格式
Pascal VOC是计算机视觉领域中一种广泛使用的数据集格式,由视觉对象类别挑战赛(Pascal Visual Object Classes Challenge)推出。它旨在为对象检测、图像分割、图像分类等任务提供一个标准的数据集格式和评估体系。VOC数据集格式由于其结构清晰、标注细致,成为了研究和开发视觉模型常用的数据集之一。
VOC数据结构
Pascal VOC数据集主要包括图像文件和对应的标注文件。标注信息以XML格式存储,每张图片对应一个XML文件。这些XML文件包含了图像的详细标注信息,如对象边界框(BoundingBox)、类别名、分割掩码(Segmentation mask)等。
XML标注文件主要内容
- folder:图片所在的文件夹名称。
- filename:图片的文件名。
- size:包含图片的尺寸信息,如宽度(width)、高度(height)和深度(depth,通常是颜色通道数)。
- segmented:标示图像是否用于分割任务。
- object:标记在图像中的对象,主要包含以下信息:
- name:对象的类别名称。
- pose:对象的姿态描述。
- truncated:表示对象是否被截断。
- difficult:表示对象是否难以识别。
- bndbox:对象的边界框,通常包含左上角坐标(xmin, ymin)和右下角坐标(xmax, ymax)。
举例说明
以下是一个Pascal VOC标注文件简单的例子,展示了一个包含两个对象(“cat"和"dog”)的图像标注:
<annotation>
<folder>Images</folder>
<filename>cat_dog.jpg</filename>
<size>
<width>500</width>
<height>375</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>cat</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>48</xmin>
<ymin>240</ymin>
<xmax>195</xmax>
<ymax>371</ymax>
</bndbox>
</object>
<object>
<name>dog</name>
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>8</xmin>
<ymin>12</ymin>
<xmax>352</xmax>
<ymax>498</ymax>
</bndbox>
</object>
</annotation>
在实际应用中,研究人员和开发者可以根据自己的需要使用或生成满足Pascal VOC标准的数据集,以便在各种视觉任务中训练和评估模型。
2.COCO格式
COCO(Common Objects in Context)数据集是计算机视觉研究领域中一个极为重要和广泛使用的数据集。它由微软团队发起,旨在推动场景理解的研究前进。COCO数据集专注于检测、分割和描述在日常场景中常见的物体,为研究目标检测、语义分割、目标分割(实例分割)、人体关键点检测、图像字幕等提供了丰富的数据支持。
COCO数据集格式
COCO数据集的标注信息是以JSON格式存储的,这种格式具有良好的可读性和便于程序处理的特点。一个COCO数据集的JSON文件通常包含以下几个主要的字段:
- images:包含了数据集中所有图像的基础信息,如图像ID(id),图像的宽(width)和高(height),图像的文件名(file_name)等。
- annotations:包含了对图像中物体的标注信息,例如物体的类别(category_id),物体所在的图像ID(image_id),以及物体的位置信息(如Bounding Box或多边形描述等)。
- categories:定义了数据集中所有物体类别的信息,包括类别ID(id)和类别名称(name)。
- licenses和info:分别提供了图像版权的许可信息和关于数据集的一般信息,例如版本、描述、贡献者等。
标注实例
以下是COCO数据集部分标注信息的简化示例:
{
"images": [
{
"file_name": "000000123456.jpg",
"height": 768,
"width": 1024,
"id": 123456
}
],
"annotations": [
{
"segmentation": [[224.24, 297.18, ...]],
"area": 1481.56,
"iscrowd": 0,
"image_id": 123456,
"bbox": [209.71, 297.18, 34.69, 42.63],
"category_id": 1,
"id": 56789
}
],
"categories": [
{"id": 1, "name": "person"},
{"id": 2, "name": "bicycle"},
// ... more categories
],
"info": {
"description": "COCO 2024 Dataset",
// Further information
},
"licenses": [
{
"id": 1,
"name": "Attribution License",
// More details
}
]
}
在实际应用中,研究人员和开发者可以利用COCO数据集提供的丰富标注信息来训练和评估各种视觉模型,推动计算机视觉技术的发展。COCO数据集由于其数据量大、类别多、任务多样的特点,成为了计算机视觉领域内最具影响力和最为常用的基准数据集之一。
3.YOLO格式
YOLO(You Only Look Once)是一种流行的目标检测算法,它的数据集格式主要用于快速有效地训练和测试目标检测模型。YOLO数据集格式因其简单易用而广受研究人员和开发者的欢迎。这种格式主要由两部分组成:图像文件和对应的标注文本文件。
图像文件
图像文件包括了数据集中的所有图片,可以是不同尺寸的JPEG或PNG等格式。
标注文本文件
每张图片都有一个对应的标注文本文件,文件名与图片相同,但扩展名为.txt
。在YOLO数据集格式中,一个标注文本文件包含了图片中所有检测对象的标注信息。每个对象由一行代表,这行数据包含了五个值:
- 对象类别ID:一个整数,表示对象的类别。类别ID通常从0开始编号。
- 中心点坐标X:浮点数,表示对象边界框中心点的X坐标,以图像宽度的比例表示。
- 中心点坐标Y:浮点数,表示对象边界框中心点的Y坐标,以图像高度的比例表示。
- 边界框宽度:浮点数,表示对象边界框的宽度,以图像宽度的比例表示。
- 边界框高度:浮点数,表示对象边界框的高度,以图像高度的比例表示。
所有的坐标和尺寸都被归一化到了[0, 1]区间内,这使得模型训练更为方便,同时也使得YOLO能够适应不同尺寸的输入图片。
示例
假设在一张图像中,有一个属于第1类(类别ID为0)的对象,该对象的边界框中心点位于整个图像宽度和高度的50%位置,边界框的宽度为图像宽度的30%,高度为图像高度的40%,对应的标注文本文件将包含如下内容:
0 0.5 0.5 0.3 0.4
如果图片中有多个对象,每个对象的标注就占据文件中的一行。
注意事项
- YOLO的数据预处理阶段将这些归一化的坐标转换回实际的像素坐标,以便进行目标检测。
- 类别ID与实际的对象类别之间的映射关系通常定义在一个单独的类别标签文件中(如
coco.names
),每行对应一个对象类别。
YOLO数据集格式的简洁性使得在准备和处理数据集时更加高效,同时也便于深度学习框架的集成和使用。
格式转换
一、coco转voc
代码如下(示例):
from pycocotools.coco import COCO
import os
from lxml import etree, objectify
import shutil
from tqdm import tqdm
import sys
import argparse
# 将类别名字和id建立索引
def catid2name(coco):
classes = dict()
for cat in coco.dataset['categories']:
classes[cat['id']] = cat['name']
return classes
# 将标签信息写入xml
def save_anno_to_xml(filename, size, objs, save_path):
E = objectify.ElementMaker(annotate=False)
anno_tree = E.annotation(
E.folder("DATA"),
E.filename(filename),
E.source(
E.database("The VOC Database"),
E.annotation("PASCAL VOC"),
E.image("flickr")
),
E.size(
E.width(size['width']),
E.height(size['height']),
E.depth(size['depth'])
),
E.segmented(0)
)
for obj in objs:
E2 = objectify.ElementMaker(annotate=False)
anno_tree2 = E2.object(
E.name(obj[0]),
E.pose("Unspecified"),
E.truncated(0),
E.difficult(0),
E.bndbox(
E.xmin(obj[1]),
E.ymin(obj[2]),
E.xmax(obj[3]),
E.ymax(obj[4])
)
)
anno_tree.append(anno_tree2)
anno_path = os.path.join(save_path, filename[:-3] + "xml")
etree.ElementTree(anno_tree).write(anno_path, pretty_print=True)
# 利用cocoAPI从json中加载信息
def load_coco(anno_file, xml_save_path):
if os.path.exists(xml_save_path):
shutil.rmtree(xml_save_path)
os.makedirs(xml_save_path)
coco = COCO(anno_file)
classes = catid2name(coco)
imgIds = coco.getImgIds()
classesIds = coco.getCatIds()
for imgId in tqdm(imgIds):
size = {}
img = coco.loadImgs(imgId)[0]
filename = img['file_name']
width = img['width']
height = img['height']
size['width'] = width
size['height'] = height
size['depth'] = 3
annIds = coco.getAnnIds(imgIds=img['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
objs = []
for ann in anns:
object_name = classes[ann['category_id']]
# bbox:[x,y,w,h]
bbox = list(map(int, ann['bbox']))
xmin = bbox[0]
ymin = bbox[1]
xmax = bbox[0] + bbox[2]
ymax = bbox[1] + bbox[3]
obj = [object_name, xmin, ymin, xmax, ymax]
objs.append(obj)
save_anno_to_xml(filename, size, objs, xml_save_path)
def parseJsonFile(data_dir, xmls_save_path):
assert os.path.exists(data_dir), "data dir:{} does not exits".format(data_dir)
if os.path.isdir(data_dir):
data_types = ['train2017', 'val2017']
for data_type in data_types:
ann_file = 'instances_{}.json'.format(data_type)
xmls_save_path = os.path.join(xmls_save_path, data_type)
load_coco(ann_file, xmls_save_path)
elif os.path.isfile(data_dir):
anno_file = data_dir
load_coco(anno_file, xmls_save_path)
if __name__ == '__main__':
"""
脚本说明:
该脚本用于将coco格式的json文件转换为voc格式的xml文件
参数说明:
data_dir:json文件的路径
xml_save_path:xml输出路径
"""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data-dir', type=str, default='./data/labels/coco/train.json', help='json path')
parser.add_argument('-s', '--save-path', type=str, default='./data/convert/voc', help='xml save path')
opt = parser.parse_args()
print(opt)
if len(sys.argv) > 1:
parseJsonFile(opt.data_dir, opt.save_path)
else:
data_dir = './data/labels/coco/train.json'
xml_save_path = './data/convert/voc'
parseJsonFile(data_dir=data_dir, xmls_save_path=xml_save_path)
二、coco转yolo
代码如下(示例):
from pycocotools.coco import COCO
import os
import shutil
from tqdm import tqdm
import sys
import argparse
images_nums = 0
category_nums = 0
bbox_nums = 0
# 将类别名字和id建立索引
def catid2name(coco):
classes = dict()
for cat in coco.dataset['categories']:
classes[cat['id']] = cat['name']
return classes
# 将[xmin,ymin,xmax,ymax]转换为yolo格式[x_center, y_center, w, h](做归一化)
def xyxy2xywhn(object, width, height):
cat_id = object[0]
xn = object[1] / width
yn = object[2] / height
wn = object[3] / width
hn = object[4] / height
out = "{} {:.5f} {:.5f} {:.5f} {:.5f}".format(cat_id, xn, yn, wn, hn)
return out
def save_anno_to_txt(images_info, save_path):
filename = images_info['filename']
txt_name = filename[:-3] + "txt"
with open(os.path.join(save_path, txt_name), "w") as f:
for obj in images_info['objects']:
line = xyxy2xywhn(obj, images_info['width'], images_info['height'])
f.write("{}\n".format(line))
# 利用cocoAPI从json中加载信息
def load_coco(anno_file, xml_save_path):
if os.path.exists(xml_save_path):
shutil.rmtree(xml_save_path)
os.makedirs(xml_save_path)
coco = COCO(anno_file)
classes = catid2name(coco)
imgIds = coco.getImgIds()
classesIds = coco.getCatIds()
with open(os.path.join(xml_save_path, "classes.txt"), 'w') as f:
for id in classesIds:
f.write("{}\n".format(classes[id]))
for imgId in tqdm(imgIds):
info = {}
img = coco.loadImgs(imgId)[0]
filename = img['file_name']
width = img['width']
height = img['height']
info['filename'] = filename
info['width'] = width
info['height'] = height
annIds = coco.getAnnIds(imgIds=img['id'], iscrowd=None)
anns = coco.loadAnns(annIds)
objs = []
for ann in anns:
object_name = classes[ann['category_id']]
# bbox:[x,y,w,h]
bbox = list(map(float, ann['bbox']))
xc = bbox[0] + bbox[2] / 2.
yc = bbox[1] + bbox[3] / 2.
w = bbox[2]
h = bbox[3]
obj = [ann['category_id'], xc, yc, w, h]
objs.append(obj)
info['objects'] = objs
save_anno_to_txt(info, xml_save_path)
def parseJsonFile(json_path, txt_save_path):
assert os.path.exists(json_path), "json path:{} does not exists".format(json_path)
if os.path.exists(txt_save_path):
shutil.rmtree(txt_save_path)
os.makedirs(txt_save_path)
assert json_path.endswith('json'), "json file:{} It is not json file!".format(json_path)
load_coco(json_path, txt_save_path)
if __name__ == '__main__':
"""
脚本说明:
该脚本用于将coco格式的json文件转换为yolo格式的txt文件
参数说明:
json_path:json文件的路径
txt_save_path:txt保存的路径
"""
parser = argparse.ArgumentParser()
parser.add_argument('-jp', '--json-path', type=str, default='./data/labels/coco/train.json', help='json path')
parser.add_argument('-s', '--save-path', type=str, default='./data/convert/yolo', help='txt save path')
opt = parser.parse_args()
if len(sys.argv) > 1:
print(opt)
parseJsonFile(opt.json_path, opt.save_path)
# print("image nums: {}".format(images_nums))
# print("category nums: {}".format(category_nums))
# print("bbox nums: {}".format(bbox_nums))
else:
json_path = './data/labels/coco/train.json' # r'D:\practice\compete\goodsDec\data\train\train.json'
txt_save_path = './data/convert/yolo'
parseJsonFile(json_path, txt_save_path)
# print("image nums: {}".format(images_nums))
# print("category nums: {}".format(category_nums))
# print("bbox nums: {}".format(bbox_nums))
三、voc转coco
代码如下(示例):
import xml.etree.ElementTree as ET
import os
import json
from datetime import datetime
import sys
import argparse
coco = dict()
coco['images'] = []
coco['type'] = 'instances'
coco['annotations'] = []
coco['categories'] = []
category_set = dict()
image_set = set()
category_item_id = -1
image_id = 000000
annotation_id = 0
def addCatItem(name):
global category_item_id
category_item = dict()
category_item['supercategory'] = 'none'
category_item_id += 1
category_item['id'] = category_item_id
category_item['name'] = name
coco['categories'].append(category_item)
category_set[name] = category_item_id
return category_item_id
def addImgItem(file_name, size):
global image_id
if file_name is None:
raise Exception('Could not find filename tag in xml file.')
if size['width'] is None:
raise Exception('Could not find width tag in xml file.')
if size['height'] is None:
raise Exception('Could not find height tag in xml file.')
image_id += 1
image_item = dict()
image_item['id'] = image_id
image_item['file_name'] = file_name
image_item['width'] = size['width']
image_item['height'] = size['height']
image_item['license'] = None
image_item['flickr_url'] = None
image_item['coco_url'] = None
image_item['date_captured'] = str(datetime.today())
coco['images'].append(image_item)
image_set.add(file_name)
return image_id
def addAnnoItem(object_name, image_id, category_id, bbox):
global annotation_id
annotation_item = dict()
annotation_item['segmentation'] = []
seg = []
# bbox[] is x,y,w,h
# left_top
seg.append(bbox[0])
seg.append(bbox[1])
# left_bottom
seg.append(bbox[0])
seg.append(bbox[1] + bbox[3])
# right_bottom
seg.append(bbox[0] + bbox[2])
seg.append(bbox[1] + bbox[3])
# right_top
seg.append(bbox[0] + bbox[2])
seg.append(bbox[1])
annotation_item['segmentation'].append(seg)
annotation_item['area'] = bbox[2] * bbox[3]
annotation_item['iscrowd'] = 0
annotation_item['ignore'] = 0
annotation_item['image_id'] = image_id
annotation_item['bbox'] = bbox
annotation_item['category_id'] = category_id
annotation_id += 1
annotation_item['id'] = annotation_id
coco['annotations'].append(annotation_item)
def read_image_ids(image_sets_file):
ids = []
with open(image_sets_file, 'r') as f:
for line in f.readlines():
ids.append(line.strip())
return ids
def parseXmlFilse(data_dir, json_save_path, split='train'):
assert os.path.exists(data_dir), "data path:{} does not exist".format(data_dir)
labelfile = split + ".txt"
image_sets_file = os.path.join(data_dir, "ImageSets", "Main", labelfile)
xml_files_list = []
if os.path.isfile(image_sets_file):
ids = read_image_ids(image_sets_file)
xml_files_list = [os.path.join(data_dir, "Annotations", f"{i}.xml") for i in ids]
elif os.path.isdir(data_dir):
# 修改此处xml的路径即可
# xml_dir = os.path.join(data_dir,"labels/voc")
xml_dir = data_dir
xml_list = os.listdir(xml_dir)
xml_files_list = [os.path.join(xml_dir, i) for i in xml_list]
for xml_file in xml_files_list:
if not xml_file.endswith('.xml'):
continue
tree = ET.parse(xml_file)
root = tree.getroot()
# 初始化
size = dict()
size['width'] = None
size['height'] = None
if root.tag != 'annotation':
raise Exception('pascal voc xml root element should be annotation, rather than {}'.format(root.tag))
# 提取图片名字
file_name = root.findtext('filename')
assert file_name is not None, "filename is not in the file"
# 提取图片 size {width,height,depth}
size_info = root.findall('size')
assert size_info is not None, "size is not in the file"
for subelem in size_info[0]:
size[subelem.tag] = int(subelem.text)
if file_name is not None and size['width'] is not None and file_name not in image_set:
# 添加coco['image'],返回当前图片ID
current_image_id = addImgItem(file_name, size)
print('add image with name: {}\tand\tsize: {}'.format(file_name, size))
elif file_name in image_set:
raise Exception('file_name duplicated')
else:
raise Exception("file name:{}\t size:{}".format(file_name, size))
# 提取一张图片内所有目标object标注信息
object_info = root.findall('object')
if len(object_info) == 0:
continue
# 遍历每个目标的标注信息
for object in object_info:
# 提取目标名字
object_name = object.findtext('name')
if object_name not in category_set:
# 创建类别索引
current_category_id = addCatItem(object_name)
else:
current_category_id = category_set[object_name]
# 初始化标签列表
bndbox = dict()
bndbox['xmin'] = None
bndbox['xmax'] = None
bndbox['ymin'] = None
bndbox['ymax'] = None
# 提取box:[xmin,ymin,xmax,ymax]
bndbox_info = object.findall('bndbox')
for box in bndbox_info[0]:
bndbox[box.tag] = int(box.text)
if bndbox['xmin'] is not None:
if object_name is None:
raise Exception('xml structure broken at bndbox tag')
if current_image_id is None:
raise Exception('xml structure broken at bndbox tag')
if current_category_id is None:
raise Exception('xml structure broken at bndbox tag')
bbox = []
# x
bbox.append(bndbox['xmin'])
# y
bbox.append(bndbox['ymin'])
# w
bbox.append(bndbox['xmax'] - bndbox['xmin'])
# h
bbox.append(bndbox['ymax'] - bndbox['ymin'])
print('add annotation with object_name:{}\timage_id:{}\tcat_id:{}\tbbox:{}'.format(object_name,
current_image_id,
current_category_id,
bbox))
addAnnoItem(object_name, current_image_id, current_category_id, bbox)
json_parent_dir = os.path.dirname(json_save_path)
if not os.path.exists(json_parent_dir):
os.makedirs(json_parent_dir)
json.dump(coco, open(json_save_path, 'w'))
print("class nums:{}".format(len(coco['categories'])))
print("image nums:{}".format(len(coco['images'])))
print("bbox nums:{}".format(len(coco['annotations'])))
if __name__ == '__main__':
"""
脚本说明:
本脚本用于将VOC格式的标注文件.xml转换为coco格式的标注文件.json
参数说明:
voc_data_dir:两种格式
1.voc2012文件夹的路径,会自动找到voc2012/imageSets/Main/xx.txt
2.xml标签文件存放的文件夹
json_save_path:json文件输出的文件夹
split:主要用于voc2012查找xx.txt,如train.txt.如果用格式2,则不会用到该参数
"""
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--voc-dir', type=str, default='data/label/voc', help='voc path')
parser.add_argument('-s', '--save-path', type=str, default='./data/convert/coco/train.json', help='json save path')
parser.add_argument('-t', '--type', type=str, default='train', help='only use in voc2012/2007')
opt = parser.parse_args()
if len(sys.argv) > 1:
print(opt)
parseXmlFilse(opt.voc_dir, opt.save_path, opt.type)
else:
# voc_data_dir = r'D:/dataset/VOC2012/VOCdevkit/VOC2012'
voc_data_dir = './data/labels/voc'
json_save_path = './data/convert/coco/train.json'
split = 'train'
parseXmlFilse(data_dir=voc_data_dir, json_save_path=json_save_path, split=split)
四、voc转yolo
代码如下(示例):
import os
import json
import argparse
import sys
import shutil
from lxml import etree
from tqdm import tqdm
category_set = set()
image_set = set()
bbox_nums = 0
def parse_xml_to_dict(xml):
"""
将xml文件解析成字典形式,参考tensorflow的recursive_parse_xml_to_dict
Args:
xml: xml tree obtained by parsing XML file contents using lxml.etree
Returns:
Python dictionary holding XML contents.
"""
if len(xml) == 0: # 遍历到底层,直接返回tag对应的信息
return {xml.tag: xml.text}
result = {}
for child in xml:
child_result = parse_xml_to_dict(child) # 递归遍历标签信息
if child.tag != 'object':
result[child.tag] = child_result[child.tag]
else:
if child.tag not in result: # 因为object可能有多个,所以需要放入列表里
result[child.tag] = []
result[child.tag].append(child_result[child.tag])
return {xml.tag: result}
def write_classIndices(category_set):
class_indices = dict((k, v) for v, k in enumerate(category_set))
json_str = json.dumps(dict((val, key) for key, val in class_indices.items()), indent=4)
with open('class_indices.json', 'w') as json_file:
json_file.write(json_str)
def xyxy2xywhn(bbox, size):
bbox = list(map(float, bbox))
size = list(map(float, size))
xc = (bbox[0] + (bbox[2] - bbox[0]) / 2.) / size[0]
yc = (bbox[1] + (bbox[3] - bbox[1]) / 2.) / size[1]
wn = (bbox[2] - bbox[0]) / size[0]
hn = (bbox[3] - bbox[1]) / size[1]
return (xc, yc, wn, hn)
def parser_info(info: dict, only_cat=True, class_indices=None):
filename = info['annotation']['filename']
image_set.add(filename)
objects = []
width = int(info['annotation']['size']['width'])
height = int(info['annotation']['size']['height'])
for obj in info['annotation']['object']:
obj_name = obj['name']
category_set.add(obj_name)
if only_cat:
continue
xmin = int(obj['bndbox']['xmin'])
ymin = int(obj['bndbox']['ymin'])
xmax = int(obj['bndbox']['xmax'])
ymax = int(obj['bndbox']['ymax'])
bbox = xyxy2xywhn((xmin, ymin, xmax, ymax), (width, height))
if class_indices is not None:
obj_category = class_indices[obj_name]
object = [obj_category, bbox]
objects.append(object)
return filename, objects
def parseXmlFilse(voc_dir, save_dir):
assert os.path.exists(voc_dir), "ERROR {} does not exists".format(voc_dir)
if os.path.exists(save_dir):
shutil.rmtree(save_dir)
os.makedirs(save_dir)
xml_files = [os.path.join(voc_dir, i) for i in os.listdir(voc_dir) if os.path.splitext(i)[-1] == '.xml']
for xml_file in xml_files:
with open(xml_file) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
info_dict = parse_xml_to_dict(xml)
parser_info(info_dict, only_cat=True)
with open(save_dir + "/classes.txt", 'w') as classes_file:
for cat in sorted(category_set):
classes_file.write("{}\n".format(cat))
class_indices = dict((v, k) for k, v in enumerate(sorted(category_set)))
xml_files = tqdm(xml_files)
for xml_file in xml_files:
with open(xml_file) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
info_dict = parse_xml_to_dict(xml)
filename, objects = parser_info(info_dict, only_cat=False, class_indices=class_indices)
if len(objects) != 0:
global bbox_nums
bbox_nums += len(objects)
with open(save_dir + "/" + filename.split(".")[0] + ".txt", 'w') as f:
for obj in objects:
f.write(
"{} {:.5f} {:.5f} {:.5f} {:.5f}\n".format(obj[0], obj[1][0], obj[1][1], obj[1][2], obj[1][3]))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--voc-dir', type=str, default='./data/labels/voc')
parser.add_argument('--save-dir', type=str, default='./data/convert/yolo')
opt = parser.parse_args()
if len(sys.argv) > 1:
print(opt)
parseXmlFilse(**vars(opt))
print("image nums: {}".format(len(image_set)))
print("category nums: {}".format(len(category_set)))
print("bbox nums: {}".format(bbox_nums))
else:
voc_dir = './data/labels/voc'
save_dir = './data/convert/yolo'
parseXmlFilse(voc_dir, save_dir)
print("image nums: {}".format(len(image_set)))
print("category nums: {}".format(len(category_set)))
print("bbox nums: {}".format(bbox_nums))
五、yolo转coco
代码如下(示例):
import argparse
import json
import os
import sys
import cv2
from datetime import datetime
# 初始化COCO格式的字典
coco = dict()
coco['images'] = []
coco['type'] = 'instances'
coco['annotations'] = []
coco['categories'] = []
# 初始化图片和标注的ID
image_id = 000000
annotation_id = 0
def addCatItem(category_dict):
for k, v in category_dict.items():
category_item = {'supercategory': 'none', 'id': int(k), 'name': v}
coco['categories'].append(category_item)
def addImgItem(file_name, size):
global image_id
image_id += 1
image_item = {
'id': image_id,
'file_name': file_name,
'width': size[1],
'height': size[0],
'license': None,
'flickr_url': None,
'coco_url': None,
'date_captured': str(datetime.today())
}
coco['images'].append(image_item)
return image_id
def addAnnoItem(image_id, category_id, bbox):
global annotation_id
annotation_id += 1
seg = [bbox[0], bbox[1], bbox[0], bbox[1] + bbox[3], bbox[0] + bbox[2], bbox[1] + bbox[3], bbox[0] + bbox[2], bbox[1]]
annotation_item = {
'segmentation': [seg],
'area': bbox[2] * bbox[3],
'iscrowd': 0,
'ignore': 0,
'image_id': image_id,
'bbox': bbox,
'category_id': category_id,
'id': annotation_id
}
coco['annotations'].append(annotation_item)
def xywhn2xywh(bbox, size):
bbox = list(map(float, bbox))
size = list(map(float, size))
xmin = (bbox[0] - bbox[2] / 2.) * size[1]
ymin = (bbox[1] - bbox[3] / 2.) * size[0]
w = bbox[2] * size[1]
h = bbox[3] * size[0]
return [int(xmin), int(ymin), int(w), int(h)]
def parseXmlFiles(image_path, anno_path, save_path, json_name='train.json'):
assert os.path.exists(image_path), f"ERROR {image_path} does not exist."
assert os.path.exists(anno_path), f"ERROR {anno_path} does not exist."
if not os.path.exists(save_path):
os.makedirs(save_path)
json_path = os.path.join(save_path, json_name)
# 读取类别信息
with open(os.path.join(anno_path, 'classes.txt'), 'r') as f:
categories = [line.strip() for line in f.readlines()]
category_id = dict((k, v) for k, v in enumerate(categories))
addCatItem(category_id)
images = [os.path.join(image_path, i) for i in os.listdir(image_path) if i.lower().endswith(('.jpg', '.png'))]
files = [os.path.join(anno_path, i) for i in os.listdir(anno_path) if i.endswith('.txt') and not i.startswith('classes')]
for file in files:
filename = os.path.basename(file)[:-4]
img_file = next((img for img in images if img.endswith(filename + '.jpg') or img.endswith(filename + '.png')), None)
if img_file:
img = cv2.imread(img_file)
shape = img.shape
current_image_id = addImgItem(os.path.basename(img_file), shape)
with open(file, 'r') as fid:
for line in fid.readlines():
parts = line.strip().split()
category = int(parts[0])
bbox = xywhn2xywh(parts[1:5], shape)
addAnnoItem(current_image_id, category, bbox)
# 保存JSON
with open(json_path, 'w') as f:
json.dump(coco, f)
print(f"Converted: {len(coco['categories'])} categories, {len(coco['images'])} images, {len(coco['annotations'])} annotations")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="YOLO to COCO format converter.")
parser.add_argument('--anno-path', type=str, required=True, help='Directory path to YOLO annotations')
parser.add_argument('--image-path', type=str, required=True, help='Directory path to images')
parser.add_argument('--save-path', type=str, required=True, help='Directory path to save COCO formatted annotation')
parser.add_argument('--json-name', type=str, default='train.json', help='Output JSON file name')
args = parser.parse_args()
parseXmlFiles(args.image_path, args.anno_path, args.save_path, args.json_name)
# 务必确保classes.txt文件存在于标注文件夹中,并且正确列出所有类别。
六、yolo转voc
代码如下(示例):
import argparse
import os
import sys
import shutil
import cv2
from lxml import etree, objectify
# 将标签信息写入xml
from tqdm import tqdm
images_nums = 0
category_nums = 0
bbox_nums = 0
def save_anno_to_xml(filename, size, objs, save_path):
E = objectify.ElementMaker(annotate=False)
anno_tree = E.annotation(
E.folder("DATA"),
E.filename(filename),
E.source(
E.database("The VOC Database"),
E.annotation("PASCAL VOC"),
E.image("flickr")
),
E.size(
E.width(size[1]),
E.height(size[0]),
E.depth(size[2])
),
E.segmented(0)
)
for obj in objs:
E2 = objectify.ElementMaker(annotate=False)
anno_tree2 = E2.object(
E.name(obj[0]),
E.pose("Unspecified"),
E.truncated(0),
E.difficult(0),
E.bndbox(
E.xmin(obj[1][0]),
E.ymin(obj[1][1]),
E.xmax(obj[1][2]),
E.ymax(obj[1][3])
)
)
anno_tree.append(anno_tree2)
anno_path = os.path.join(save_path, filename[:-3] + "xml")
etree.ElementTree(anno_tree).write(anno_path, pretty_print=True)
def xywhn2xyxy(bbox, size):
bbox = list(map(float, bbox))
size = list(map(float, size))
xmin = (bbox[0] - bbox[2] / 2.) * size[1]
ymin = (bbox[1] - bbox[3] / 2.) * size[0]
xmax = (bbox[0] + bbox[2] / 2.) * size[1]
ymax = (bbox[1] + bbox[3] / 2.) * size[0]
box = [xmin, ymin, xmax, ymax]
return list(map(int, box))
def parseXmlFilse(image_path, anno_path, save_path):
global images_nums, category_nums, bbox_nums
assert os.path.exists(image_path), "ERROR {} dose not exists".format(image_path)
assert os.path.exists(anno_path), "ERROR {} dose not exists".format(anno_path)
if os.path.exists(save_path):
shutil.rmtree(save_path)
os.makedirs(save_path)
category_set = []
with open(anno_path + '/classes.txt', 'r') as f:
for i in f.readlines():
category_set.append(i.strip())
category_nums = len(category_set)
category_id = dict((k, v) for k, v in enumerate(category_set))
images = [os.path.join(image_path, i) for i in os.listdir(image_path)]
files = [os.path.join(anno_path, i) for i in os.listdir(anno_path)]
images_index = dict((v.split(os.sep)[-1][:-4], k) for k, v in enumerate(images))
images_nums = len(images)
for file in tqdm(files):
if os.path.splitext(file)[-1] != '.txt' or 'classes' in file.split(os.sep)[-1]:
continue
if file.split(os.sep)[-1][:-4] in images_index:
index = images_index[file.split(os.sep)[-1][:-4]]
img = cv2.imread(images[index])
shape = img.shape
filename = images[index].split(os.sep)[-1]
else:
continue
objects = []
with open(file, 'r') as fid:
for i in fid.readlines():
i = i.strip().split()
category = int(i[0])
category_name = category_id[category]
bbox = xywhn2xyxy((i[1], i[2], i[3], i[4]), shape)
obj = [category_name, bbox]
objects.append(obj)
bbox_nums += len(objects)
save_anno_to_xml(filename, shape, objects, save_path)
if __name__ == '__main__':
"""
脚本说明:
本脚本用于将yolo格式的标注文件.txt转换为voc格式的标注文件.xml
参数说明:
anno_path:标注文件txt存储路径
save_path:json文件输出的文件夹
image_path:图片路径
"""
parser = argparse.ArgumentParser()
parser.add_argument('-ap', '--anno-path', type=str, default='./data/labels/yolo', help='yolo txt path')
parser.add_argument('-s', '--save-path', type=str, default='./data/convert/voc', help='xml save path')
parser.add_argument('--image-path', default='./data/images')
opt = parser.parse_args()
if len(sys.argv) > 1:
print(opt)
parseXmlFilse(**vars(opt))
print("image nums: {}".format(images_nums))
print("category nums: {}".format(category_nums))
print("bbox nums: {}".format(bbox_nums))
else:
anno_path = './data/labels/yolo'
save_path = './data/convert/voc1'
image_path = './data/images'
parseXmlFilse(image_path, anno_path, save_path)
print("image nums: {}".format(images_nums))
print("category nums: {}".format(category_nums))
print("bbox nums: {}".format(bbox_nums))
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。