최지우

add .rec conversion cod and image data processing code

1 +import numpy as np
2 +import os
3 +
4 +np.random.seed(3)
5 +
6 +from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
7 +# 원본 위
8 +optInputPath = '~/yoga/'
9 +# 저장될 위치
10 +optOutputPath = '~/preview/'
11 +
12 +# 이미지 크기 조정 비율
13 +optRescale = 1./255
14 +# 이미지 회전
15 +optRotationRange=10
16 +# 이미지 수평 이동
17 +optWidthShiftRange=0.2
18 +# 이미지 수직 이동
19 +optHeightShiftRange=0.2
20 +# 이미지 밀림 강도
21 +optShearRange=0.5
22 +# 이미지 확대/ 축소
23 +optZoomRange=[0.9,2.2]
24 +# 이미지 수평 뒤집기
25 +optHorizontalFlip = True
26 +# 이미지 수직 뒤집기
27 +optVerticalFlip = False
28 +optFillMode='nearest'
29 +# 이미지당 늘리는 갯수
30 +optNbrOfIncreasePerPic = 3
31 +# 배치 수
32 +optNbrOfBatchPerPic = 2
33 +
34 +train_datagen = ImageDataGenerator(rescale=optRescale,
35 + rotation_range=optRotationRange,
36 + width_shift_range=optWidthShiftRange,
37 + height_shift_range=optHeightShiftRange,
38 + shear_range=optShearRange,
39 + zoom_range=optZoomRange,
40 + horizontal_flip=optHorizontalFlip,
41 + vertical_flip=optVerticalFlip,
42 + fill_mode=optFillMode)
43 +
44 +def checkFoler(path):
45 + try:
46 + if not(os.path.isdir(path)):
47 + os.makedirs(os.path.join(path))
48 + except OSError as e:
49 + if e.errno != errno.EEXIST:
50 + raise
51 +
52 +def increaseImage(path ,folder):
53 + for index in range(0,optNbrOfIncreasePerPic):
54 + img = load_img(path)
55 + x = img_to_array(img)
56 + x = x.reshape((1,) + x.shape)
57 + i = 0
58 + checkFoler(optOutputPath+folder)
59 + print('index : ' + str(index))
60 + for batch in train_datagen.flow(x, batch_size=1, save_to_dir=optOutputPath+folder, save_prefix='tri', save_format='jpg'):
61 + i += 1
62 + print(folder + " " + str(i))
63 + if i >= optNbrOfBatchPerPic:
64 + break
65 +
66 +def generator(dirName):
67 + checkFoler(optOutputPath)
68 + try:
69 + fileNames = os.listdir(dirName)
70 + for fileName in fileNames:
71 + fullFileName = os.path.join(dirName, fileName)
72 + if os.path.isdir(fullFileName):
73 + generator(fullFileName)
74 + else:
75 + ext = os.path.splitext(fullFileName)[-1]
76 + folderName = os.path.splitext(fullFileName)[0].split('/')[-2]
77 + if(ext == '.jpg'):
78 + increaseImage(fullFileName, folderName)
79 +
80 + except PermissionError:
81 + pass
82 +
83 +if __name__ == "__main__":
84 + generator(optInputPath)
1 +import mxnet as mx
2 +import os
3 +
4 +MXNET_HOME="/home/eraser/mxnet"
5 +
6 +os.system('python3 %s/tools/im2rec.py /home/eraser/train /home/eraser/yoga_data/train --recursive --list --num-thread 8'%MXNET_HOME)
7 +os.system('python3 %s/tools/im2rec.py /home/eraser/validation /home/eraser/yoga_data/validation --recursive --list --num-thread 8'%MXNET_HOME)
8 +
9 +os.system('python3 %s/tools/im2rec.py /home/eraser/train /home/eraser/yoga_data/train --recursive --pass-through --pack-label --num-thread 8'%MXNET_HOME)
10 +os.system('python3 %s/tools/im2rec.py /home/eraser/validation /home/eraser/yoga_data/validation --recursive --pass-through --pack-label --num-thread 8'%MXNET_HOME)
...\ No newline at end of file ...\ No newline at end of file