-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascal_voc.py
More file actions
97 lines (82 loc) · 4.14 KB
/
Copy pathpascal_voc.py
File metadata and controls
97 lines (82 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from pathlib import Path
import os
import sys
# Define IN paths
train_path = '/content/images/train'
val_path = '/content/images/validation'
test_path = '/content/images/test'
# Define OUT paths
pascal_voc_path = '/content/images/pascal_voc'
train_folder = '/train'
val_folder = '/validation'
test_folder = '/test'
images_folder = '/images'
annotation_folder = '/Annotations'
Path(pascal_voc_path + train_folder + images_folder).mkdir(parents=True, exist_ok=True)
Path(pascal_voc_path + train_folder + annotation_folder).mkdir(parents=True, exist_ok=True)
Path(pascal_voc_path + val_folder + images_folder).mkdir(parents=True, exist_ok=True)
Path(pascal_voc_path + val_folder + annotation_folder).mkdir(parents=True, exist_ok=True)
Path(pascal_voc_path + test_folder + images_folder).mkdir(parents=True, exist_ok=True)
Path(pascal_voc_path + test_folder + annotation_folder).mkdir(parents=True, exist_ok=True)
# Get list of all training images
jpeg_file_list = [path for path in Path(train_path).rglob('*.jpeg')]
jpg_file_list = [path for path in Path(train_path).rglob('*.jpg')]
png_file_list = [path for path in Path(train_path).rglob('*.png')]
bmp_file_list = [path for path in Path(train_path).rglob('*.bmp')]
if sys.platform == 'linux':
JPEG_file_list = [path for path in Path(train_path).rglob('*.JPEG')]
JPG_file_list = [path for path in Path(train_path).rglob('*.JPG')]
image_file_list = jpg_file_list + JPG_file_list + png_file_list + bmp_file_list + JPEG_file_list + jpeg_file_list
else:
image_file_list = jpg_file_list + png_file_list + bmp_file_list + jpeg_file_list
file_num = len(image_file_list)
for i in range(file_num):
move_me = image_file_list[i]
fn = move_me.name
base_fn = move_me.stem
parent_path = move_me.parent
xml_fn = base_fn + '.xml'
os.rename(move_me, pascal_voc_path + train_folder + images_folder + '/' + fn)
xml_me = os.path.join(parent_path, xml_fn)
if (os.path.isfile(xml_me)):
os.rename(xml_me, os.path.join(pascal_voc_path + train_folder + annotation_folder, xml_fn))
# Get list of all validation images
jpeg_file_list = [path for path in Path(val_path).rglob('*.jpeg')]
jpg_file_list = [path for path in Path(val_path).rglob('*.jpg')]
png_file_list = [path for path in Path(val_path).rglob('*.png')]
bmp_file_list = [path for path in Path(val_path).rglob('*.bmp')]
if sys.platform == 'linux':
JPEG_file_list = [path for path in Path(val_path).rglob('*.JPEG')]
JPG_file_list = [path for path in Path(val_path).rglob('*.JPG')]
image_file_list = jpg_file_list + JPG_file_list + png_file_list + bmp_file_list + JPEG_file_list + jpeg_file_list
else:
image_file_list = jpg_file_list + png_file_list + bmp_file_list + jpeg_file_list
file_num = len(image_file_list)
for i in range(file_num):
move_me = image_file_list[i]
fn = move_me.name
base_fn = move_me.stem
parent_path = move_me.parent
xml_fn = base_fn + '.xml'
os.rename(move_me, pascal_voc_path + val_folder + images_folder + '/' + fn)
os.rename(os.path.join(parent_path, xml_fn), os.path.join(pascal_voc_path + val_folder + annotation_folder, xml_fn))
# Get list of all test images
jpeg_file_list = [path for path in Path(test_path).rglob('*.jpeg')]
jpg_file_list = [path for path in Path(test_path).rglob('*.jpg')]
png_file_list = [path for path in Path(test_path).rglob('*.png')]
bmp_file_list = [path for path in Path(test_path).rglob('*.bmp')]
if sys.platform == 'linux':
JPEG_file_list = [path for path in Path(test_path).rglob('*.JPEG')]
JPG_file_list = [path for path in Path(test_path).rglob('*.JPG')]
image_file_list = jpg_file_list + JPG_file_list + png_file_list + bmp_file_list + JPEG_file_list + jpeg_file_list
else:
image_file_list = jpg_file_list + png_file_list + bmp_file_list + jpeg_file_list
file_num = len(image_file_list)
for i in range(file_num):
move_me = image_file_list[i]
fn = move_me.name
base_fn = move_me.stem
parent_path = move_me.parent
xml_fn = base_fn + '.xml'
os.rename(move_me, pascal_voc_path + test_folder + images_folder + '/' + fn)
os.rename(os.path.join(parent_path, xml_fn), os.path.join(pascal_voc_path + test_folder + annotation_folder, xml_fn))