1+ # -*- coding: utf-8 -*-
2+
3+ """
4+ @date: 2020/4/22 下午2:05
5+ @file: pascal_voc_07.py
6+ @author: zj
7+ @description: 下载并解压PASCAL VOC数据集
8+ """
9+
10+ import cv2
11+ import os
12+ import glob
13+ import shutil
14+ import numpy as np
15+ from torchvision .datasets import VOCDetection
16+
17+ from utils import util
18+
19+
20+ def get_dataset (root_dir ):
21+ """
22+ 下载PASCAL VOC数据集
23+ """
24+ dataset = VOCDetection (root_dir , year = '2007' , image_set = 'trainval' , download = True )
25+
26+ # img, target = dataset.__getitem__(1000)
27+ # img = np.array(img)
28+ # print(target)
29+ # print(img.shape)
30+
31+ # cv2.imshow('img', img)
32+ # cv2.waitKey(0)
33+
34+ return dataset
35+
36+
37+ def pretreat (src_root_dir , dst_root_dir ):
38+ """
39+ 判断源文件目录是否为空
40+ 清空结果文件目录,并新建图像和标注文件夹
41+ :return:
42+ """
43+ if not os .path .exists (src_root_dir ):
44+ util .error ("%s doesn't exist" % src_root_dir )
45+ if os .path .exists (dst_root_dir ):
46+ shutil .rmtree (dst_root_dir )
47+ os .mkdir (dst_root_dir )
48+
49+ dst_img_dir = os .path .join (dst_root_dir , 'imgs' )
50+ dst_annotation_dir = os .path .join (dst_root_dir , 'annotations' )
51+ os .mkdir (dst_img_dir )
52+ os .mkdir (dst_annotation_dir )
53+
54+ return src_root_dir , dst_root_dir , dst_img_dir , dst_annotation_dir
55+
56+
57+ if __name__ == '__main__' :
58+ data_dir = '../../data'
59+ data_set = get_dataset (data_dir )
60+
61+ src_root_dir = '../../data/VOCdevkit/VOC2007'
62+ dst_root_dir = '../../data/VOC_dataset'
63+ src_root_dir , dst_root_dir , dst_img_dir , dst_annotation_dir = pretreat (src_root_dir , dst_root_dir )
64+
65+ img_path_list = glob .glob (os .path .join (src_root_dir , 'JPEGImages' , '*.jpg' ))
66+ annotation_path_list = glob .glob (os .path .join (src_root_dir , 'Annotations' , '*.xml' ))
67+
68+ for img_path in img_path_list :
69+ dst_img_path = os .path .join (dst_img_dir , os .path .basename (img_path ))
70+ shutil .copyfile (img_path , dst_img_path )
71+
72+ for annotation_path in annotation_path_list :
73+ dst_annotation_path = os .path .join (dst_annotation_dir , os .path .basename (annotation_path ))
74+ shutil .copyfile (annotation_path , dst_annotation_path )
75+
76+ print ('done' )
0 commit comments