【舰船数据集格式转换】AIR-SARShip-1.0数据集VOC转COCO
【舰船数据集格式转换】AIR-SARShip-2.0数据集VOC转COCO
sar舰船图像检测的工作,其中涉及到了一些sar舰船常用的数据集,但大多都是VOC格式的数据集,在做对比实验时候,如果用到COCO格式,则需要进行转换。我希望记录下来,给后来遇到相同问题的人提供一些微不足道的帮助。系列介绍了4种常用舰船数据集的转换,在AIR-SARShip 1.0、AIR-SARShip 2.0 、HRSID、SSDD数据集。可直接使用,无需修改!
HRSID数据集是电子科技大学在2020年1月发布数据集,HRSID是高分辨率sar图像中用于船舶检测、语义分割和实例分割任务的数据集。该数据集共包含5604张高分辨率SAR图像和16951个ship实例。ISSID数据集借鉴了Microsoft Common Objects in Context (COCO)数据集的构建过程,包括不同分辨率的SAR图像、极化、海况、海域和沿海港口。该数据集是研究人员评估其方法的基准。对于HRSID, SAR图像的分辨率分别为:0.5m, 1 m, 3 m。
其中每一个文本都是一副图像的标注,如下图所示:
目的是将其变为.json
格式,将所有图像的标注放在同一个文件里,因为每种数据集的标注格式不同,我们想把它们转换为统一的coco格式的json文件。
代码如下:
# coding=utf-8
import xml.etree.ElementTree as ET
import os
import json
voc_clses = ['ship']
categories = []
for iind, cat in enumerate(voc_clses):
cate = {}
cate['supercategory'] = cat
cate['name'] = cat
cate['id'] = iind
categories.append(cate)
def getimages(xmlname, id):
sig_xml_box = []
tree = ET.parse(xmlname)
root = tree.getroot()
images = {}
for i in root: # 遍历一级节点
if i.tag == 'filename':
file_name =i.text[:-4] + '.jpg' # 0001.jpg
# print('image name: ', file_name)
images['file_name'] = file_name
if i.tag == 'size':
for j in i:
if j.tag == 'width':
width = j.text
images['width'] = width
if j.tag == 'height':
height = j.text
images['height'] = height
if i.tag == 'object':
for j in i:
if j.tag == 'name':
cls_name = j.text
cat_id = voc_clses.index(cls_name) + 1
if j.tag == 'bndbox':
bbox = []
xmin = 0
ymin = 0
xmax = 0
ymax = 0
for r in j:
if r.tag == 'xmin':
xmin = eval(r.text)
if r.tag == 'ymin':
ymin = eval(r.text)
if r.tag == 'xmax':
xmax = eval(r.text)
if r.tag == 'ymax':
ymax = eval(r.text)
bbox.append(xmin)
bbox.append(ymin)
bbox.append(xmax - xmin)
bbox.append(ymax - ymin)
bbox.append(id) # 保存当前box对应的image_id
bbox.append(cat_id)
# anno area
bbox.append((xmax - xmin) * (ymax - ymin) - 10.0) # bbox的ares
# coco中的ares数值是 < w*h 的, 因为它其实是按segmentation的面积算的,所以我-10.0一下...
sig_xml_box.append(bbox)
# print('bbox', xmin, ymin, xmax - xmin, ymax - ymin, 'id', id, 'cls_id', cat_id)
images['id'] = id
# print ('sig_img_box', sig_xml_box)
return images, sig_xml_box
def txt2list(txtfile):
f = open(txtfile)
l = []
for line in f:
l.append(line[:-1])
f.close()
print(l)
return l
# voc2007xmls = 'anns'
voc2007xmls = r'D:\Dataset\HRSID_JPG\HRSID_JPG\xml\train'
# test_txt = 'voc2007/test.txt'
test_txt = r'D:\Dataset\HRSID_JPG\HRSID_JPG\train_small.txt'
xml_names = txt2list(test_txt)
xmls = []
bboxes = []
ann_js = {}
for ind, xml_name in enumerate(xml_names):
xmls.append(os.path.join(voc2007xmls, xml_name + '.xml'))
json_name = r'E:\pycharm_workspace\voc2coco-pattern\data/HRSID_annotations_small/instances_train2017.json' # 要生成的文件名
images = []
for i_index, xml_file in enumerate(xmls):
image, sig_xml_bbox = getimages(xml_file, i_index)
images.append(image)
bboxes.extend(sig_xml_bbox)
ann_js['images'] = images
ann_js['categories'] = categories
annotations = []
for box_ind, box in enumerate(bboxes):
anno = {}
anno['image_id'] = box[-3]
anno['category_id'] = box[-2]
anno['bbox'] = box[:-3]
anno['id'] = box_ind
anno['area'] = box[-1]
anno['iscrowd'] = 0
annotations.append(anno)
ann_js['annotations'] = annotations
json.dump(ann_js, open(json_name, 'w'), indent=4) # indent=4 更加美观显示
其中json文件的内容为coco数据集格式的标注,所有标注都在同一个文件夹下:
{
"images": [
{
"file_name": "P0001_0_800_10190_10990.jpg",
"width": "800",
"height": "800",
"id": 0
},
{
"file_name": "P0001_0_800_7200_8000.jpg",
"width": "800",
"height": "800",
"id": 1
},
"categories": [
{
"supercategory": "Ship",
"name": "Ship",
"id": 0
}
],
"annotations": [
{
"image_id": 1,
"category_id": 1,
"bbox": [
603,
556,
8,
10
],
"id": 0,
"area": 70.0,
"iscrowd": 0
},
{
"image_id": 1,
"category_id": 1,
"bbox": [
465,
64,
12,
34
],
"id": 1,
"area": 398.0,
"iscrowd": 0
},
这里只展示了部分转换结果。
以上就是今天要讲的内容,本文仅仅简单介绍了HRSID数据集的转化,最后一个SSDD数据集的转换代码我会在之后的文章中逐一讲述。感谢聆听!