Labelme标注的数据集转VOC2007格式的数据集。

举报
AI浩 发表于 2021/12/23 01:41:29 2021/12/23
【摘要】 VOC2007数据文件夹说明 1)JPEGImages文件夹 文件夹里包含了训练图片和测试图片,混放在一起 2)Annatations文件夹 文件夹存放的是xml格式的标签文件,每个xml文件都对应于JPEGImages文件夹的一张图片 3)ImageSets文件夹 Main存放的是图像物体识别的数据,Main里面有te...

VOC2007数据文件夹说明

1)JPEGImages文件夹

文件夹里包含了训练图片和测试图片,混放在一起

2)Annatations文件夹

文件夹存放的是xml格式的标签文件,每个xml文件都对应于JPEGImages文件夹的一张图片

3)ImageSets文件夹

Main存放的是图像物体识别的数据,Main里面有test.txt, train.txt, val.txt,trainval.txt.这四个文件我们后面会生成

XML说明

<?xml version="1.0" encoding="utf-8"?>

<annotation>

         <source>

                   <image>optic rs image</image>

                   <annotation>Lmars RSDS2016</annotation>

                   <flickrid>0</flickrid>

                   <database>Lmars Detection Dataset of RS</database>

         </source>

         <object>

<!--bounding box的四个坐标,分别为左上角和右下角的x,y坐标-->

                   <bndbox>

                            <xmin>690</xmin>

                            <ymin>618</ymin>

                            <ymax>678</ymax>

                            <xmax>748</xmax>

                   </bndbox>

<!--是否容易被识别,0表示容易,1表示困难-->

                   <difficult>0</difficult>

                   <pose>Left</pose>

<!--物体类别-->

                   <name>aircraft</name>

<!--是否被裁剪,0表示完整,1表示不完整-->

                   <truncated>1</truncated>

         </object>

         <filename>aircraft_773.jpg</filename>

         <!--是否用于分割,0表示用于,1表示不用于-->

         <segmented>0</segmented>

<!--图片所有者-->

         <owner>

                   <name>Lmars, Wuhan University</name>

                   <flickrid>I do not know</flickrid>

         </owner>

         <folder>RSDS2016</folder>

         <size>

                   <width>1044</width>

                   <depth>3</depth>

                   <height>915</height>

         </size>

</annotation>

完整代码:


  
  1. import os
  2. from typing import List, Any
  3. import numpy as np
  4. import codecs
  5. import json
  6. from glob import glob
  7. import cv2
  8. import shutil
  9. from sklearn.model_selection import train_test_split
  10. # 1.标签路径
  11. labelme_path = "LabelmeData/"  # 原始labelme标注数据路径
  12. saved_path = "VOC2007/"  # 保存路径
  13. isUseTest=True#是否创建test集
  14. # 2.创建要求文件夹
  15. if not os.path.exists(saved_path + "Annotations"):
  16.     os.makedirs(saved_path + "Annotations")
  17. if not os.path.exists(saved_path + "JPEGImages/"):
  18.     os.makedirs(saved_path + "JPEGImages/")
  19. if not os.path.exists(saved_path + "ImageSets/Main/"):
  20.     os.makedirs(saved_path + "ImageSets/Main/")
  21. # 3.获取待处理文件
  22. files = glob(labelme_path + "*.json")
  23. files = [i.replace("\\","/").split("/")[-1].split(".json")[0] for i in files]
  24. print(files)
  25. # 4.读取标注信息并写入 xml
  26. for json_file_ in files:
  27.     json_filename = labelme_path + json_file_ + ".json"
  28.     json_file = json.load(open(json_filename, "r", encoding="utf-8"))
  29.     height, width, channels = cv2.imread(labelme_path + json_file_ + ".jpg").shape
  30.     with codecs.open(saved_path + "Annotations/" + json_file_ + ".xml", "w", "utf-8") as xml:
  31.         xml.write('<annotation>\n')
  32.         xml.write('\t<folder>' + 'WH_data' + '</folder>\n')
  33.         xml.write('\t<filename>' + json_file_ + ".jpg" + '</filename>\n')
  34.         xml.write('\t<source>\n')
  35.         xml.write('\t\t<database>WH Data</database>\n')
  36.         xml.write('\t\t<annotation>WH</annotation>\n')
  37.         xml.write('\t\t<image>flickr</image>\n')
  38.         xml.write('\t\t<flickrid>NULL</flickrid>\n')
  39.         xml.write('\t</source>\n')
  40.         xml.write('\t<owner>\n')
  41.         xml.write('\t\t<flickrid>NULL</flickrid>\n')
  42.         xml.write('\t\t<name>WH</name>\n')
  43.         xml.write('\t</owner>\n')
  44.         xml.write('\t<size>\n')
  45.         xml.write('\t\t<width>' + str(width) + '</width>\n')
  46.         xml.write('\t\t<height>' + str(height) + '</height>\n')
  47.         xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
  48.         xml.write('\t</size>\n')
  49.         xml.write('\t\t<segmented>0</segmented>\n')
  50.         for multi in json_file["shapes"]:
  51.             points = np.array(multi["points"])
  52.             labelName=multi["label"]
  53.             xmin = min(points[:, 0])
  54.             xmax = max(points[:, 0])
  55.             ymin = min(points[:, 1])
  56.             ymax = max(points[:, 1])
  57.             label = multi["label"]
  58.             if xmax <= xmin:
  59.                 pass
  60.             elif ymax <= ymin:
  61.                 pass
  62.             else:
  63.                 xml.write('\t<object>\n')
  64.                 xml.write('\t\t<name>' + labelName+ '</name>\n')
  65.                 xml.write('\t\t<pose>Unspecified</pose>\n')
  66.                 xml.write('\t\t<truncated>1</truncated>\n')
  67.                 xml.write('\t\t<difficult>0</difficult>\n')
  68.                 xml.write('\t\t<bndbox>\n')
  69.                 xml.write('\t\t\t<xmin>' + str(int(xmin)) + '</xmin>\n')
  70.                 xml.write('\t\t\t<ymin>' + str(int(ymin)) + '</ymin>\n')
  71.                 xml.write('\t\t\t<xmax>' + str(int(xmax)) + '</xmax>\n')
  72.                 xml.write('\t\t\t<ymax>' + str(int(ymax)) + '</ymax>\n')
  73.                 xml.write('\t\t</bndbox>\n')
  74.                 xml.write('\t</object>\n')
  75.                 print(json_filename, xmin, ymin, xmax, ymax, label)
  76.         xml.write('</annotation>')
  77. # 5.复制图片到 VOC2007/JPEGImages/下
  78. image_files = glob(labelme_path + "*.jpg")
  79. print("copy image files to VOC007/JPEGImages/")
  80. for image in image_files:
  81.     shutil.copy(image, saved_path + "JPEGImages/")
  82. # 6.split files for txt
  83. txtsavepath = saved_path + "ImageSets/Main/"
  84. ftrainval = open(txtsavepath + '/trainval.txt', 'w')
  85. ftest = open(txtsavepath + '/test.txt', 'w')
  86. ftrain = open(txtsavepath + '/train.txt', 'w')
  87. fval = open(txtsavepath + '/val.txt', 'w')
  88. total_files = glob("./VOC2007/Annotations/*.xml")
  89. total_files = [i.replace("\\","/").split("/")[-1].split(".xml")[0] for i in total_files]
  90. trainval_files=[]
  91. test_files=[]
  92. if isUseTest:
  93.     trainval_files, test_files = train_test_split(total_files, test_size=0.15, random_state=55)
  94. else:
  95.     trainval_files=total_files
  96. for file in trainval_files:
  97.     ftrainval.write(file + "\n")
  98. # split
  99. train_files, val_files = train_test_split(trainval_files, test_size=0.15, random_state=55)
  100. # train
  101. for file in train_files:
  102.     ftrain.write(file + "\n")
  103. # val
  104. for file in val_files:
  105.     fval.write(file + "\n")
  106. for file in test_files:
  107.     print(file)
  108.     ftest.write(file + "\n")
  109. ftrainval.close()
  110. ftrain.close()
  111. fval.close()
  112. ftest.close()

注:训练集和验证集的划分方法是采用 sklearn.model_selection.train_test_split 进行分割的。

文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。

原文链接:wanghao.blog.csdn.net/article/details/105766915

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。