-
Notifications
You must be signed in to change notification settings - Fork 3
/
PreProcessDataset.py
62 lines (50 loc) · 2.15 KB
/
PreProcessDataset.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
from utils.utils import CreateFolder, LoadImagesPaths
import cv2, argparse, os
def CropImage(Image, Size):
""" Cropping Image """
i = 0
Images = []
while(i + Size < Image.shape[0]):
j = 0
while(j + Size < Image.shape[1]):
Images.append(Image[i:i + Size, j: j + Size, ])
j += Size
i += Size
return Images
def CropImages(ImagesPaths, Size):
SaveDirName = "./TrainDataset/"
CreateFolder(SaveDirName)
ImagesCountLength = len(str(len(ImagesPaths) * 30))
i = 1
for ImagePath in ImagesPaths:
Image = cv2.imread(ImagePath)
Images = CropImage(Image, Size)
for Image in Images:
cv2.imwrite(SaveDirName + '0' * (ImagesCountLength - len(str(i))) + str(i) + ".png", Image)
i += 1
def ResizeImages(ImagesPaths, Size):
SaveDirName = "./DatasetResized_{}/".format(Size)
CreateFolder(SaveDirName)
ImagesCountLength = len(str(len(ImagesPaths)))
i = 1
for ImagePath in ImagesPaths:
Image = cv2.imread(ImagePath)
Max_HW = min(Image.shape[0], Image.shape[1])
Image = Image[:Max_HW, :Max_HW, :]
Image = cv2.resize(Image, (Size, Size))
cv2.imwrite(SaveDirName + '0' * (ImagesCountLength - len(str(i))) + str(i) + ".png", Image)
i += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dataset-directories', type = str, default = os.getcwd() + '/Dataset/', help = 'Directories Paths of Dataset')
parser.add_argument('-s', '--size-image', type = int, default = 256, help = 'PreProcess Image to Size')
parser.add_argument('-t', '--preprocessing-type', type = int, default = 1, help = 'PreProcessing Type; 1 >> Cropping, 2 >> Resizing')
FLAGS, unparsed = parser.parse_known_args()
ImagesPaths = []
for Directory in FLAGS.dataset_directories.split(','):
ImagesPaths += LoadImagesPaths(Directory)
if FLAGS.preprocessing_type == 1:
CropImages(ImagesPaths, FLAGS.size_image)
elif FLAGS.preprocessing_type == 2:
ResizeImages(ImagesPaths, FLAGS.size_image)
# python PreProcessDataset.py -d ./Dataset/ -s 128 -t 1