系列文章目录
【船舶数据集格式转换】AIR-SARShip-1.0数据集VOC转COCO 【船舶数据集格式转换】AIR-SARShip-2.0数据集VOC转COCO 【船舶数据集格式转换】HRSID数据集VOC转COCO
`
文章目录
- 系列文章目录
- 前言
- 一、sar船舶图像检测数据集
- 二、使用步骤
-
- 1.原始数据集
- 2.xml2json_SSDD
- 3.json文件
- 总结
前言
最近一直在做sar船舶图像检测涉及一些工作sar对比算法中常用的舰船数据集VOC格式数据集需要转换为COCO格式,当然,修改比较算法。我希望能记录下来,为以后的人提供一些微不足道的帮助。本文介绍了四种常用船舶数据集的转换,包括数据集。不需要修改就可以直接使用!之前已经介绍了其他三个数据集的转换,本文介绍了最后一个数据集。
一、sar船舶图像检测数据集
先简单介绍一下数据集:
SSDD基于国内外的第一个数据集SAR图像船舶目标检测公共数据集包括不同图像分辨率、船舶尺寸、海况、传感器类型等各种情况下的船舶图像,可作为研究人员评估其算法的基准。SSDD数据集中的每一艘船,都标注有带置信分数的边界框。
二、使用步骤
1.原始数据集
原始数据为.xml
格式如下图所示:
每个文本都是一副图像标记,如下图所示:
目的是把它变成.json
格式,所有图像的标记都放在同一个文件中,因为每个数据集的标记格式不同,我们想把它们转换成统一的coco格式的json文件。
2.xml2json_SSDD
代码如下:
# 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[:] + '.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'E:\pycharm_workspace\detection\SSDD数据以及
标签\SSDD数据以及标签\Annotations' # test_txt = 'voc2007/test.txt' test_txt = r'E:\pycharm_workspace\detection\SSDD数据以及标签\SSDD数据以及标签\val.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/SSDD_annotations/instances_val2017.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 更加美观显示
3.json文件
最终就可以得到如下图所示的json文件:
其中json文件的内容为coco数据集格式的标注,所有标注都在同一个文件夹下:
{
"images": [
{
"file_name": "000001.jpg",
"width": "416",
"height": "323",
"id": 0
},
{
"file_name": "000002.jpg",
"width": "501",
"height": "355",
"id": 1
},
{
"file_name": "000003.jpg",
"width": "500",
"height": "403",
"id": 2
},
"categories": [
{
"supercategory": "Ship",
"name": "Ship",
"id": 1
}
],
"annotations": [
{
"image_id": 0,
"category_id": 1,
"bbox": [
208,
50,
65,
101
],
"id": 0,
"area": 6555.0,
"iscrowd": 0
},
{
"image_id": 1,
"category_id": 1,
"bbox": [
212,
150,
52,
19
],
"id": 1,
"area": 978.0,
"iscrowd": 0
},
{
"image_id": 2,
"category_id": 1,
"bbox": [
79,
175,
44,
87
],
"id": 2,
"area": 3818.0,
"iscrowd": 0
},
这里展示了SSDD数据集coco格式的部分转换结果。
总结
以上就是今天要讲的内容,本文仅仅简单介绍了SSDD数据集的转化,至此全部数据集的转换已经全部给出。感谢聆听!