keras_edit_image.py
2.78 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
import numpy as np
import os
np.random.seed(3)
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
# 원본 위
optInputPath = '~/yoga/'
# 저장될 위치
optOutputPath = '~/preview/'
# 이미지 크기 조정 비율
optRescale = 1./255
# 이미지 회전
optRotationRange=10
# 이미지 수평 이동
optWidthShiftRange=0.2
# 이미지 수직 이동
optHeightShiftRange=0.2
# 이미지 밀림 강도
optShearRange=0.5
# 이미지 확대/ 축소
optZoomRange=[0.9,2.2]
# 이미지 수평 뒤집기
optHorizontalFlip = True
# 이미지 수직 뒤집기
optVerticalFlip = False
optFillMode='nearest'
# 이미지당 늘리는 갯수
optNbrOfIncreasePerPic = 3
# 배치 수
optNbrOfBatchPerPic = 2
train_datagen = ImageDataGenerator(rescale=optRescale,
rotation_range=optRotationRange,
width_shift_range=optWidthShiftRange,
height_shift_range=optHeightShiftRange,
shear_range=optShearRange,
zoom_range=optZoomRange,
horizontal_flip=optHorizontalFlip,
vertical_flip=optVerticalFlip,
fill_mode=optFillMode)
def checkFoler(path):
try:
if not(os.path.isdir(path)):
os.makedirs(os.path.join(path))
except OSError as e:
if e.errno != errno.EEXIST:
raise
def increaseImage(path ,folder):
for index in range(0,optNbrOfIncreasePerPic):
img = load_img(path)
x = img_to_array(img)
x = x.reshape((1,) + x.shape)
i = 0
checkFoler(optOutputPath+folder)
print('index : ' + str(index))
for batch in train_datagen.flow(x, batch_size=1, save_to_dir=optOutputPath+folder, save_prefix='tri', save_format='jpg'):
i += 1
print(folder + " " + str(i))
if i >= optNbrOfBatchPerPic:
break
def generator(dirName):
checkFoler(optOutputPath)
try:
fileNames = os.listdir(dirName)
for fileName in fileNames:
fullFileName = os.path.join(dirName, fileName)
if os.path.isdir(fullFileName):
generator(fullFileName)
else:
ext = os.path.splitext(fullFileName)[-1]
folderName = os.path.splitext(fullFileName)[0].split('/')[-2]
if(ext == '.jpg'):
increaseImage(fullFileName, folderName)
except PermissionError:
pass
if __name__ == "__main__":
generator(optInputPath)