% Visualize the MCT face detections on the Cerf/FIFA data set that were
% used in
%
%   B. Schauerte, and R. Stiefelhagen, "Predicting Human Gaze using 
%   Quaternion DCT Image Signature Saliency and Face Detection," in IEEE
%   Workshop on Applications of Computer Vision (WACV), 2012.
%
% Special thanks go to M. Fischer for providing his implementation of MCT
% face detection. If you wonder about the false positives on the data set,
% I have to say that we usually combine the face detectors with tracking in
% order to eliminate such outliers. We did not retrain or optimize the
% detectors (or detector parameters) for the images in the data set.
%
% @author: B. Schauerte
% @date:   2011-2012
% @url:    http://cvhci.anthropomatik.kit.edu/~bschauer/

if ispc
  path_dataset='C:\Users\Boris\Desktop\data\fifadb-face-detections\';
else
  path_dataset='/home/bschauer/data/saliency-faces/fifadb/';
end
sub_path_images='./';%'images/'; % the images
sub_path_fixdata='./';%'metadata/'; % (raw/binary) eye fixation data

imgList=dir([path_dataset sub_path_images '*.tif']);
num_images = numel(imgList);

do_save_demo_images=false;
do_save_demo_maps=false;
demo_path='C:\Users\Boris\Desktop\wacv2012-face-detections\img\';

for j=1:num_images
  % create original image filename and load image
  image_filename=sprintf('%s%s',[path_dataset sub_path_images],imgList(j).name);
  if exist(image_filename,'file')
    image=imread(image_filename);
  else
    warning(['Cannot open image ' image_filename '. Skipping.']);
    continue;
  end
  
  % load the face detection
  img_ending='.tif';
  anno_ending='.png.txt';
  anno_filename=strrep(image_filename,img_ending,anno_ending);
  face_detections=load(anno_filename)';
  
  % create a Gaussian face saliency map (w/o scaling factors)
  S=zeros(size(image,1),size(image,2));
  x = ones(size(image,1),1)*[1:size(image,2)];
  y = [1:size(image,1)]'*ones(1,size(image,2));
  width_factor=1; % @note: change this to scale the face (model/ellipse) width
  height_factor=1; % @note: change this to scale the face (model/ellipse) height
  for i = 1:size(face_detections,2)
    % calculate center of the box
    x0 = round(face_detections(1,i) + (0.5 * face_detections(3,i)));
    y0 = round(face_detections(2,i) + (0.5 * face_detections(4,i)));
    
    % if circular model, calculate the radius (according to the approach by Cerf et al.)
    %r0 = round((face_detections(4,i) + face_detections(3,i)) / 4);
    
    % if elliptical model, calculate an angle/orientation
    if size(face_detections,1) >= 5
      a0 = face_detections(5,i);
    else
      a0 = 0;
    end
    w0 = face_detections(3,i) * width_factor; % the width
    h0 = face_detections(4,i) * height_factor; % the height
    
    u  =  cos(a0)*(x-x0) + sin(a0)*(y-y0);
    v  = -sin(a0)*(x-x0) + cos(a0)*(y-y0);
    
    % add Gaussian weight function for the face
    S  = S + ( exp((-(u).^2)/w0^2) .* exp((-(v).^2)/h0^2) );
  end
  
  subplot(1,2,1); imshow(image);
  subplot(1,2,2); imshow(mat2gray(S));
  drawnow;
  
  if do_save_demo_images, imwrite(image,[demo_path strrep(imgList(j).name,img_ending,'_img.png')]); end
  if do_save_demo_maps, imwrite(mat2gray(S),[demo_path strrep(imgList(j).name,img_ending,'_map.png')]); end
end