2017-11-23 17:44:27 +00:00
|
|
|
% A demo script that demonstrates how to process a single video file using
|
|
|
|
% OpenFace and extract and visualize all of the features
|
|
|
|
|
2017-01-04 22:32:38 +00:00
|
|
|
clear
|
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% The location executable will depend on the OS
|
2017-01-04 22:32:38 +00:00
|
|
|
if(isunix)
|
|
|
|
executable = '"../../build/bin/FeatureExtraction"';
|
|
|
|
else
|
|
|
|
executable = '"../../x64/Release/FeatureExtraction.exe"';
|
|
|
|
end
|
2016-04-28 19:40:36 +00:00
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% Input file
|
|
|
|
in_dir = '../../image_sequence';
|
2016-04-28 19:40:36 +00:00
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% Where to store the output
|
|
|
|
output_dir = './processed_features/';
|
2016-04-28 19:40:36 +00:00
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% This will take directory after -fdir and output all the features to directory
|
|
|
|
% after -out_dir
|
|
|
|
command = sprintf('%s -fdir "%s" -out_dir "%s" -verbose', executable, in_dir, output_dir);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
2017-01-04 22:32:38 +00:00
|
|
|
if(isunix)
|
|
|
|
unix(command);
|
|
|
|
else
|
|
|
|
dos(command);
|
|
|
|
end
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
%% Demonstrating reading the output files
|
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% Most of the features will be in the csv file in the output directory with
|
|
|
|
% the same name as the input file
|
2017-11-23 18:33:10 +00:00
|
|
|
[~,name,~] = fileparts(in_dir);
|
2017-11-23 17:44:27 +00:00
|
|
|
output_csv = sprintf('%s/%s.csv', output_dir, name);
|
|
|
|
|
|
|
|
% First read in the column names, to know which columns to read for
|
|
|
|
% particular features
|
|
|
|
tab = readtable(output_csv);
|
2016-04-28 19:40:36 +00:00
|
|
|
column_names = tab.Properties.VariableNames;
|
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
% Read all of the data
|
|
|
|
all_params = dlmread(output_csv, ',', 1, 0);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
% This indicates which frames were succesfully tracked
|
2017-11-23 17:44:27 +00:00
|
|
|
|
|
|
|
% Find which column contains success of tracking data and timestamp data
|
|
|
|
valid_ind = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'success'));
|
2017-11-23 18:33:10 +00:00
|
|
|
frame_ind = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'frame'));
|
2017-11-23 17:44:27 +00:00
|
|
|
|
|
|
|
% Extract tracking success data and only read those frame
|
|
|
|
valid_frames = logical(all_params(:,valid_ind));
|
|
|
|
|
|
|
|
% Get the timestamp data
|
2017-11-23 18:33:10 +00:00
|
|
|
frame_nums = all_params(valid_frames, frame_ind);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
%% Finding which header line starts with p_ (basically model params)
|
|
|
|
shape_inds = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'p_'));
|
|
|
|
|
|
|
|
% Output rigid (first 6) and non-rigid shape parameters
|
|
|
|
shape_params = all_params(valid_frames, shape_inds);
|
|
|
|
|
|
|
|
figure
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, shape_params);
|
2016-04-28 19:40:36 +00:00
|
|
|
title('Shape parameters');
|
2017-11-23 18:33:10 +00:00
|
|
|
xlabel('Frame');
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
%% Demonstrate 2D landmarks
|
|
|
|
landmark_inds_x = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'x_'));
|
|
|
|
landmark_inds_y = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'y_'));
|
|
|
|
|
|
|
|
xs = all_params(valid_frames, landmark_inds_x);
|
|
|
|
ys = all_params(valid_frames, landmark_inds_y);
|
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
eye_landmark_inds_x = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'eye_lmk_x_'));
|
|
|
|
eye_landmark_inds_y = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'eye_lmk_y_'));
|
|
|
|
|
|
|
|
eye_xs = all_params(valid_frames, eye_landmark_inds_x);
|
|
|
|
eye_ys = all_params(valid_frames, eye_landmark_inds_y);
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
figure
|
|
|
|
|
|
|
|
for j = 1:size(xs,1)
|
|
|
|
plot(xs(j,:), -ys(j,:), '.');
|
2017-11-23 17:44:27 +00:00
|
|
|
hold on;
|
|
|
|
plot(eye_xs(j,:), -eye_ys(j,:), '.r');
|
|
|
|
hold off;
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
xlim([min(xs(1,:)) * 0.5, max(xs(2,:))*1.4]);
|
|
|
|
ylim([min(-ys(1,:)) * 1.4, max(-ys(2,:))*0.5]);
|
|
|
|
xlabel('x (px)');
|
|
|
|
ylabel('y (px)');
|
|
|
|
drawnow
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
%% Demonstrate 3D landmarks
|
|
|
|
landmark_inds_x = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'X_'));
|
|
|
|
landmark_inds_y = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'Y_'));
|
|
|
|
landmark_inds_z = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'Z_'));
|
|
|
|
|
|
|
|
xs = all_params(valid_frames, landmark_inds_x);
|
|
|
|
ys = all_params(valid_frames, landmark_inds_y);
|
|
|
|
zs = all_params(valid_frames, landmark_inds_z);
|
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
eye_landmark_inds_x = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'eye_lmk_X_'));
|
|
|
|
eye_landmark_inds_y = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'eye_lmk_Y_'));
|
|
|
|
eye_landmark_inds_z = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'eye_lmk_Z_'));
|
|
|
|
|
|
|
|
eye_xs = all_params(valid_frames, eye_landmark_inds_x);
|
|
|
|
eye_ys = all_params(valid_frames, eye_landmark_inds_y);
|
|
|
|
eye_zs = all_params(valid_frames, eye_landmark_inds_z);
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
figure
|
|
|
|
for j = 1:size(xs,1)
|
|
|
|
plot3(xs(j,:), ys(j,:), zs(j,:), '.');axis equal;
|
2017-11-23 17:44:27 +00:00
|
|
|
hold on;
|
|
|
|
plot3(eye_xs(j,:), eye_ys(j,:), eye_zs(j,:), '.r');
|
|
|
|
hold off;
|
2016-04-28 19:40:36 +00:00
|
|
|
xlabel('X (mm)');
|
|
|
|
ylabel('Y (mm)');
|
|
|
|
zlabel('Z (mm)');
|
|
|
|
drawnow
|
|
|
|
end
|
|
|
|
|
|
|
|
%% Demonstrate AUs
|
|
|
|
au_reg_inds = cellfun(@(x) ~isempty(x) && x==5, strfind(column_names, '_r'));
|
|
|
|
|
|
|
|
aus = all_params(valid_frames, au_reg_inds);
|
|
|
|
figure
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, aus);
|
2016-04-28 19:40:36 +00:00
|
|
|
title('Facial Action Units (intensity)');
|
|
|
|
xlabel('Time (s)');
|
|
|
|
ylabel('Intensity');
|
|
|
|
ylim([0,6]);
|
|
|
|
|
|
|
|
au_class_inds = cellfun(@(x) ~isempty(x) && x==5, strfind(column_names, '_c'));
|
|
|
|
|
|
|
|
aus = all_params(valid_frames, au_class_inds);
|
|
|
|
figure
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, aus);
|
2016-04-28 19:40:36 +00:00
|
|
|
title('Facial Action Units (presense)');
|
|
|
|
xlabel('Time (s)');
|
|
|
|
ylim([0,2]);
|
|
|
|
%% Demo pose
|
|
|
|
pose_inds = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'pose_'));
|
|
|
|
|
|
|
|
pose = all_params(valid_frames, pose_inds);
|
|
|
|
figure
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, pose);
|
2016-04-28 19:40:36 +00:00
|
|
|
title('Pose (rotation and translation)');
|
2017-11-23 18:33:10 +00:00
|
|
|
xlabel('Frame number');
|
2016-04-28 19:40:36 +00:00
|
|
|
|
2017-11-23 17:44:27 +00:00
|
|
|
%% Demo gaze
|
|
|
|
gaze_inds = cellfun(@(x) ~isempty(x) && x==1, strfind(column_names, 'gaze_angle'));
|
|
|
|
|
|
|
|
% Read gaze (x,y,z) for one eye and (x,y,z) for another
|
|
|
|
gaze = all_params(valid_frames, gaze_inds);
|
|
|
|
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, gaze(:,1), 'DisplayName', 'Left - right');
|
2017-11-23 17:44:27 +00:00
|
|
|
hold on;
|
2017-11-23 18:33:10 +00:00
|
|
|
plot(frame_nums, gaze(:,2), 'DisplayName', 'Up - down');
|
|
|
|
xlabel('Frame number') % x-axis label
|
2017-11-23 17:44:27 +00:00
|
|
|
ylabel('Angle radians') % y-axis label
|
|
|
|
legend('show');
|
|
|
|
hold off;
|
|
|
|
|
2016-04-28 19:40:36 +00:00
|
|
|
%% Output HOG files
|
2017-11-23 17:44:27 +00:00
|
|
|
output_hog_file = sprintf('%s/%s.hog', output_dir, name);
|
|
|
|
[hog_data, valid_inds] = Read_HOG_file(output_hog_file);
|
2016-04-28 19:40:36 +00:00
|
|
|
|
|
|
|
%% Output aligned images
|
2017-11-23 17:44:27 +00:00
|
|
|
output_aligned_dir = sprintf('%s/%s_aligned/', output_dir, name);
|
|
|
|
img_files = dir([output_aligned_dir, '/*.bmp']);
|
2016-04-28 19:40:36 +00:00
|
|
|
imgs = cell(numel(img_files, 1));
|
|
|
|
for i=1:numel(img_files)
|
2017-11-23 17:44:27 +00:00
|
|
|
imgs{i} = imread([ output_aligned_dir, '/', img_files(i).name]);
|
2016-04-28 19:40:36 +00:00
|
|
|
imshow(imgs{i})
|
|
|
|
drawnow
|
|
|
|
end
|