-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_prep.py
52 lines (40 loc) · 1.15 KB
/
data_prep.py
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
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 9 23:10:58 2019
@author: -
"""
import os
from glob import iglob
from os.path import join,basename
import shutil
import random
data_path = './data/'
train_path = './train'
test_path = './test'
# create training set
print('_'*30)
print('Creating training set....')
print('_'*30)
for file in iglob(join(data_path,'*')):
save_path = join(train_path,basename(file))
if not os.path.exists(save_path):
os.makedirs(save_path)
for img in iglob(join(file,'*')):
shutil.copy2(img,save_path)
print(img)
print('_'*30)
print('Creating test set....')
print('_'*30)
for file in iglob(join(train_path,'*')):
save_path = join(test_path, basename(file))
if not os.path.exists(save_path):
os.makedirs(save_path)
total_imgs = [x for x in iglob(join(file,'*'))]
rand_amt = 0.15 * len(total_imgs) # select 15% of data from each category as testing set
test_imgs= []
for i in range(int(rand_amt)):
img = random.choice(total_imgs)
if img not in test_imgs:
shutil.move(img,save_path)
test_imgs.append(img)
print(img)