-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
89 lines (83 loc) · 3.84 KB
/
dataset.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
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
#import libraries
import shutil
import os
from PIL import Image
import PIL
#Create relevant directories for preprocessing if they don't exist
if not os.path.exists("/home/ubuntu/data/VLR/dataset/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/")
print("Dataset Directory Created.")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/test/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/test/")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/train/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/train/")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/test/blur/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/test/blur/")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/test/sharp/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/test/sharp/")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/train/blur/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/train/blur/")
if not os.path.exists("/home/ubuntu/data/VLR/dataset/train/sharp/"):
os.makedirs("/home/ubuntu/data/VLR/dataset/train/sharp/")
test_source_folder = r"/home/ubuntu/data/VLR/raw_dataset/test/"
train_source_folder = r"/home/ubuntu/data/VLR/raw_dataset/train/"
test_destination_folder = r"/home/ubuntu/data/VLR/dataset/test/"
train_destination_folder = r"/home/ubuntu/data/VLR/dataset/train/"
i = 0
j = 0
k = 0
l = 0
#Preprocessing Test files to newly created directories.
print("Preprocessing Test Files.")
for file_name in os.listdir(test_source_folder):
subfile = test_source_folder + file_name
# print(subfile)
for image_file in os.listdir(subfile):
if image_file == 'blur':
images_dir = os.path.join(subfile, image_file)
for fname in os.listdir(images_dir):
src_file = os.path.join(images_dir, fname)
new_dir = os.path.join(test_destination_folder, image_file)
if (len(str(i))<6):
save_value = '0'*(6-len(str(i))) + str(i)
dst_name = new_dir + '/' + save_value + '.png'
os.rename(src_file, dst_name)
i += 1
elif image_file == 'sharp':
images_dir = os.path.join(subfile, image_file)
for fname in os.listdir(images_dir):
src_file = os.path.join(images_dir, fname)
new_dir = os.path.join(test_destination_folder, image_file)
if (len(str(i))<6):
save_value = '0'*(6-len(str(i))) + str(i)
dst_name = new_dir + '/' + save_value + '.png'
os.rename(src_file, dst_name)
j += 1
print("Test Files Copied.")
#Preprocessing Train files to newly created directories.
for file_name in os.listdir(train_source_folder):
subfile = train_source_folder + file_name
# print(subfile)
for image_file in os.listdir(subfile):
if image_file == 'blur':
images_dir = os.path.join(subfile, image_file)
for fname in os.listdir(images_dir):
src_file = os.path.join(images_dir, fname)
new_dir = os.path.join(train_destination_folder, image_file)
if (len(str(i))<6):
save_value = '0'*(6-len(str(i))) + str(i)
dst_name = new_dir + '/' + save_value + '.png'
os.rename(src_file, dst_name)
k += 1
elif image_file == 'sharp':
images_dir = os.path.join(subfile, image_file)
for fname in os.listdir(images_dir):
src_file = os.path.join(images_dir, fname)
new_dir = os.path.join(train_destination_folder, image_file)
if (len(str(i))<6):
save_value = '0'*(6-len(str(i))) + str(i)
dst_name = new_dir + '/' + save_value + '.png'
os.rename(src_file, dst_name)
l += 1
print("Train Files Copied.")
print("All Files Copied.")