Face++ 制作样本

%
% 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
    
    
        







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值