ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

从VOC数据集选择部分类别数据

2021-05-26 22:04:41  阅读:192  来源: 互联网

标签:22 img VOC 数据 ann savepath 类别 VOC2007 os


 

 1 #!/usr/bin/env python
 2 # -*- encoding: utf-8 -*-
 3 """
 4 @Author  :   {FirstElfin}
 5 @License :   (C) Copyright 2013-2020, {DHWL}
 6 @Contact :   {2968793701@qq.com}
 7 @Software:   PyCharm
 8 @File    :   test.py
 9 @Time    :   11/22/19 11:55 AM
10 """
11 import os
12 import xml.etree.ElementTree as ET
13 import shutil
14 
15 ann_filepath = './VOC2007_22/Annotations/'
16 img_filepath = './VOC2007_22/JPEGImages/'
17 img_savepath = './VOC2007/JPEGImages/'
18 ann_savepath = './VOC2007/Annotations/'
19 if not os.path.exists(img_savepath):
20     os.mkdir(img_savepath)
21 
22 if not os.path.exists(ann_savepath):
23     os.mkdir(ann_savepath)
24 
25 classes = ['bicycle', 'bus', 'car', 'motorbike', 'train']
26 
27 
28 def save_annotation(file):
29 
30     tree = ET.parse(ann_filepath + '/' + file)
31     root = tree.getroot()
32     result = root.findall("object")
33     bool_num = 0
34     for obj in result:
35         if obj.find("name").text not in classes:
36             root.remove(obj)
37         else:
38             bool_num = 1
39     if bool_num:
40         tree.write(ann_savepath + file)
41         return True
42     else:
43         return False
44 
45 
46 def save_images(file):
47     name_img = img_filepath + os.path.splitext(file)[0] + ".jpg"
48     shutil.copy(name_img, img_savepath)
49     return True
50 
51 
52 if __name__ == '__main__':
53     for f in os.listdir(ann_filepath):
54         if save_annotation(f):
55             save_images(f)

  ./VOC2007_22是备份的数据集(原数据集),./VOC2007是我们自己要生成的数据集(制作自己的数据)

  通过classes可以选择你需要的类别,原始类别为:

1 classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
2            'bus', 'car', 'cat', 'chair', 'cow', 'diningtable',
3            'dog', 'horse', 'motorbike', 'pottedplant',
4            'sheep', 'sofa', 'train', 'tvmonitor', 'person']

 

 

 

标签:22,img,VOC,数据,ann,savepath,类别,VOC2007,os
来源: https://blog.51cto.com/u_15228753/2819786

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有