funnel_dataset.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """Performs face alignment and stores face thumbnails in the output directory."""
  2. from __future__ import absolute_import
  3. from __future__ import division
  4. from __future__ import print_function
  5. from scipy import misc
  6. import sys
  7. import os
  8. import argparse
  9. import facenet
  10. import subprocess
  11. from contextlib import contextmanager
  12. import tempfile
  13. import shutil
  14. import numpy as np
  15. @contextmanager
  16. def TemporaryDirectory():
  17. name = tempfile.mkdtemp()
  18. try:
  19. yield name
  20. finally:
  21. shutil.rmtree(name)
  22. def main(args):
  23. funnel_cmd = 'funnelReal'
  24. funnel_model = 'people.train'
  25. output_dir = os.path.expanduser(args.output_dir)
  26. if not os.path.exists(output_dir):
  27. os.makedirs(output_dir)
  28. # Store some git revision info in a text file in the output directory
  29. src_path,_ = os.path.split(os.path.realpath(__file__))
  30. facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
  31. dataset = facenet.get_dataset(args.input_dir)
  32. np.random.shuffle(dataset)
  33. # Scale the image such that the face fills the frame when cropped to crop_size
  34. #scale = float(args.face_size) / args.image_size
  35. with TemporaryDirectory() as tmp_dir:
  36. for cls in dataset:
  37. output_class_dir = os.path.join(output_dir, cls.name)
  38. tmp_output_class_dir = os.path.join(tmp_dir, cls.name)
  39. if not os.path.exists(output_class_dir) and not os.path.exists(tmp_output_class_dir):
  40. print('Aligning class %s:' % cls.name)
  41. tmp_filenames = []
  42. if not os.path.exists(tmp_output_class_dir):
  43. os.makedirs(tmp_output_class_dir)
  44. input_list_filename = os.path.join(tmp_dir, 'input_list.txt')
  45. output_list_filename = os.path.join(tmp_dir, 'output_list.txt')
  46. input_file = open(input_list_filename, 'w')
  47. output_file = open(output_list_filename,'w')
  48. for image_path in cls.image_paths:
  49. filename = os.path.split(image_path)[1]
  50. input_file.write(image_path+'\n')
  51. output_filename = os.path.join(tmp_output_class_dir, filename)
  52. output_file.write(output_filename+'\n')
  53. tmp_filenames.append(output_filename)
  54. input_file.close()
  55. output_file.close()
  56. cmd = args.funnel_dir+funnel_cmd + ' ' + input_list_filename + ' ' + args.funnel_dir+funnel_model + ' ' + output_list_filename
  57. subprocess.call(cmd, shell=True)
  58. # Resize and crop images
  59. if not os.path.exists(output_class_dir):
  60. os.makedirs(output_class_dir)
  61. scale = 1.0
  62. for tmp_filename in tmp_filenames:
  63. img = misc.imread(tmp_filename)
  64. img_scale = misc.imresize(img, scale)
  65. sz1 = img.shape[1]/2
  66. sz2 = args.image_size/2
  67. img_crop = img_scale[int(sz1-sz2):int(sz1+sz2),int(sz1-sz2):int(sz1+sz2),:]
  68. filename = os.path.splitext(os.path.split(tmp_filename)[1])[0]
  69. output_filename = os.path.join(output_class_dir, filename+'.png')
  70. print('Saving image %s' % output_filename)
  71. misc.imsave(output_filename, img_crop)
  72. # Remove tmp directory with images
  73. shutil.rmtree(tmp_output_class_dir)
  74. def parse_arguments(argv):
  75. parser = argparse.ArgumentParser()
  76. parser.add_argument('input_dir', type=str, help='Directory with unaligned images.')
  77. parser.add_argument('output_dir', type=str, help='Directory with aligned face thumbnails.')
  78. parser.add_argument('funnel_dir', type=str, help='Directory containing the funnelReal binary and the people.train model file')
  79. parser.add_argument('--image_size', type=int,
  80. help='Image size (height, width) in pixels.', default=110)
  81. parser.add_argument('--face_size', type=int,
  82. help='Size of the face thumbnail (height, width) in pixels.', default=96)
  83. return parser.parse_args(argv)
  84. if __name__ == '__main__':
  85. main(parse_arguments(sys.argv[1:]))