align_dataset.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # MIT License
  2. #
  3. # Copyright (c) 2016 David Sandberg
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. % LFW
  23. % source_path = '/home/david/datasets/lfw/raw';
  24. % target_path = '/home/david/datasets/lfw/lfw_mtcnnalign_160';
  25. % image_size = 160 + 0;
  26. % margin = round(image_size*0.2) + 0;
  27. % FaceScrub
  28. % source_path = '/home/david/datasets/facescrub/facescrub/';
  29. % target_path = '/home/david/datasets/facescrub/facescrub_mtcnnalign_182_160';
  30. % failed_images_list = '/home/david/datasets/facescrub/facescrub_mtcnnalign_182_160/failed_images.txt';
  31. % image_size = 160 + 12;
  32. % margin = round(image_size*0.2) + 12;
  33. source_path = '/home/david/datasets/casia/CASIA-maxpy-clean/';
  34. target_path = '/home/david/datasets/casia/casia_maxpy_mtcnnalign_182_160';
  35. failed_images_list = '/home/david/datasets/casia/casia_maxpy_mtcnnalign_182_160/failed_images.txt';
  36. image_size = 160 + 12;
  37. margin = round(image_size*0.2) + 12;
  38. image_extension = 'png';
  39. minsize=20; %minimum size of face
  40. use_new = 0;
  41. caffe_path='/home/david/repo2/caffe/matlab';
  42. pdollar_toolbox_path='/home/david/repo2/toolbox';
  43. if use_new
  44. caffe_model_path='/home/david/repo2/MTCNN_face_detection_alignment/code/codes/MTCNNv2/model';
  45. else
  46. caffe_model_path='/home/david/repo2/MTCNN_face_detection_alignment/code/codes/MTCNNv1/model';
  47. end;
  48. addpath(genpath(caffe_path));
  49. addpath(genpath(pdollar_toolbox_path));
  50. caffe.set_mode_gpu();
  51. caffe.set_device(0);
  52. %three steps's threshold
  53. threshold=[0.6 0.7 0.7];
  54. %scale factor
  55. factor=0.709;
  56. %load caffe models
  57. if use_new
  58. prototxt_dir = strcat(caffe_model_path,'/det4.prototxt');
  59. model_dir = strcat(caffe_model_path,'/det4.caffemodel');
  60. end;
  61. %faces=cell(0);
  62. k = 0;
  63. classes = dir(source_path);
  64. %classes = classes(randperm(length(classes)));
  65. for i=1:length(classes),
  66. if classes(i).name(1)~='.'
  67. source_class_path = sprintf('%s/%s', source_path, classes(i).name);
  68. target_class_path = sprintf('%s/%s', target_path, classes(i).name);
  69. imgs = dir(source_class_path);
  70. %imgs = imgs(randperm(length(imgs)));
  71. if ~exist(target_class_path, 'dir'),
  72. mkdir(target_class_path);
  73. end;
  74. for j=1:length(imgs),
  75. if imgs(j).isdir==0
  76. [pathstr,name,ext] = fileparts(imgs(j).name);
  77. target_img_path = sprintf('%s/%s.%s', target_class_path, name, image_extension);
  78. if ~exist(target_img_path,'file') && any([ strcmpi(ext,'.jpg') strcmpi(ext,'.jpeg') strcmpi(ext,'.png') strcmpi(ext,'.gif') ])
  79. if mod(k,1000)==0
  80. fprintf('Resetting GPU\n');
  81. caffe.reset_all();
  82. caffe.set_mode_gpu();
  83. caffe.set_device(0);
  84. prototxt_dir = strcat(caffe_model_path,'/det1.prototxt');
  85. model_dir = strcat(caffe_model_path,'/det1.caffemodel');
  86. PNet=caffe.Net(prototxt_dir,model_dir,'test');
  87. prototxt_dir = strcat(caffe_model_path,'/det2.prototxt');
  88. model_dir = strcat(caffe_model_path,'/det2.caffemodel');
  89. RNet=caffe.Net(prototxt_dir,model_dir,'test');
  90. prototxt_dir = strcat(caffe_model_path,'/det3.prototxt');
  91. model_dir = strcat(caffe_model_path,'/det3.caffemodel');
  92. ONet=caffe.Net(prototxt_dir,model_dir,'test');
  93. if use_new
  94. prototxt_dir = strcat(caffe_model_path,'/det4.prototxt');
  95. model_dir = strcat(caffe_model_path,'/det4.caffemodel');
  96. LNet=caffe.Net(prototxt_dir,model_dir,'test');
  97. end;
  98. end;
  99. source_img_path = sprintf('%s/%s', source_class_path, imgs(j).name);
  100. % source_img_path = '/home/david/datasets/facescrub/facescrub//Billy_Zane/095f83fefdf1dc493c013edb1ef860001193e8d9.jpg'
  101. try
  102. img = imread(source_img_path);
  103. catch exception
  104. fprintf('Unexpected error (%s): %s\n', exception.identifier, exception.message);
  105. continue;
  106. end;
  107. fprintf('%6d: %s\n', k, source_img_path);
  108. if length(size(img))<3
  109. img = repmat(img,[1,1,3]);
  110. end;
  111. img_size = size(img); % [height, width, channels]
  112. img_size = fliplr(img_size(1:2)); % [x,y]
  113. if use_new
  114. [boundingboxes, points]=detect_face_v2(img,minsize,PNet,RNet,ONet,LNet,threshold,false,factor);
  115. else
  116. [boundingboxes, points]=detect_face_v1(img,minsize,PNet,RNet,ONet,threshold,false,factor);
  117. end;
  118. nrof_faces = size(boundingboxes,1);
  119. det = boundingboxes;
  120. if nrof_faces>0
  121. if nrof_faces>1
  122. % select the faces with the largest bounding box
  123. % closest to the image center
  124. bounding_box_size = (det(:,3)-det(:,1)).*(det(:,4)-det(:,2));
  125. img_center = img_size / 2;
  126. offsets = [ (det(:,1)+det(:,3))/2 (det(:,2)+det(:,4))/2 ] - ones(nrof_faces,1)*img_center;
  127. offset_dist_squared = sum(offsets.^2,2);
  128. [a, index] = max(bounding_box_size-offset_dist_squared*2.0); % some extra weight on the centering
  129. det = det(index,:);
  130. points = points(:,index);
  131. end;
  132. % if nrof_faces>0
  133. % figure(1); clf;
  134. % imshow(img);
  135. % hold on;
  136. % plot(points(1:5,1),points(6:10,1),'g.','MarkerSize',10);
  137. % bb = round(det(1:4));
  138. % rectangle('Position',[bb(1) bb(2) bb(3)-bb(1) bb(4)-bb(2)],'LineWidth',2,'LineStyle','-')
  139. % xxx = 1;
  140. % end;
  141. det(1) = max(det(1)-margin/2, 1);
  142. det(2) = max(det(2)-margin/2, 1);
  143. det(3) = min(det(3)+margin/2, img_size(1));
  144. det(4) = min(det(4)+margin/2, img_size(2));
  145. det(1:4) = round(det(1:4));
  146. img = img(det(2):det(4),det(1):det(3),:);
  147. img = imresize(img, [image_size, image_size]);
  148. imwrite(img, target_img_path);
  149. k = k + 1;
  150. else
  151. fprintf('Detection failed: %s\n', source_img_path);
  152. fid = fopen(failed_images_list,'at');
  153. if fid>=0
  154. fprintf(fid, '%s\n', source_img_path);
  155. fclose(fid);
  156. end;
  157. end;
  158. if mod(k,100)==0
  159. xxx = 1;
  160. end;
  161. end;
  162. end;
  163. end;
  164. end;
  165. end;