您的位置:首页 > 其它

Face++ 制作样本

2014-01-08 22:41 411 查看
%
% Face++ Matlab SDK demo
%

clc; clear; close all;
% input your API_KEY & API_SECRET
API_KEY = 'c937dda9184e051071c15b750af155af';%自己获取的API_KEY和API_SECRET
API_SECRET = '7HsPNLWu81T_yCPdL1ySQFDWEYnFq6Si ';

% If you have chosen Amazon as your API sever and
% changed API_KEY&API_SECRET into yours,
% pls reform the FACEPP call as following :
% api = facepp(API_KEY, API_SECRET, 'US')
api = facepp(API_KEY, API_SECRET);

%遍历文件夹里子文件夹的图片
path='E:\eye_detect_experiments\images\';%主文件夹路径
Folders=dir(fullfile(path,'*.*'));%存放子文件夹名
LengthFolders=length(Folders);%子文件夹的个数
for i=1:LengthFolders
%判断是否为文件夹
if(strcmp(Folders(i).name,'.')|strcmp(Folders(i).name,'..'))
continue;
end

FolderPath=[path Folders(i).name '\'];%这里也可以用strcat()函数

%遍历每幅图像
Files=dir(fullfile(FolderPath,'*.*'));%文件夹下只放图片
FileLength=length(Files);
%对每一副图像进行处理
for j=1:FileLength
if(strcmp(Files(j).name,'.')|strcmp(Files(j).name,'..'))
continue;
end

img=[FolderPath Files(j).name];
in=imread(img);

% Detect faces in the image, obtain related information (faces, img_id, img_height,
% img_width, session_id, url, attributes)
rst = detect_file(api, img, 'all');
img_width = rst{1}.img_width;
img_height = rst{1}.img_height;
face = rst{1}.face;
fprintf('Totally %d faces detected!\n', length(face));
imshow(in);
hold on;
for k= 1 : length(face)
% Draw face rectangle on the image
face_k = face{k};
center = face_k.position.center;
w = face_k.position.width / 100 * img_width;
h = face_k.position.height / 100 * img_height;
rectangle('Position', ...
[center.x * img_width / 100 -  w/2, center.y * img_height / 100 - h/2, w, h], ...
'Curvature', 0.4, 'LineWidth',2, 'EdgeColor', 'blue');

% Detect facial key points
rst2 = api.landmark(face_k.face_id, '83p');
landmark_points = rst2{1}.result{1}.landmark;
landmark_names = fieldnames(landmark_points);

% Draw facial key points
% for j = 1 : length(landmark_names)
%     pt = getfield(landmark_points, landmark_names{j});
%     scatter(pt.x * img_width / 100, pt.y * img_height / 100, 'g.');
% end

%开始寻找眼睛区域
%左眼
left_eye_left_corner=getfield(landmark_points,'left_eye_left_corner');
left_eye_rigth_corner=getfield(landmark_points,'left_eye_right_corner');
left_eye_bottom=getfield(landmark_points,'left_eye_bottom');
left_eye_top=getfield(landmark_points,'left_eye_top');
left_eye_sx=(left_eye_left_corner.x-2)* img_width / 100;
left_eye_sy=(left_eye_top.y-2)* img_height / 100;
left_eye_width=(left_eye_rigth_corner.x-left_eye_left_corner.x+4)* img_width / 100;
left_eye_height=(left_eye_bottom.y-left_eye_top.y+4)* img_height / 100;
rectangle('Position', ...
[left_eye_sx, left_eye_sy, left_eye_width,left_eye_height], ...
'LineWidth',2, 'EdgeColor', 'red');
left_eye_img=in(left_eye_sy:left_eye_sy+left_eye_height,left_eye_sx:left_eye_sx+left_eye_width);
in(left_eye_sy:left_eye_sy+left_eye_height,left_eye_sx:left_eye_sx+left_eye_width)=0;
left_eye_name=['E:\eye_detect_experiments\eyes\'  Files(j).name  num2str(k) '_left.jpg'];
imwrite(left_eye_img,left_eye_name,'jpg');

%右眼
right_eye_left_corner=getfield(landmark_points,'right_eye_left_corner');
right_eye_rigth_corner=getfield(landmark_points,'right_eye_right_corner');
right_eye_bottom=getfield(landmark_points,'right_eye_bottom');
right_eye_top=getfield(landmark_points,'right_eye_top');
right_eye_sx=(right_eye_left_corner.x-2)* img_width / 100;
right_eye_sy=(right_eye_top.y-2)* img_height / 100;
right_eye_width=(right_eye_rigth_corner.x-right_eye_left_corner.x+4)* img_width / 100;
right_eye_height=(right_eye_bottom.y-right_eye_top.y+4)* img_height / 100;
rectangle('Position', ...
[right_eye_sx, right_eye_sy, right_eye_width,right_eye_height], ...
'LineWidth',2, 'EdgeColor', 'red');
right_eye_img=in(right_eye_sy:right_eye_sy+right_eye_height,right_eye_sx:right_eye_sx+right_eye_width);
in(right_eye_sy:right_eye_sy+right_eye_height,right_eye_sx:right_eye_sx+right_eye_width)=0;
right_eye_name=['E:\eye_detect_experiments\eyes\'  Files(j).name  num2str(k) '_right.jpg'];
imwrite(right_eye_img,right_eye_name,'jpg');
end
create_neg_img_name=['E:\eye_detect_experiments\create_neg_sample\' Files(j).name];
imwrite(in,create_neg_img_name,'jpg');
pause;
close all;
end
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: