Advertisement
Guest User

dataaug

a guest
Apr 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. from tqdm import tqdm
  5. from sklearn.model_selection import train_test_split
  6. import scipy.misc
  7. from keras.layers import Dense, Flatten, Input, Conv2D, Conv2DTranspose, MaxPooling2D, UpSampling2D
  8. from keras.models import Model
  9. from PIL import Image
  10. from keras.utils import to_categorical
  11. from sklearn.utils import shuffle
  12. from keras.preprocessing.image import ImageDataGenerator
  13. from keras.preprocessing.image import array_to_img, img_to_array, load_img
  14. gen = ImageDataGenerator(
  15. rotation_range=20,
  16. width_shift_range=0.2,
  17. height_shift_range=0.2,
  18. horizontal_flip=True
  19. )
  20.  
  21. APPLE_DIRECTORY = "./apple/"
  22.  
  23. apple_images = []
  24.  
  25.  
  26. for apple_dir in tqdm(os.listdir(APPLE_DIRECTORY)):
  27. for apple in os.listdir(APPLE_DIRECTORY + '/' + apple_dir):
  28. if apple.endswith('_crop.png'):
  29. img = cv2.imread(APPLE_DIRECTORY + '/' + apple_dir + '/' + apple)
  30. img = cv2.resize(img, (92,92), interpolation = cv2.INTER_AREA)
  31. #img = img_to_array(load_img(APPLE_DIRECTORY + '/' + apple_dir + '/' + apple))
  32. img = img_to_array(img)
  33. img = img.reshape((1,) + img.shape)
  34.  
  35. apple_images.append(img)
  36. #image= apple_images.reshape((1,)+ apple_images.shape)
  37. output_path = './apple_aug/apple_random{}.png'
  38. count =10
  39. # let's create infinite flow of images
  40. images_flow = gen.flow(img, batch_size=100)
  41. for i, new_images in enumerate(images_flow):
  42. # we access only first image because of batch_size=1
  43. new_image = array_to_img(new_images[0], scale=True)
  44. new_image.save(output_path.format(i + 1))
  45. if i >= count:
  46. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement