Skip to content

Commit

Permalink
add bfm generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
yfeng95 committed Jul 18, 2018
1 parent e79ba9f commit b5a7e59
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 0 deletions.
70 changes: 70 additions & 0 deletions examples/Data/BFM/add_tri_mouth.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
tex = model.texMU/255.;
tex = reshape(tex, 3, length(tex)/3);
tri = model.tri;

uv_coords = model_info.uv_coords';
% modify bad vers
bad_ind = [10032, 10155, 10280];
round1 = [10033, 10158 ];
round2 = [10534, 10157, 10661];
round3 = [10916, 10286];
uv_coords(:, bad_ind(1)) = mean(uv_coords(:, round1), 2);
uv_coords(:, bad_ind(2)) = mean(uv_coords(:, round2), 2);
uv_coords(:, bad_ind(3)) = mean(uv_coords(:, round3), 2);
model_info.uv_coords = uv_coords;

vertices = [uv_coords; zeros(1, size(uv_coords, 2))];

% vertices = model.shapeMU + model.expMU;% + model.expPC*randn(29, 1);
% vertices = reshape(vertices, 3, length(vertices)/3);
% vertices = double(vertices);

figure
tex(:, model.kpt_ind) = 0;

tri_mouth_ind = model_info.segbin_tri(3,:);
tri_mouth = tri(:, boolean(tri_mouth_ind));
patch('Vertices', vertices', 'FaceVertexCData', tex', 'Faces', tri_mouth', 'FaceColor', 'interp', 'EdgeColor', 'none', 'EdgeLighting', 'none', 'LineWidth', 1);

% axis equal;
tm_inner = model.tri_mouth;
tm_inner_add =[6420 6542 6664; %% add inner mouth triangles
6420 6294 6167;
6167 6297 6420;
6167 6297 6296;
6167 6296 6295;
6167 6295 6039;
6168 6295 6039];
ind_bad = 38;
all_ind = 1:size(tm_inner, 2);
tm_inner = tm_inner(:, setdiff(all_ind, bad_ind));
tm_inner = [tm_inner tm_inner_add'];
model_info.tri_mouth = tm_inner;
model.tri_mouth = tm_inner;
save 'me/BFM.mat' model
save 'me/BFM_info.mat' model_info

% show
vm_inner = unique(tm_inner(:));

edgeColor = 'red';
patch('Vertices', vertices', 'Faces', tm_inner', 'FaceColor', 'green', 'EdgeColor', edgeColor, 'EdgeLighting', 'none', 'LineWidth', 1);

tm = tri_mouth;
vm = unique(tm(:));
hold on
plot(vertices(1,vm), vertices(2,vm), 'bx');

plot(vertices(1,bad_ind), vertices(2,bad_ind), 'ro');

plot(vertices(1,vm_inner), vertices(2,vm_inner), 'g.');

for i = 1:length(vm)
ind = vm(i);
text(vertices(1,ind), vertices(2,ind), num2str(ind));
end


% for i = 1:size(model.tri_mouth, 2)
% patch('Vertices', vertices', 'Faces', model.tri_mouth(:,i)', 'FaceColor', 'green', 'EdgeColor', edgeColor, 'EdgeLighting', 'none', 'LineWidth', 1);
% end
97 changes: 97 additions & 0 deletions examples/Data/BFM/generate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
% addpath(genpath(pwd))
% --> model

%% load raw BFM models
load('raw/01_MorphableModel.mat');

%% load 3ddfa data
% 1. load bfm information. trim
load('3ddfa/model_info.mat');
trimIndex_f = [3*trimIndex-2, 3*trimIndex-1, 3*trimIndex]';
trimIndex_f = trimIndex_f(:);

model.shapeMU = shapeMU(trimIndex_f,:);
model.shapePC = shapePC(trimIndex_f, :);
model.shapeEV = shapeEV;
model.texMU = texMU(trimIndex_f, :);
model.texPC = texPC(trimIndex_f, :);
model.texEV = texEV;
model.tri = tri;
model.kpt_ind = keypoints;

model_info.kpt_ind = keypoints;
model_info.trimIndex = trimIndex;
model_info.symlist = symlist;
model_info.symlist_tri = symlist_tri;
%segbin: nose eyes mouth rest
model_info.segbin = segbin(trimIndex, :)';
model_info.segbin_tri = segbin_tri';

% 2. load expression
load('3ddfa/Model_Expression.mat');
model.expMU = mu_exp;
model.expPC = w_exp;
model.expEV = sigma_exp;

% 3. load mouth tri
load('3ddfa/Model_tri_mouth');
model.tri_mouth = tri_mouth;
model_info.tri_mouth = tri_mouth;

% 4. face contour
load('3ddfa/Model_face_contour_trimed.mat');
model_info.face_contour = face_contour;
model_info.face_contour_line = face_contour_line;
model_info.face_contour_front = face_contour_front;
model_info.face_contour_front_line = face_contour_front_line;

% 5. nose hole
load('3ddfa/Modelplus_nose_hole.mat');
model_info.nose_hole = nose_hole;
model_info.nose_hole_right = nose_hole_right;
model_info.nose_hole_left = nose_hole_left;

% 6. parallel for key points
load('3ddfa/Modelplus_parallel')
model_info.parallel = parallel;
model_info.parallel_face_contour = parallel_face_contour;

%% load 3DMMasSTN UV coords
load('stn/BFM_UV.mat');
uv_coords = UV(trimIndex, :)';

% modify bad vers
bad_ind = [10032, 10155, 10280];
round1 = [10033, 10158 ];
round2 = [10534, 10157, 10661];
round3 = [10916, 10286];
uv_coords(:, bad_ind(1)) = mean(uv_coords(:, round1), 2);
uv_coords(:, bad_ind(2)) = mean(uv_coords(:, round2), 2);
uv_coords(:, bad_ind(3)) = mean(uv_coords(:, round3), 2);

model_info.uv_coords = uv_coords';
UV = model_info.uv_coords;

% modify tri mouth
tm_inner = model.tri_mouth;
tm_inner_add =[6420 6542 6664; %% add inner mouth triangles
6420 6294 6167;
6167 6297 6420;
6167 6297 6296;
6167 6296 6295;
6167 6295 6039;
6168 6295 6039];
ind_bad = 38;
all_ind = 1:size(tm_inner, 2);
tm_inner = tm_inner(:, setdiff(all_ind, bad_ind));
tm_inner = [tm_inner tm_inner_add'];
model_info.tri_mouth = tm_inner;
model.tri_mouth = tm_inner;

%% show
clearvars -except model model_info UV

save 'Out/BFM.mat' model
save 'Out/BFM_info.mat' model_info
save 'Out/BFM_UV.mat' UV
copyfile('3ddfa/pncc_code.mat', 'Out/pncc_code.mat')
68 changes: 68 additions & 0 deletions examples/Data/BFM/show.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
addpath(genpath(pwd))
load 'Out/BFM.mat'
load 'Out/BFM_info.mat'

% test model
vertices = model.shapeMU + model.expMU;
vertices = reshape(vertices, 3, length(vertices)/3);
vertices = double(vertices);
tex = model.texMU/255.;
tex = reshape(tex, 3, length(tex)/3);
tri = model.tri;

% keypoints
% tex(:, model_info.parallel{2}) = 0;
% tex(:, face_contour) = 0;
% tex(2, face_contour) = 1;

% show
figure
subplot(221)
tri = [tri model.tri_mouth];
patch('Vertices', vertices', 'FaceVertexCData', tex', 'Faces', tri', 'FaceColor', 'interp', 'EdgeColor', 'none', 'EdgeLighting', 'none', 'LineWidth', 1);

% mouth triangles
% edgeColor = 'red';
% patch('Vertices', vertices', 'Faces', model.tri_mouth', 'FaceColor', 'green', 'EdgeColor', edgeColor, 'EdgeLighting', 'none', 'LineWidth', 1);

% % stitch triangles
% edgeColor = 'yellow';
% patch('Vertices', vertices', 'Faces', trif_stitch', 'FaceColor', 'none', 'EdgeColor', edgeColor, 'EdgeLighting', 'none', 'LineWidth', 1);
axis equal;

%% project
s = 128;
uv_coords = model_info.uv_coords * (s -1) + 1;
uv_vertices = [uv_coords; zeros(1, size(uv_coords, 2))];
subplot(222);
patch('Vertices', uv_vertices', 'FaceVertexCData', tex', 'Faces', tri', 'FaceColor', 'interp', 'EdgeColor', 'none', 'EdgeLighting', 'none', 'LineWidth', 1);
% patch('Vertices', uv_vertices', 'Faces', model.tri_mouth', 'FaceColor', 'green', 'EdgeColor', edgeColor, 'EdgeLighting', 'none', 'LineWidth', 1);

hold on

plot(uv_vertices(1, model.kpt_ind), uv_vertices(2, model.kpt_ind), 'bx');
plot(uv_vertices(1, model_info.face_contour_front), uv_vertices(2, model_info.face_contour_front), 'g.');
plot(uv_vertices(1, model_info.parallel{1}), uv_vertices(2, model_info.parallel{1}), 'k.');

axis equal;


%% segbin
ver1_nose = vertices(:, boolean(model_info.segbin(1,:)));
ver2_eye = vertices(:, boolean(model_info.segbin(2,:)));
ver3_mouth = vertices(:, boolean(model_info.segbin(3,:)));
ver4_rest = vertices(:, boolean(model_info.segbin(4,:)));

subplot(223)
plot(vertices(1,:), vertices(2,:), 'k.');
hold on
plot(ver1_nose(1,:), ver1_nose(2,:), 'r.');
hold on
plot(ver2_eye(1,:), ver2_eye(2,:), 'b.');
hold on
plot(ver3_mouth(1,:), ver3_mouth(2,:), 'g.');
hold on
plot(ver4_rest(1,:), ver4_rest(2,:), 'm.');



0 comments on commit b5a7e59

Please sign in to comment.