Skip to content

Commit 3213e91

Browse files
Make script work with nested folders
1 parent 18da634 commit 3213e91

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

Diff for: util_scripts/train_val_test_split.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# https://colab.research.google.com/github/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/Train_TFLite2_Object_Detction_Model.ipynb
88

99
import glob
10+
from pathlib import Path
1011
import random
1112
import os
1213

@@ -17,10 +18,10 @@
1718
test_path = '/content/images/test'
1819

1920
# Get list of all images
20-
jpg_file_list = glob.glob(image_path + '/*.jpg')
21-
JPG_file_list = glob.glob(image_path + '/*.JPG')
22-
png_file_list = glob.glob(image_path + '/*.png')
23-
bmp_file_list = glob.glob(image_path + '/*.bmp')
21+
jpg_file_list = [path for path in Path(image_path).rglob('*.jpg')]
22+
JPG_file_list = [path for path in Path(image_path).rglob('*.JPG')]
23+
png_file_list = [path for path in Path(image_path).rglob('*.png')]
24+
bmp_file_list = [path for path in Path(image_path).rglob('*.bmp')]
2425

2526
file_list = jpg_file_list + JPG_file_list + png_file_list + bmp_file_list
2627
file_num = len(file_list)
@@ -33,35 +34,39 @@
3334
train_num = int(file_num*train_percent)
3435
val_num = int(file_num*val_percent)
3536
test_num = file_num - train_num - val_num
36-
print('Files moving to train: %d' % train_num)
37-
print('Files moving to validation: %d' % val_num)
38-
print('Files moving to test: %d' % test_num)
37+
print('Images moving to train: %d' % train_num)
38+
print('Images moving to validation: %d' % val_num)
39+
print('Images moving to test: %d' % test_num)
3940

4041
# Select 80% of files randomly and move them to train folder
4142
for i in range(train_num):
4243
move_me = random.choice(file_list)
43-
fn = move_me.split('/')[-1]
44-
base_fn = fn[:(len(fn)-4)] # Gets rid of .jpg, .png, or .bmp at end of the string
44+
fn = move_me.name
45+
base_fn = move_me.stem
46+
parent_path = move_me.parent
4547
xml_fn = base_fn + '.xml'
4648
os.rename(move_me, train_path+'/'+fn)
47-
os.rename(os.path.join(image_path,xml_fn),os.path.join(train_path,xml_fn))
49+
os.rename(os.path.join(parent_path,xml_fn),os.path.join(train_path,xml_fn))
4850
file_list.remove(move_me)
4951

5052
# Select 10% of remaining files and move them to validation folder
5153
for i in range(val_num):
5254
move_me = random.choice(file_list)
53-
fn = move_me.split('/')[-1]
54-
base_fn = fn[:(len(fn)-4)] # Gets rid of .jpg, .png, or .bmp at end of the string
55+
fn = move_me.name
56+
base_fn = move_me.stem
57+
parent_path = move_me.parent
5558
xml_fn = base_fn + '.xml'
5659
os.rename(move_me, val_path+'/'+fn)
57-
os.rename(os.path.join(image_path,xml_fn),os.path.join(val_path,xml_fn))
60+
os.rename(os.path.join(parent_path,xml_fn),os.path.join(val_path,xml_fn))
5861
file_list.remove(move_me)
5962

6063
# Move remaining files to test folder
6164
for i in range(test_num):
62-
file = file_list[i]
63-
fn = file.split('/')[-1]
64-
base_fn = fn[:(len(fn)-4)] # Gets rid of .jpg, .png, or .bmp at end of the string
65+
move_me = random.choice(file_list)
66+
fn = move_me.name
67+
base_fn = move_me.stem
68+
parent_path = move_me.parent
6569
xml_fn = base_fn + '.xml'
66-
os.rename(file, test_path+'/'+fn)
67-
os.rename(os.path.join(image_path,xml_fn),os.path.join(test_path,xml_fn))
70+
os.rename(move_me, test_path+'/'+fn)
71+
os.rename(os.path.join(parent_path,xml_fn),os.path.join(test_path,xml_fn))
72+
file_list.remove(move_me)

0 commit comments

Comments
 (0)