指点成金-最美分享吧

登录

使用Python将DOTA数据集的格式转换成VOC2007数据集的格式

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了使用Python将DOTA数据集的格式转换成VOC2007数据集的格式相关的知识,希望对你有一定的参考价值。

  • 一、VOC2007数据集
  • 二、DOTA数据集
  • 三、将DOTA数据集的格式转换成VOC2007数据集的格式

一、VOC2007数据集

  VOC2007数据集的文件结构如下图所示。


  其中,文件夹Annotations中存放的是图像的标注信息的xml文件,命名从000001.xml开始;文件夹ImageSets中存放的是图像划分的集合的txt文件,目标检测任务对应的train、val、trainval、test数据集的txt文件存放在Main文件夹中;文件夹JPEGImages中存放的是所有图片的jpg文件,命名从000001.jpg开始;文件夹SegmentationClassSegmentationObject中存放的是其他任务的数据信息。
  文件夹Annotations中存放的某一张图像的标注信息的xml文件里面的内容如下所示。

<annotation><folder>VOC2007folder><filename>000007.jpgfilename><source>    <database>The VOC2007 Databasedatabase><annotation>PASCAL VOC2007annotation> <image>flickrimage><flickrid>194179466flickrid>source><owner><flickrid>monsieurrompuflickrid><name>Thom Zemanekname>owner><size><width>500width><height>333height><depth>3depth>size><segmented>0segmented><object>    <name>carname><pose>Unspecifiedpose><truncated>1truncated><difficult>0difficult><bndbox><xmin>141xmin><ymin>50ymin><xmax>500xmax><ymax>330ymax>bndbox>object>annotation>

  关于VOC2007数据集的其他详细信息可见→VOC2007数据集详细分析。

二、DOTA数据集

  DOTA数据集的官方链接→DOTA数据集链接。

  DOTA数据集(全称A Large-scale Dataset for Object DeTection in Aerial Images)是用于航拍图像中的目标检测的大型图像数据集, 它可用于发现和评估航拍图像中的物体。 对于DOTA数据集,它包含来自不同传感器和平台的2806个航拍图像。每个图像的大小在大约800×800到4000×4000像素的范围内,并且包含各种比例,方向和形状的对象。这些DOTA图像由航空影像解释专家分类为15个常见对象类别。完全注释的DOTA图像包含188、282个实例,每个实例都由任意(8自由度)四边形标记。

  目前DOTA数据集有三个版本:

  • DOTA-v1.0:包含15个常见类别,2,806个图像和188,282个实例。DOTA-V1.0中的训练集,验证集和测试集的比例分别为1/2,1 / 6和1/3。
          plane, ship, storage tank, baseball diamond, tennis court, basketball court, ground track field, harbor, bridge, large vehicle, small vehicle, helicopter, roundabout, soccer ball field and swimming pool.
  • DOTA-v1.5:使用了与DOTA-v1.0相同的图像,但是非常小的实例(小于10像素)也有注释。此外,还增加了一个新的类别"container crane"。它总共包含403,318个实例。图像和数据集分割的数量与DOTA-v1.0相同。该版本是为了与IEEE CVPR 2019联合举办的2019 DOAI航拍图像目标检测挑战赛发布的。
          plane, ship, storage tank, baseball diamond, tennis court, basketball court, ground track field, harbor, bridge, large vehicle, small vehicle, helicopter, roundabout, soccer ball field, swimming pool and container crane.
  • DOTA-v2.0:收集更多Google地球,GF-2卫星和空中图像。DOTA-v2.0中有18个常见类别,11,268个图像和1,793,658个实例。与DOTA-V1.5相比,它进一步增加了新类别的"airport"和"helipad"。DOTA的11,268个图像分为训练、验证、测试验证和测试挑战集。
          plane, ship, storage tank, baseball diamond, tennis court, basketball court, ground track field, harbor, bridge, large vehicle, small vehicle, helicopter, roundabout, soccer ball field, swimming pool, container crane, airport and helipad.

  DOTA数据集的文件结构如下图所示。


  数据集DOTA文件夹下有trainvaltest三个文件夹。文件夹trainval下各有imageslabelTxt-v1.0labelTxt-v1.5三个文件夹,文件夹test下只有images一个文件夹。
  其中images文件夹中存放的是遥感图像,如下图所示。


  labelTxt-v1.0文件夹中存放的是DOTA v1.0版本的标签信息,如下图所示,有labelTxttrainset_reclabelTxt两个文件夹。labelTxt文件夹中存放的是obb(定向边界框)标签信息,trainset_reclabelTxt文件夹中存放的是hbb(水平边界框)标签信息。


  labelTxt-v1.5文件夹中存放的是DOTA v1.5版本的标签信息,与labelTxt-v1.0文件夹类似,如下图所示,该文件夹下有存放obb(定向边界框)标签信息的文件夹DOTA-v1.5_train和存放hbb(水平边界框)标签信息的文件夹DOTA-v1.5_train_hbb

三、将DOTA数据集的格式转换成VOC2007数据集的格式

  使用Python将DOTA数据集的格式转换成VOC2007数据集的格式需要进行以下操作。

  1. 首先可以对DOTA数据集中图片的ground truth进行可视化,可视化的代码visual_DOTA.py及结果如下所示。
import cv2 import osimport numpy as np thr=0.95def custombasename(fullname):      return os.path.basename(os.path.splitext(fullname)[0])    def GetFileFromThisRootDir(dir,ext = None):    allfiles = []    needExtFilter = (ext != None)    for root,dirs,files in os.walk(dir):      for filespath in files:        filepath = os.path.join(root, filespath)        extension = os.path.splitext(filepath)[1][1:]        if needExtFilter and extension in ext:          allfiles.append(filepath)        elif not needExtFilter:          allfiles.append(filepath)    return allfiles   def visualise_gt(label_path, pic_path, newpic_path):    results =  GetFileFromThisRootDir(label_path)    for result in results:        f = open(result,"r")        lines = f.readlines()        if len(lines)==0:  #如果为空            print("文件为空",result)            continue        boxes = []        for i,line in enumerate(lines):            #score = float(line.strip().split(" ")[8])            #if i in [0,1]:   #如果可视化DOTA-v1.5,前两行不需要,跳过,取消注释;如果可视化DOTA-v1.0,前两行需要,注释掉这两行代码            #    continue            name = result.split("/")[-1]            box=line.strip().split(" ")[0:8]            box = np.array(box,dtype = np.float64)            #if float(score)>thr:            boxes.append(box)        boxes = np.array(boxes,np.float64)        f.close()           filepath=os.path.join(pic_path, name.split(".")[0]+".jpg")        im=cv2.imread(filepath)        #print line3        for i in range(boxes.shape[0]):            box =np.array( [[boxes[i][0],boxes[i][1]],[boxes[i][2],boxes[i][3]], \                            [boxes[i][4],boxes[i][5]],[boxes[i][6],boxes[i][7]]],np.int32)            box = box.reshape((-1,1,2))            cv2.polylines(im,[box],True,(0,0,255),2)        cv2.imwrite(os.path.join(newpic_path,result.split("/")[-1].split(".")[0]+".jpg"),im)        #下面是有score的                #        x,y,w,h,score=box.split("_")#        #        score=float(score)        #        cv2.rectangle(im,(int(x),int(y)),(int(x)+int(w),int(y)+int(h)),(0,0,255),1)        #        cv2.putText(im,"%3f"%score, (int(x)+int(w),int(y)+int(h)+5),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),1)        #        cv2.imwrite(newpic_path+filename,im) if __name__ == "__main__":    pic_path = "E:/Remote Sensing/Data Set/DOTA/train/images/" #样本图片路径    label_path = "E:/Remote Sensing/Data Set/DOTA/train/labelTxt-v1.0/trainset_reclabelTxt/"#DOTA标签的所在路径        newpic_path= "E:/Remote Sensing/Data Set/DOTA/hbbshow/train/"  #可视化保存路径    if not os.path.isdir(newpic_path):        os.makedirs(newpic_path)    visualise_gt(label_path, pic_path, newpic_path)

  1. 新建一个与VOC2007数据集的文件结构类似的DOTA数据集文件结构,只保留我们需要的部分。

  2. 由于DOTA数据集中有的图片纵横比太大,不能直接用于后续的训练,所以需要对DOTA数据集进行切割。将数据集中的图片切割为600 × \times × 600固定大小的图片,并对切割后的图片生成相对应的标注信息xml文件。切割的代码DOTA_VOC.py及结果如下所示。

import osimport imageiofrom xml.dom.minidom import Documentimport numpy as npimport copy, cv2def save_to_xml(save_path, im_width, im_height, objects_axis, label_name, name, hbb=True):    im_depth = 0    object_num = len(objects_axis)    doc = Document()    annotation = doc.createElement("annotation")    doc.appendChild(annotation)    folder = doc.createElement("folder")    folder_name = doc.createTextNode("VOC2007")    folder.appendChild(folder_name)    annotation.appendChild(folder)    filename = doc.createElement("filename")    filename_name = doc.createTextNode(name)    filename.appendChild(filename_name)    annotation.appendChild(filename)    source = doc.createElement("source")    annotation.appendChild(source)    database = doc.createElement("database")    database.appendChild(doc.createTextNode("The VOC2007 Database"))    source.appendChild(database)    annotation_s = doc.createElement("annotation")    annotation_s.appendChild(doc.createTextNode("PASCAL VOC2007"))    source.appendChild(annotation_s)    image = doc.createElement("image")    image.appendChild(doc.createTextNode("flickr"))    source.appendChild(image)    flickrid = doc.createElement("flickrid")    flickrid.appendChild(doc.createTextNode("322409915"))    source.appendChild(flickrid)    owner = doc.createElement("owner")    annotation.appendChild(owner)    flickrid_o = doc.createElement("flickrid")    flickrid_o.appendChild(doc.createTextNode("knautia"))    owner.appendChild(flickrid_o)    name_o = doc.createElement("name")    name_o.appendChild(doc.createTextNode("yang"))    owner.appendChild(name_o)    size = doc.createElement("size")    annotation.appendChild(size)    width = doc.createElement("width")    width.appendChild(doc.createTextNode(str(im_width)))    height = doc.createElement("height")    height.appendChild(doc.createTextNode(str(im_height)))    depth = doc.createElement("depth")    depth.appendChild(doc.createTextNode(str(im_depth)))    size.appendChild(width)    size.appendChild(height)    size.appendChild(depth)    segmented = doc.createElement("segmented")    segmented.appendChild(doc.createTextNode("0"))    annotation.appendChild(segmented)    for i in range(object_num):        objects = doc.createElement("object")        annotation.appendChild(objects)        object_name = doc.createElement("name")        object_name.appendChild(doc.createTextNode(label_name[int(objects_axis[i][-1])]))        objects.appendChild(object_name)        pose = doc.createElement("pose")        pose.appendChild(doc.createTextNode("Unspecified"))        objects.appendChild(pose)        truncated = doc.createElement("truncated")        truncated.appendChild(doc.createTextNode("1"))        objects.appendChild(truncated)        difficult = doc.createElement("difficult")        difficult.appendChild(doc.createTextNode("0"))        objects.appendChild(difficult)        bndbox = doc.createElement("bndbox")        objects.appendChild(bndbox)        if hbb:           x0 = doc.createElement("xmin")           x0.appendChild(doc.createTextNode(str((objects_axis[i][0]))))           bndbox.appendChild(x0)           y0 = doc.createElement("ymin")           y0.appendChild(doc.createTextNode(str((objects_axis[i][1]))))           bndbox.appendChild(y0)           x1 = doc.createElement("xmax")           x1.appendChild(doc.createTextNode(str((objects_axis[i][2]))))           bndbox.appendChild(x1)           y1 = doc.createElement("ymax")           y1.appendChild(doc.createTextNode(str((objects_axis[i][5]))))           bndbox.appendChild(y1)               else:            x0 = doc.createElement("x0")            x0.appendChild(doc.createTextNode(str((objects_axis[i][0]))))            bndbox.appendChild(x0)            y0 = doc.createElement("y0")            y0.appendChild(doc.createTextNode(str((objects_axis[i][1]))))            bndbox.appendChild(y0)            x1 = doc.createElement("x1")            x1.appendChild(doc.createTextNode(str((objects_axis[i][2]))))            bndbox.appendChild(x1)            y1 = doc.createElement("y1")            y1.appendChild(doc.createTextNode(str((objects_axis[i][3]))))            bndbox.appendChild(y1)                        x2 = doc.createElement("x2")            x2.appendChild(doc.createTextNode(str((objects_axis[i][4]))))            bndbox.appendChild(x2)            y2 = doc.createElement("y2")            y2.appendChild(doc.createTextNode(str((objects_axis[i][5]))))            bndbox.appendChild(y2)            x3 = doc.createElement("x3")            x3.appendChild(doc.createTextNode(str((objects_axis[i][6]))))            bndbox.appendChild(x3)            y3 = doc.createElement("y3")            y3.appendChild(doc.createTextNode(str((objects_axis[i][7]))))            bndbox.appendChild(y3)            f = open(save_path,"w")    f.write(doc.toprettyxml(indent = ""))    f.close() class_list = ["plane", "baseball-diamond", "bridge", "ground-track-field",               "small-vehicle", "large-vehicle", "ship",               "tennis-

以上是关于使用Python将DOTA数据集的格式转换成VOC2007数据集的格式的主要内容,如果未能解决你的问题,请参考以下文章