open source pkg v1

This commit is contained in:
Vijay Yadev
2020-08-04 19:12:31 -04:00
parent bef213dba9
commit c389fc2c47
3708 changed files with 1624220 additions and 1 deletions

View File

@@ -0,0 +1,220 @@
function [images, detections, labels] = Collect_wild_imgs(root_test_data, use_afw, use_lfpw, use_helen, use_ibug)
use_68 = true;
images = [];
labels = [];
detections = [];
if(use_afw)
[img, det, lbl] = Collect_AFW(root_test_data, use_68);
images = cat(1, images, img');
detections = cat(1, detections, det);
labels = cat(1, labels, lbl);
end
if(use_lfpw)
[img, det, lbl] = Collect_LFPW(root_test_data, use_68);
images = cat(1, images, img');
detections = cat(1, detections, det);
labels = cat(1, labels, lbl);
end
if(use_ibug)
[img, det, lbl] = Collect_ibug(root_test_data, use_68);
images = cat(1, images, img');
detections = cat(1, detections, det);
labels = cat(1, labels, lbl);
end
if(use_helen)
[img, det, lbl] = Collect_helen(root_test_data, use_68);
images = cat(1, images, img');
detections = cat(1, detections, det);
labels = cat(1, labels, lbl);
end
% convert to format expected by the Fitting method
detections(:,3) = detections(:,1) + detections(:,3);
detections(:,4) = detections(:,2) + detections(:,4);
end
function [images, detections, labels] = Collect_AFW(root_test_data, use_68)
dataset_loc = [root_test_data, '/AFW/'];
landmarkLabels = dir([dataset_loc '\*.pts']);
num_imgs = size(landmarkLabels,1);
images = struct;
if(use_68)
labels = zeros(num_imgs, 68, 2);
else
labels = zeros(num_imgs, 66, 2);
end
detections = zeros(num_imgs, 4);
load([root_test_data, '/Bounding Boxes/bounding_boxes_afw.mat']);
num_landmarks = 68;
for imgs = 1:num_imgs
[~,name,~] = fileparts(landmarkLabels(imgs).name);
landmarks = dlmread([dataset_loc, landmarkLabels(imgs).name], ' ', [3,0,num_landmarks+2,1]);
if(~use_68)
inds_frontal = [1:60,62:64,66:68];
landmarks = landmarks(inds_frontal,:);
end
images(imgs).img = [dataset_loc, name '.jpg'];
labels(imgs,:,:) = landmarks;
detections(imgs,:) = bounding_boxes{imgs}.bb_detector;
end
detections(:,3) = detections(:,3) - detections(:,1);
detections(:,4) = detections(:,4) - detections(:,2);
end
function [images, detections, labels] = Collect_LFPW(root_test_data, use_68)
dataset_loc = [root_test_data, '/lfpw/testset/'];
landmarkLabels = dir([dataset_loc '\*.pts']);
num_imgs = size(landmarkLabels,1);
images = struct;
if(use_68)
labels = zeros(num_imgs, 68, 2);
else
labels = zeros(num_imgs, 66, 2);
end
detections = zeros(num_imgs, 4);
load([root_test_data, '/Bounding Boxes/bounding_boxes_lfpw_testset.mat']);
num_landmarks = 68;
for imgs = 1:num_imgs
[~,name,~] = fileparts(landmarkLabels(imgs).name);
landmarks = dlmread([dataset_loc, landmarkLabels(imgs).name], ' ', [3,0,num_landmarks+2,1]);
if(~use_68)
inds_frontal = [1:60,62:64,66:68];
landmarks = landmarks(inds_frontal,:);
end
images(imgs).img = [dataset_loc, name '.png'];
labels(imgs,:,:) = landmarks;
detections(imgs,:) = bounding_boxes{imgs}.bb_detector;
end
detections(:,3) = detections(:,3) - detections(:,1);
detections(:,4) = detections(:,4) - detections(:,2);
end
function [images, detections, labels] = Collect_ibug(root_test_data, use_68)
dataset_loc = [root_test_data, '/ibug/'];
landmarkLabels = dir([dataset_loc '\*.pts']);
num_imgs = size(landmarkLabels,1);
images = struct;
if(use_68)
labels = zeros(num_imgs, 68, 2);
else
labels = zeros(num_imgs, 66, 2);
end
detections = zeros(num_imgs, 4);
load([root_test_data, '/Bounding Boxes/bounding_boxes_ibug.mat']);
num_landmarks = 68;
for imgs = 1:num_imgs
[~,name,~] = fileparts(landmarkLabels(imgs).name);
landmarks = dlmread([dataset_loc, landmarkLabels(imgs).name], ' ', [3,0,num_landmarks+2,1]);
if(~use_68)
inds_frontal = [1:60,62:64,66:68];
landmarks = landmarks(inds_frontal,:);
end
images(imgs).img = [dataset_loc, name '.jpg'];
labels(imgs,:,:) = landmarks;
detections(imgs,:) = bounding_boxes{imgs}.bb_detector;
end
detections(:,3) = detections(:,3) - detections(:,1);
detections(:,4) = detections(:,4) - detections(:,2);
end
function [images, detections, labels] = Collect_helen(root_test_data, use_68)
dataset_loc = [root_test_data, '/helen/testset/'];
landmarkLabels = dir([dataset_loc '\*.pts']);
num_imgs = size(landmarkLabels,1);
images = struct;
if(use_68)
labels = zeros(num_imgs, 68, 2);
else
labels = zeros(num_imgs, 66, 2);
end
detections = zeros(num_imgs, 4);
load([root_test_data, '/Bounding Boxes/bounding_boxes_helen_testset.mat']);
num_landmarks = 68;
for imgs = 1:num_imgs
[~,name,~] = fileparts(landmarkLabels(imgs).name);
landmarks = dlmread([dataset_loc, landmarkLabels(imgs).name], ' ', [3,0,num_landmarks+2,1]);
if(~use_68)
inds_frontal = [1:60,62:64,66:68];
landmarks = landmarks(inds_frontal,:);
end
images(imgs).img = [dataset_loc, name '.jpg'];
labels(imgs,:,:) = landmarks;
detections(imgs,:) = bounding_boxes{imgs}.bb_detector;
end
detections(:,3) = detections(:,3) - detections(:,1);
detections(:,4) = detections(:,4) - detections(:,2);
end

View File

@@ -0,0 +1,50 @@
root_test_data = '../../test data/';
[images,dets,gt_labels] = Collect_wild_imgs(root_test_data);
%%
% The offsets and detector bounding boxes
bboxes = zeros(numel(images), 6);
for i=1:numel(images)
image = imread(images(i).img);
[rows, cols, ~] = size(image);
% zone in on a smaller version of the image
zoom_bbox = dets(i,:);
width = zoom_bbox(3) - zoom_bbox(1);
height = zoom_bbox(4) - zoom_bbox(2);
zoom_bbox(1) = zoom_bbox(1) - width/2;
zoom_bbox(2) = zoom_bbox(2) - height/2;
zoom_bbox(3) = zoom_bbox(1) + 2 * width;
zoom_bbox(4) = zoom_bbox(2) + 2 * height;
zoom_bbox(zoom_bbox < 1) = 1;
if(zoom_bbox(3) > cols)
zoom_bbox(3) = cols;
end
if(zoom_bbox(4) > rows)
zoom_bbox(4) = rows;
end
zoom_bbox = round(zoom_bbox);
image_zoom = image(zoom_bbox(2):zoom_bbox(4), zoom_bbox(1):zoom_bbox(3),:);
% The actual face detection
face_detector = vision.CascadeObjectDetector();
bbox = step(face_detector, image_zoom);
if(~isempty(bbox))
bboxes(i,:) = [zoom_bbox(1), zoom_bbox(2), bbox(1), bbox(2), bbox(3), bbox(4)];
end
release(face_detector);
end
save('matlab.mat', 'bboxes', 'dets', 'gt_labels');

View File

@@ -0,0 +1,28 @@
load('menpo_train.mat');
dataset_loc = 'C:\Users\tbaltrus\Documents\menpo_data_orig/';
bboxes_st = bboxes;
% The offsets and detector bounding boxes
dets = zeros(numel(bboxes_st), 4);
bboxes = zeros(numel(bboxes_st), 4);
gt_labels = cell(numel(bboxes_st),1);
%%
for i=1:numel(bboxes_st)
name = bboxes_st(i).name(1:end-4);
landmarks = importdata([dataset_loc, name, '.pts'], ' ', 3);
landmarks = landmarks.data;
bbox_gt = [min(landmarks(:,1)), min(landmarks(:,2)), max(landmarks(:,1)), max(landmarks(:,2))];
bboxes(i,:) = bbox_gt;
gt_labels{i} = landmarks;
% zone in on a smaller version of the image
if(~isempty(bboxes_st(i).bbox))
dets(i,:) = [bboxes_st(i).bbox(1), bboxes_st(i).bbox(2), bboxes_st(i).bbox(3), bboxes_st(i).bbox(4)];
end
end
save('menpo_mtcnn.mat', 'bboxes', 'dets', 'gt_labels');

View File

@@ -0,0 +1,70 @@
root_test_data = '../../test data/';
[~,dets,gt_labels] = Collect_wild_imgs(root_test_data);
%% Analysing the bounding boxes and errors
detection_root = './openCV_haarcascade_frontalface_alt/';
%%
afw_loc = [detection_root, 'out_haar_afw/'];
afw_dets = dir([afw_loc '*.pts']);
bboxes = [];
bboxes = cat(1, bboxes, zeros(numel(afw_dets), 6));
count = 0;
for i=1:numel(afw_dets)
count = count + 1;
bboxes(count,:) = csvread([afw_loc, afw_dets(i).name]);
end
%%
lfpw_loc = [detection_root, 'out_lfpw_trainset/'];
lfpw_dets = dir([lfpw_loc '*.pts']);
bboxes = cat(1, bboxes, zeros(numel(lfpw_dets), 6));
for i=1:numel(lfpw_dets)
count = count + 1;
bboxes(count,:) = csvread([lfpw_loc, lfpw_dets(i).name]);
end
%%
ibug_loc = [detection_root, 'out_ibug/'];
ibug_dets = dir([ibug_loc '*.pts']);
bboxes = cat(1, bboxes, zeros(numel(ibug_dets), 6));
for i=1:numel(ibug_dets)
count = count + 1;
bboxes(count,:) = csvread([ibug_loc, ibug_dets(i).name]);
end
%%
helen_loc = [detection_root, 'out_helen_trainset/'];
helen_dets = dir([helen_loc '*.pts']);
bboxes = cat(1, bboxes, zeros(numel(helen_dets), 6));
for i=1:numel(helen_dets)
count = count + 1;
% f = fopen([afw_loc, afw_dets(i).name]);
bboxes(count,:) = csvread([helen_loc, helen_dets(i).name]);
% fclose(f);
% bboxes(count,:) = bbox;
end
%%
save('ocv.mat', 'bboxes', 'dets', 'gt_labels');

View File

@@ -0,0 +1,33 @@
% load('ocv.mat')
% Move into Matlab space
% bboxes = bboxes + 1;
clear
% Replace this with the location of in 300 faces in the wild data
if(exist([getenv('USERPROFILE') '/Dropbox/AAM/test data/'], 'file'))
root_test_data = [getenv('USERPROFILE') '/Dropbox/AAM/test data/'];
else
root_test_data = 'D:/Dropbox/Dropbox/AAM/test data/';
end
[images, detections, labels] = Collect_wild_imgs(root_test_data, true, true, true, true);
%% some visualisations
% Find the width and height mappings
widths_gt = (max(labels(:,:,1)') - min(labels(:,:,1)'))';
heights_gt = (max(labels(:,:,2)') - min(labels(:,:,2)'))';
widths_det = detections(:,3) - detections(:,1);
heights_det = detections(:,4) - detections(:,2);
s_width = sqrt(std(widths_det ./ widths_gt));
s_height = sqrt(std(heights_det ./ heights_gt));
tx_gt = min(labels(:,:,1)')';
ty_gt = min(labels(:,:,2)')';
tx_det = detections(:,1);
ty_det = detections(:,2);
s_tx = std((tx_gt - tx_det) ./ widths_det);
s_ty = std((ty_gt - ty_det) ./ heights_det);

View File

@@ -0,0 +1,61 @@
% load('ocv.mat')
% Move into Matlab space
% bboxes = bboxes + 1;
load('matlab.mat')
% Find the actual bboxes
bboxes_detector = bboxes(:,3:6);
% add x offset
bboxes_detector(:,1) = bboxes(:,1) + bboxes_detector(:,1);
% add y offset
bboxes_detector(:,2) = bboxes(:,2) + bboxes_detector(:,2);
non_detected = bboxes(:,3) == 1;
% Find the width and height mappings
widths_gt = (max(gt_labels(:,:,1)') - min(gt_labels(:,:,1)'))';
widths_det = bboxes_detector(:,3);
bad_det = abs(1 - widths_gt ./ widths_det) > 0.5;
non_detected = non_detected | bad_det;
% if the width is quite different from detection then it failed
bboxes_detector = bboxes_detector(~non_detected,:);
gt_labels = gt_labels(~non_detected,:,:);
dets = dets(~non_detected,:);
%% some visualisations
% a = 1;
% plot(gt_labels(a,:,1), gt_labels(a,:,2), '.r');
% hold on;
% bbox = bboxes_detector(a,:);
% % bbox(2) = -bbox(2);
% rectangle('Position', bbox);
% hold off;
% axis equal;
% Want to find out what scaling and translation would lead to the smallest
% RMSE error between initialised landmarks and gt landmarks TODO
% Find the width and height mappings
widths_gt = (max(gt_labels(:,:,1)') - min(gt_labels(:,:,1)'))';
heights_gt = (max(gt_labels(:,:,2)') - min(gt_labels(:,:,2)'))';
widths_det = bboxes_detector(:,3);
heights_det = bboxes_detector(:,4);
s_width = widths_det \ widths_gt;
s_height = heights_det \ heights_gt;
tx_gt = min(gt_labels(:,:,1)')';
ty_gt = min(gt_labels(:,:,2)')';
tx_det = bboxes_detector(:,1);
ty_det = bboxes_detector(:,2);
s_tx = mean((tx_gt - tx_det) ./ widths_det);
s_ty = mean((ty_gt - ty_det) ./ heights_det);

View File

@@ -0,0 +1,61 @@
% load('ocv.mat')
% Move into Matlab space
% bboxes = bboxes + 1;
load('matlab.mat')
% Find the actual bboxes
bboxes_detector = bboxes(:,3:6);
% add x offset
bboxes_detector(:,1) = bboxes(:,1) + bboxes_detector(:,1);
% add y offset
bboxes_detector(:,2) = bboxes(:,2) + bboxes_detector(:,2);
non_detected = bboxes(:,3) == 1;
% Find the width and height mappings
widths_gt = (max(gt_labels(:,:,1)') - min(gt_labels(:,:,1)'))';
widths_det = bboxes_detector(:,3);
bad_det = abs(1 - widths_gt ./ widths_det) > 0.5;
non_detected = non_detected | bad_det;
% if the width is quite different from detection then it failed
bboxes_detector = bboxes_detector(~non_detected,:);
gt_labels = gt_labels(~non_detected,:,:);
dets = dets(~non_detected,:);
%% some visualisations
% a = 1;
% plot(gt_labels(a,:,1), gt_labels(a,:,2), '.r');
% hold on;
% bbox = bboxes_detector(a,:);
% % bbox(2) = -bbox(2);
% rectangle('Position', bbox);
% hold off;
% axis equal;
% Want to find out what scaling and translation would lead to the smallest
% RMSE error between initialised landmarks and gt landmarks TODO
% Find the width and height mappings
widths_gt = (max(gt_labels(:,:,1)') - min(gt_labels(:,:,1)'))';
heights_gt = (max(gt_labels(:,:,2)') - min(gt_labels(:,:,2)'))';
widths_det = bboxes_detector(:,3);
heights_det = bboxes_detector(:,4);
s_width = widths_det \ widths_gt;
s_height = heights_det \ heights_gt;
tx_gt = min(gt_labels(:,:,1)')';
ty_gt = min(gt_labels(:,:,2)')';
tx_det = bboxes_detector(:,1);
ty_det = bboxes_detector(:,2);
s_tx = mean((tx_gt - tx_det) ./ widths_det);
s_ty = mean((ty_gt - ty_det) ./ heights_det);

View File

@@ -0,0 +1,85 @@
load('menpo_mtcnn.mat')
% Find the ground truth bboxes
bboxes_gt = bboxes;
bboxes_det = dets;
non_detected = bboxes_det(:,3) == 0;
% Removing the outliers
widths_gt = bboxes_gt(:,3) - bboxes_gt(:,1);
widths_det = bboxes_det(:,3) - bboxes_det(:,1);
heights_gt = bboxes_gt(:,4) - bboxes_gt(:,2);
heights_det = bboxes_det(:,4) - bboxes_det(:,2);
tx_gt = bboxes_gt(:,1);
ty_gt = bboxes_gt(:,2);
tx_det = bboxes_det(:,1);
ty_det = bboxes_det(:,2);
bad_det_1 = abs(1 - widths_gt ./ widths_det) > 0.5;
bad_det_2 = abs(1 - heights_gt ./ heights_det) > 0.5;
bad_det_3 = abs((tx_gt - tx_det) ./ widths_det) > 0.4;
bad_det_4 = abs((ty_gt - ty_det) ./ heights_det) > 0.5;
non_detected = non_detected | bad_det_1 | bad_det_2 | bad_det_3 | bad_det_4;
% if the width is quite different from detection then it failed
bboxes_gt = bboxes_gt(~non_detected,:);
bboxes_det = bboxes_det(~non_detected,:);
%% some visualisations
% a = 1;
% plot(gt_labels(a,:,1), gt_labels(a,:,2), '.r');
% hold on;
% bbox = bboxes_detector(a,:);
% % bbox(2) = -bbox(2);
% rectangle('Position', bbox);
% hold off;
% axis equal;
% Want to find out what scaling and translation would lead to the smallest
% RMSE error between initialised landmarks and gt landmarks TODO
% Find the width and height mappings
widths_gt = bboxes_gt(:,3) - bboxes_gt(:,1);
widths_det = bboxes_det(:,3) - bboxes_det(:,1);
heights_gt = bboxes_gt(:,4) - bboxes_gt(:,2);
heights_det = bboxes_det(:,4) - bboxes_det(:,2);
s_width = widths_det \ widths_gt;
s_height = heights_det \ heights_gt;
tx_gt = bboxes_gt(:,1);
ty_gt = bboxes_gt(:,2);
tx_det = bboxes_det(:,1);
ty_det = bboxes_det(:,2);
s_tx = median((tx_gt - tx_det) ./ widths_det);
s_ty = median((ty_gt - ty_det) ./ heights_det);
%%
new_widths = widths_det * s_width;
new_heights = heights_det * s_height;
new_tx = widths_det * s_tx + tx_det;
new_ty = heights_det * s_ty + ty_det;
overlaps = zeros(numel(widths_det), 1);
new_overlaps = zeros(numel(widths_det), 1);
for i=1:numel(widths_det)
bbox_gt = bboxes_gt(i,:);
bbox_old = bboxes_det(i,:);
overlaps(i) = overlap(bbox_gt, bbox_old);
bbox_new = [new_tx(i), new_ty(i), new_tx(i) + new_widths(i), new_ty(i) + new_heights(i)];
new_overlaps(i) = overlap(bbox_gt, bbox_new);
end
fprintf('Orig - %.3f, now - %.3f\n', mean(overlaps), mean(new_overlaps));

View File

@@ -0,0 +1,140 @@
% load('ocv.mat')
% Move into Matlab space
% bboxes = bboxes + 1;
use_afw = false;
use_lfpw = true;
use_helen = true;
use_ibug = false;
root_test_data = 'F:\Dropbox\Dropbox\AAM\test data/';
% this loads the ground truths
% [gt_images, gt_detections, gt_labels] = Collect_wild_imgs(root_test_data, use_afw, use_lfpw, use_helen, use_ibug);
%% Find the width and height mappings
tlx_gt = min(gt_labels(:,:,1)')' - 1;
tly_gt = min(gt_labels(:,:,2)')' - 1;
blx_gt = max(gt_labels(:,:,1)')' - 1;
bly_gt = max(gt_labels(:,:,2)')' - 1;
bboxes_gt = [tlx_gt, tly_gt, blx_gt, bly_gt];
%% Extract the detector bounding boxes as well
f = fopen('F:\dlib_baseline\helen.txt');
det_file = textscan(f, '%s %f %f %f %f %f');
fclose(f);
f = fopen('F:\dlib_baseline\lfpw.txt');
det_file_2 = textscan(f, '%s %f %f %f %f %f');
fclose(f);
det_images = cat(1, det_file{1}, det_file_2{1});
scores_det = cat(1, det_file{2}, det_file_2{2});
bboxes_det = [det_file{3}, det_file{4}, det_file{5}, det_file{6}];
bboxes_det_2 = [det_file_2{3}, det_file_2{4}, det_file_2{5}, det_file_2{6}];
bboxes_det = cat(1, bboxes_det, bboxes_det_2);
%% Go through ground truth picking out the best detections
bbox_gt_with_tp = [];
bbox_det_tp = [];
scores_tp = [];
overlaps_tp = [];
tps_all = zeros(size(bboxes_det, 1), 1);
for gt_ind = 1:size(bboxes_gt, 1)
[~, fname, ~] = fileparts(gt_images(gt_ind).img);
det_inds = find(strcmp(det_images, fname))';
bbox_gt = bboxes_gt(gt_ind,:);
bbox_det = [];
max_overlap = 0;
max_ind = 0;
for det_ind=det_inds
curr_ov = overlap(bbox_gt, bboxes_det(det_ind,:));
if(curr_ov > max_overlap)
max_overlap = curr_ov;
bbox_det = bboxes_det(det_ind,:);
score = scores_det(det_ind);
max_ind = det_ind;
end
end
if(max_overlap > 0.4)
tps_all(max_ind) = 1;
bbox_gt_with_tp = cat(1, bbox_gt_with_tp, bbox_gt);
bbox_det_tp = cat(1, bbox_det_tp, bbox_det);
scores_tp = cat(1, scores_tp, score);
overlaps_tp = cat(1, overlaps_tp, max_overlap);
end
end
%% Work out the mapping and a suitable threshold, with EER?
%% some visualisations
% Want to find out what scaling and translation would lead to the smallest
% RMSE error between initialised landmarks and gt landmarks TODO
% Find the width and height mappings
widths_gt = bbox_gt_with_tp(:,3) - bbox_gt_with_tp(:,1);
heights_gt = bbox_gt_with_tp(:,4) - bbox_gt_with_tp(:,2);
widths_det = bbox_det_tp(:,3) - bbox_det_tp(:,1);
heights_det = bbox_det_tp(:,4) - bbox_det_tp(:,2);
s_width = widths_det \ widths_gt;
s_height = heights_det \ heights_gt;
tx_gt = bbox_gt_with_tp(:,1);
ty_gt = bbox_gt_with_tp(:,2);
tx_det = bbox_det_tp(:,1);
ty_det = bbox_det_tp(:,2);
s_tx = mean((tx_gt - tx_det) ./ widths_det);
s_ty = mean((ty_gt - ty_det) ./ heights_det);
% newbbox
new_widths = widths_det * s_width;
new_heights = heights_det * s_height;
new_tx = widths_det * s_tx + tx_det;
new_ty = heights_det * s_ty + ty_det;
bbox_det_tp_new = [new_tx, new_ty, new_tx + new_widths, new_ty + new_heights];
new_overlaps = zeros(size(bbox_det_tp_new, 1), 1);
for gt_ind = 1:size(bbox_det_tp_new, 1)
new_overlaps(gt_ind) = overlap(bbox_gt_with_tp(gt_ind,:), bbox_det_tp_new(gt_ind,:));
end
%% Draw an ROC curve
score_bands = min(scores_det):(max(scores_det)-min(scores_det))/50:max(scores_det);
recalls = zeros(numel(score_bands), 1);
precisions = zeros(numel(score_bands), 1);
for i=1:numel(score_bands)
precisions(i) = sum(tps_all(scores_det >= score_bands(i))) / sum(scores_det >= score_bands(i));
recalls(i) = sum(tps_all(scores_det >= score_bands(i))) / size(gt_labels,1);
end
% tp
% bbox_det_new = bbox

View File

@@ -0,0 +1,26 @@
function [ overlap ] = overlap( rect1, rect2 )
%OVERLAP Summary of this function goes here
% Detailed explanation goes here
dy1 = abs(rect1(1) - rect1(3)) + 1;
dx1 = abs(rect1(2) - rect1(4)) + 1;
dy2 = abs(rect2(1) - rect2(3)) + 1;
dx2 = abs(rect2(2) - rect2(4)) + 1;
a1 = dx1 * dy1;
a2 = dx2 * dy2;
ia = 0;
if rect1(3) > rect2(1) && rect2(3) > rect1(1) && rect1(4) > rect2(2) && rect2(4) > rect1(2)
xx1 = max(rect1(2), rect2(2));
yy1 = max(rect1(1), rect2(1));
xx2 = min(rect1(4), rect2(4));
yy2 = min(rect1(3), rect2(3));
ia = (xx2 - xx1 + 1) * (yy2 - yy1 + 1);
end
overlap = ia / double(a1 + a2 - ia);
end

View File

@@ -0,0 +1 @@
Code here was used to learn the mapping from a face detection bounding box to the bounding boxes suitable for the initialisation of landmark detection.

View File

@@ -0,0 +1,73 @@
%% Work out the eye scaling first using the PDM
addpath('../PDM_helpers/');
% also work out a size for each window
% Replace this with the location of in 300 faces in the wild data
if(exist([getenv('USERPROFILE') '/Dropbox/AAM/test data/'], 'file'))
root_test_data = [getenv('USERPROFILE') '/Dropbox/AAM/test data/'];
else
root_test_data = 'F:/Dropbox/Dropbox/AAM/test data/';
end
[~, detections, labels] = Collect_wild_imgs(root_test_data, true, true, true, true);
% load('../models/pdm/pdm_68_multi_pie');
load('../models/pdm/pdm_68_aligned_wild');
%%
shapes = zeros(size(labels));
num_points = 68;
for i=1:size(labels,1)
[ a_orig, R, ~, ~, ~, ~] = fit_PDM_ortho_proj_to_2D(M, E, V, squeeze(labels(i,:,:)));
view_actual = Rot2Euler(R);
views = [0,0,0; 0,-30,0; -30,0,0; 0,30,0; 30,0,0];
views = views * pi/180;
[~,view_id] = min(sum(abs(bsxfun(@plus, views, -view_actual)), 2));
rot = Euler2Rot(views(view_id,:));
rot_m = rot * reshape(M, num_points, 3)';
width_model = max(rot_m(1,:)) - min(rot_m(1,:));
height_model = max(rot_m(2,:)) - min(rot_m(2,:));
bounding_box = detections(i,:);
a = (((bounding_box(3) - bounding_box(1)) / width_model) + ((bounding_box(4) - bounding_box(2))/ height_model)) / 2;
tx = (bounding_box(3) + bounding_box(1))/2;
ty = (bounding_box(4) + bounding_box(2))/2;
% correct it so that the bounding box is just around the minimum
% and maximum point in the initialised face
tx = tx - a*(min(rot_m(1,:)) + max(rot_m(1,:)))/2;
ty = ty - a*(min(rot_m(2,:)) + max(rot_m(2,:)))/2;
% visualisation of the initial state
%hold off;imshow(Image);hold on;plot(a*rot_m(1,:)+tx, a*rot_m(2,:)+ty,'.r');hold on;rectangle('Position', [bounding_box(1), bounding_box(2), bounding_box(3)-bounding_box(1), bounding_box(4)-bounding_box(2)]);
global_params = [a, 0, 0, 0, tx, ty]';
global_params(2:4) = views(view_id);
local_params = zeros(numel(E), 1);
shape = GetShapeOrtho(M, V, local_params, global_params);
shape = shape(:,1:2);
shapes(i,:,:) = shape;
shapes(i,:,:) = shapes(i,:,:) / a_orig;
labels(i,:,:) = labels(i,:,:) / a_orig;
end
%%
errs_img = sort(squeeze(mean(mean(abs(labels - shapes), 2), 3)));
errs_lmk = squeeze(mean(mean(abs(labels - shapes), 1), 3));
errs_img_95 = errs_img(round(0.95*end));
scales = [0.25, 0.35, 0.5];
windows = floor(errs_img_95 * 2 * scales + 11) + 1