Guest User

Untitled

a guest
Jan 17th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. """
  2. Created on Thu Oct 26 20:50:49 2017
  3. Multiprocessing program of independent pool workers uses the Pillow 3
  4. module to open PNG image files. The pool workers each individually
  5. convert the PNG image file to a numpy array of shape (300, 300, 3) where
  6. 300 represents the width and height of the image and 3 represents color
  7. format. The image's corresponding y_label is generated by doing keras'
  8. to_categorical encoding that transforms integers into different binary
  9. 1,0 encoding of classes. These classes are circle, rectangle, triangle,
  10. and square. The numpy array's total size of 2000 images per shape is
  11. reduced by a half if it's numpy float 16 instead of numpy float 32.
  12. @author: maggie
  13. """
  14. from __future__ import print_function
  15. from PIL import Image
  16. import glob
  17. import pickle
  18. import numpy as np
  19. from multiprocessing import Lock
  20. from multiprocessing import Pool
  21. from keras.utils import to_categorical
  22.  
  23. """to make lock and queue storage global to all child workers in the Pool"""
  24. def init(lock):
  25. global childs_lock
  26. childs_lock = lock
  27.  
  28. # this section of the code is for saving background colors
  29. # opens the text file with the name of image and the rgb colors
  30. def save_background_colors(image_path, file_txt):
  31. childs_lock.acquire()
  32. f = open(file_txt, 'r')
  33. img = Image.open(image_path)
  34. childs_lock.release()
  35.  
  36. previous_line = " "
  37. float_numbers = []
  38. for current_line in f:
  39. if image_path in previous_line:
  40. y = [current_line.strip() for current_line in current_line.split(' ')]
  41. #remove string literals by casting float to string
  42. for numbers in y:
  43. c = float(numbers)
  44. float_numbers.append(c)
  45. print (image_path, y)
  46. background_tuple = tuple(float_numbers)
  47. previous_line = current_line
  48. f.close()
  49. np_img = np.array(img, dtype = [('img_info', np.float16)])
  50.  
  51. return np_img['img_info'], background_tuple
  52.  
  53. def process_images(image_path, shape_path):
  54. shape_y = None
  55. if shape_path == "crop_images/valid_circle/":
  56. shape_y = 0
  57. if shape_path == "crop_images/valid_rectangle/":
  58. shape_y = 1
  59. if shape_path == "crop_images/valid_triangle/":
  60. shape_y = 2
  61. if shape_path == "crop_images/valid_square/":
  62. shape_y = 3
  63.  
  64. ylabel = to_categorical(shape_y, num_classes = 4)
  65. ylabel = np.reshape(ylabel, (4))
  66.  
  67. childs_lock.acquire()
  68. img = Image.open(image_path)
  69. childs_lock.release()
  70. np_img = np.array(img, dtype = [('img_info', np.float16)])
  71. #img = img.resize((200, 200), Image.ANTIALIAS) #ANTIALIAS reserves quality
  72. # to check that all image input are the same shape
  73. '''width, height = np_img.shape[0], np_img.shape[1]
  74. if width == 300 or height == 300:
  75. print ("rm ", image_path)'''
  76. img.close()
  77. return np_img['img_info'], ylabel
  78.  
  79. # global storage variable for both main and pool of workers
  80. pickle_file = 'crop_images/valid_circle.pkl'
  81. # create empty pickle_file first then append to file
  82. output = open (pickle_file, 'wb')
  83. output.close()
  84.  
  85. def result(data):
  86. output = open (pickle_file, 'ab')
  87. #print ("in pickle file: " , pickle_file)
  88. pickle.dump(data, output, pickle.HIGHEST_PROTOCOL)
  89. output.close()
  90.  
  91. if __name__ == '__main__':
  92.  
  93. shape_path = "crop_images/valid_circle/"
  94. lock = Lock()
  95. p = Pool(processes = 4, initargs = (lock, ), initializer = init)
  96.  
  97. for image_path in glob.glob(shape_path + "*png"):
  98. p.apply_async(process_images, (image_path, shape_path), callback = result)
  99. p.close() # no more tasks
  100. p.join() # wrap up current tasks
  101.  
  102. file_txt = "background_circle.txt"
  103. for image_path in glob.glob(shape_path + "*png"):
  104. p.apply_async(save_background_colors, (image_path, file_txt))
  105. p.close() # no more tasks
  106. p.join() # wrap up current tasks
Add Comment
Please, Sign In to add comment