Guest User

Untitled

a guest
Jun 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import numpy as np
  2. from pathlib import Path
  3. from os.path import basename
  4. import matplotlib.image as mpimg
  5. import curses
  6.  
  7. """
  8. This script read all files recursively in given directory, create a class of
  9. each dir, and put the images (normalized numpy arrays) in a npy binary format
  10. in the form: (num_classes, num_exemple, h, w, c)
  11. """
  12.  
  13. def create_db(images_dir_path):
  14. if not Path(train_dir).is_dir():
  15. print('Error')
  16. exit()
  17.  
  18. dataset = [] # need (num_classes, num_exemple, h, w, c)
  19. for d in Path(images_dir_path).glob('*'): # for files/dir in pathdir
  20. # skipping files
  21. if not d.is_dir():
  22. continue;
  23.  
  24. classDataSet = []
  25. for f in Path(d).glob('*.png'):
  26. img = mpimg.imread(f)
  27. img = img.astype(np.float)
  28. img /= 255.0
  29. img = np.reshape(img, newshape=(img.shape[0], img.shape[1], 1))
  30. classDataSet.append(img) # list numpy images
  31.  
  32. dataset.append(classDataSet)
  33.  
  34. dataset.sort(key=len)
  35.  
  36. return np.array(dataset)
  37.  
  38.  
  39. train_dir = '/path/to/my/files'
  40. data = create_db(train_dir)
  41. np.save('my_database.npy', data)
Add Comment
Please, Sign In to add comment