/utils.py

https://gitlab.com/bsuper/convnet-keras
Python | 178 lines | 142 code | 15 blank | 21 comment | 28 complexity | 00a9ea5b639733ab84299d3e24a54dda MD5 | raw file
  1. import os
  2. import sys
  3. import glob
  4. import argparse
  5. import shutil
  6. import PIL
  7. from PIL import Image
  8. import random
  9. from random import shuffle
  10. def number_snapshots(input_dir, count):
  11. # import pdb; pdb.set_trace()
  12. count = count
  13. oldwd = os.getcwd()
  14. os.chdir(input_dir) # go to /images/
  15. print "Count is: {0}".format(count)
  16. print glob.glob("./*") # ./train, ./test, ./val
  17. for ttv in glob.glob("./*"):
  18. folders_list = glob.glob(ttv + "/*") # ./train/face ...
  19. for folder in folders_list:
  20. print folder
  21. fldername = folder.split("/")[-1] # face or no_face
  22. outfolder = "./" + ttv.split("/")[-1] + "/new_" + fldername + "/"
  23. files_list = glob.glob(folder + "/*")
  24. for f in files_list:
  25. try:
  26. if not os.path.exists(outfolder):
  27. os.makedirs(outfolder)
  28. Image.open(f).save(outfolder + str(count) + ".jpg", "JPEG")
  29. count += 1
  30. except IOError:
  31. print "Cannot convert: {0}".format(f)
  32. sys.exit(1)
  33. # remove the old face/ and not_face/ dirs
  34. shutil.rmtree(folder)
  35. os.rename(outfolder, folder)
  36. os.chdir(oldwd)
  37. print "Done."
  38. def resize(input_dir, resize_dim):
  39. oldwd = os.getcwd()
  40. os.chdir(input_dir)
  41. print glob.glob("./*") # ./train, ./teset, ./val
  42. for ttv in glob.glob("./*"):
  43. folders_list = glob.glob(ttv + "/*")
  44. for folder in folders_list:
  45. print folder
  46. fldername = folder.split("/")[-1]
  47. outfolder = "./" + ttv.split("/")[-1] + "/new_" + fldername + "/"
  48. files_list = glob.glob(folder + "/*")
  49. for f in files_list:
  50. outfile = f.split('/')[-1]
  51. try:
  52. if not os.path.exists(outfolder):
  53. os.makedirs(outfolder)
  54. ### DO THIS to Preserve aspect ratio ###
  55. # basewidth = int(args.resize_width) # ex. 300 px
  56. # img = Image.open(f)
  57. # wpercent = (basewidth/float(img.size[0]))
  58. # hsize = int((float(img.size[1])*float(wpercent)))
  59. # img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)
  60. # img.save(outfolder + str(count) + ".jpg", "JPEG")
  61. #### DO This to Create Square Images ###
  62. img = Image.open(f)
  63. size = tuple(map(lambda k: int(k), resize_dim.split('x')))
  64. img = img.resize(size, Image.ANTIALIAS)
  65. img.save(outfolder + outfile, "JPEG")
  66. except IOError:
  67. print "Cannot convert: {0}".format(f)
  68. sys.exit(1)
  69. # remove the old face no_face dirs and rename the cropped dirs
  70. shutil.rmtree(folder)
  71. os.rename(outfolder, folder)
  72. os.chdir(oldwd)
  73. print "Done."
  74. def convert2txt(input_dir, txt_dir):
  75. """ Creates train.txt, test.txt, val.txt files from the input images folder """
  76. images_root = input_dir # ./data/images/
  77. txt_dir = txt_dir # ./data/txt/
  78. for ttv in glob.glob(images_root + "*"):
  79. # ttv will be test, train, val
  80. print ttv+"\n"
  81. data_src = ttv.split('/')[-1] # train, test, val
  82. writefolder = txt_dir
  83. writefilename = data_src + '.txt'
  84. print "Writing to: {0}\n".format(writefilename)
  85. if not os.path.exists(writefolder):
  86. os.makedirs(writefolder)
  87. old_pwd = os.getcwd()
  88. print "Changing dir"
  89. os.chdir(old_pwd + "/" + writefolder)
  90. with open(writefilename, 'w') as wf:
  91. print "Opened file: " + writefilename
  92. os.chdir(old_pwd)
  93. print glob.glob(ttv + "/*")
  94. for label in glob.glob(ttv + "/*"):
  95. # label might be .data/images/train/face or ..../no_face
  96. print label
  97. for imgsrc in glob.glob(label + "/*"):
  98. line = imgsrc.replace(input_dir, "") # should be in form 'train/no_face/472.jpg'
  99. if "no_face" in label.split('/'):
  100. # label them 0
  101. line = line + " 0\n" # 'train/no_face/472.jpg 0'
  102. else:
  103. line = line + " 1\n" # 'train/face'
  104. wf.write(line)
  105. print "Done."
  106. def shuffle_all_data(images_root, ratio):
  107. """ images_root is going to be ./data/images/
  108. ratio determines train/val/test data ratios
  109. by default it is set to 48:32:20 percentages
  110. """
  111. ratios = [int(e) for e in ratio.split(":")] # ratios [train : test : val]
  112. image_paths = []
  113. ttv_s = glob.glob(images_root + "*")
  114. # collect all image paths in one big list
  115. for ttv in ttv_s:
  116. for folder in glob.glob(ttv + "/*"):
  117. image_paths.extend(glob.glob(folder + "/*"))
  118. # import pdb; pdb.set_trace()
  119. random.shuffle(image_paths, random.random) # in-place shuffling
  120. size = len(image_paths)
  121. train_size = size*ratios[0]/100
  122. test_size = size*ratios[1]/100
  123. val_size = size - train_size - test_size
  124. if not os.path.exists(images_root + "train_shuffled/"):
  125. os.makedirs(images_root + "train_shuffled/")
  126. os.makedirs(images_root + "train_shuffled/face/")
  127. os.makedirs(images_root + "train_shuffled/no_face/")
  128. if not os.path.exists(images_root + "test_shuffled/"):
  129. os.makedirs(images_root + "test_shuffled/")
  130. os.makedirs(images_root + "test_shuffled/face/")
  131. os.makedirs(images_root + "test_shuffled/no_face/")
  132. if not os.path.exists(images_root + "val_shuffled/"):
  133. os.makedirs(images_root + "val_shuffled/")
  134. os.makedirs(images_root + "val_shuffled/face/")
  135. os.makedirs(images_root + "val_shuffled/no_face/")
  136. train_paths = image_paths[0 : train_size]
  137. test_paths = image_paths[train_size : train_size+test_size]
  138. val_paths = image_paths[-val_size:]
  139. for srcpath in train_paths:
  140. destpath = srcpath.replace("/train/","/train_shuffled/").replace("/test/",
  141. "/train_shuffled/").replace("/val/","/train_shuffled/")
  142. print "Source: {0} | Dest {1}\n".format(srcpath, destpath)
  143. os.rename(srcpath, destpath)
  144. print "Training Shuffled...\n"
  145. for srcpath in test_paths:
  146. destpath = srcpath.replace("/train/", "/test_shuffled/").replace("/test/",
  147. "/test_shuffled/").replace("/val/", "/test_shuffled/")
  148. print "Source: {0} | Dest {1}\n".format(srcpath, destpath)
  149. os.rename(srcpath, destpath)
  150. print "Testing Shuffled...\n"
  151. for srcpath in val_paths:
  152. destpath = srcpath.replace("/train/", "/val_shuffled/").replace("/test/",
  153. "/val_shuffled/").replace("/val/", "/val_shuffled/")
  154. print "Source: {0} | Dest {1}\n".format(srcpath, destpath)
  155. os.rename(srcpath, destpath)
  156. print "Val Shuffled...\n"
  157. shutil.rmtree(images_root + "train/")
  158. os.rename(images_root + "train_shuffled/", images_root + "train/")
  159. shutil.rmtree(images_root + "test/")
  160. os.rename(images_root + "test_shuffled/", images_root + "test/")
  161. shutil.rmtree(images_root + "val/")
  162. os.rename(images_root + "val_shuffled/", images_root + "val/")
  163. print "Done."