Deteksi wajah (face detection) menggunakan algoritma Viola-Jones


Berikut ini merupakan contoh pemrograman matlab untuk mendeteksi wajah (face detection).

face

faceDetector = vision.CascadeObjectDetector;
I = imread('visionteam.jpg');
bboxes = step(faceDetector, I);
IFaces = insertObjectAnnotation(I, 'rectangle', bboxes, 'Face');
figure, imshow(IFaces), title('Detected faces');

Upper Body detection
upper body

I2 = imread('visionteam.jpg');
bodyDetector = vision.CascadeObjectDetector('UpperBody');
bodyDetector.MinSize = [60 60];
bodyDetector.MergeThreshold = 10;
bboxBody = step(bodyDetector, I2);
IBody = insertObjectAnnotation(I2, 'rectangle',bboxBody,'Upper Body');
figure, imshow(IBody), title('Detected upper bodies');

Koding tersebut menggunakan algoritma viola-jones yang dapat diaplikasikan pada wajah yang menghadap ke depan (frontal face) dan dalam posisi tegak. Proses deteksi wajah dapat ditampilkan dalam bentuk GUI melalui pemrograman berikut:

function varargout = facedetection(varargin)
% FACEDETECTION MATLAB code for facedetection.fig
%      FACEDETECTION, by itself, creates a new FACEDETECTION or raises the existing
%      singleton*.
%
%      H = FACEDETECTION returns the handle to a new FACEDETECTION or the handle to
%      the existing singleton*.
%
%      FACEDETECTION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FACEDETECTION.M with the given input arguments.
%
%      FACEDETECTION('Property','Value',...) creates a new FACEDETECTION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before facedetection_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to facedetection_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help facedetection

% Last Modified by GUIDE v2.5 02-Jan-2014 17:33:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @facedetection_OpeningFcn, ...
    'gui_OutputFcn',  @facedetection_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before facedetection is made visible.
function facedetection_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to facedetection (see VARARGIN)

% Choose default command line output for facedetection
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
movegui(hObject, 'center');
clc
clear

% UIWAIT makes facedetection wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = facedetection_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

reqToolboxes = {'Computer Vision System Toolbox', 'Image Processing Toolbox'};
info = ver;
s=size(info);

flg = zeros(size(reqToolboxes));
reqSize = size(reqToolboxes,2);

for i=1:s(2)
    for j=1:reqSize
        if( strcmpi(info(1,i).Name,reqToolboxes{1,j}) )
            flg(1,j)=1;
        end
    end
end
ret = prod(flg);

if ~ret
    error('detectFaceParts requires: Computer Vision System Toolbox and Image Processing Toolbox. Please install these toolboxes.');
end

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename,pathname]=uigetfile('*.*');
if ~isequal(filename,0)
    handles.data1 = imread(fullfile(pathname,filename));
    guidata(hObject,handles);
    axes(handles.axes1)
    cla reset
    imshow(handles.data1);
else
    return
end

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tic
X = handles.data1;
thresholdFace = 1;
thresholdParts = 1;
stdsize = 176;

nameDetector = {'LeftEye'; 'RightEye'; 'Mouth'; 'Nose'; };
mins = [[12 18]; [12 18]; [15 25]; [15 18]; ];

detector.stdsize = stdsize;
detector.detector = cell(5,1);
for k=1:4
    minSize = int32([stdsize/5 stdsize/5]);
    minSize = [max(minSize(1),mins(k,1)), max(minSize(2),mins(k,2))];
    detector.detector{k} = vision.CascadeObjectDetector(char(nameDetector(k)), 'MergeThreshold', thresholdParts, 'MinSize', minSize);
end

detector.detector{5} = vision.CascadeObjectDetector('FrontalFaceCART', 'MergeThreshold', thresholdFace);

%%%%%%%%%%%%%%%%%%%%%%% detect face %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Detect faces
bbox = step(detector.detector{5}, X);

bbsize = size(bbox);
partsNum = zeros(size(bbox,1),1);

%%%%%%%%%%%%%%%%%%%%%%% detect parts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
stdsize = detector.stdsize;

for k=1:4
    if( k == 1 )
        region = [1,int32(stdsize*2/3); 1, int32(stdsize*2/3)];
    elseif( k == 2 )
        region = [int32(stdsize/3),stdsize; 1, int32(stdsize*2/3)];
    elseif( k == 3 )
        region = [1,stdsize; int32(stdsize/3), stdsize];
    elseif( k == 4 )
        region = [int32(stdsize/5),int32(stdsize*4/5); int32(stdsize/3),stdsize];
    else
        region = [1,stdsize;1,stdsize];
    end
    
    bb = zeros(bbsize);
    for i=1:size(bbox,1)
        XX = X(bbox(i,2):bbox(i,2)+bbox(i,4)-1,bbox(i,1):bbox(i,1)+bbox(i,3)-1,:);
        XX = imresize(XX,[stdsize, stdsize]);
        XX = XX(region(2,1):region(2,2),region(1,1):region(1,2),:);
        
        b = step(detector.detector{k},XX);
        
        if( size(b,1) > 0 )
            partsNum(i) = partsNum(i) + 1;
            
            if( k == 1 )
                b = sortrows(b,1);
            elseif( k == 2 )
                b = flipud(sortrows(b,1));
            elseif( k == 3 )
                b = flipud(sortrows(b,2));
            elseif( k == 4 )
                b = flipud(sortrows(b,3));
            end
            
            ratio = double(bbox(i,3)) / double(stdsize);
            b(1,1) = int32( ( b(1,1)-1 + region(1,1)-1 ) * ratio + 0.5 ) + bbox(i,1);
            b(1,2) = int32( ( b(1,2)-1 + region(2,1)-1 ) * ratio + 0.5 ) + bbox(i,2);
            b(1,3) = int32( b(1,3) * ratio + 0.5 );
            b(1,4) = int32( b(1,4) * ratio + 0.5 );
            
            bb(i,:) = b(1,:);
        end
    end
    bbox = [bbox,bb];
end


%%%%%%%%%%%%%%%%%%%%%%% draw faces %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bbox = [bbox,partsNum];
bbox(partsNum<=2,:)=[];

face =  bbox(:,1: 4);
axes(handles.axes2)
cla reset
imshow(X);
hold on

[m, ~] = size(face);
for j = 1:m
    rectangle('Position',[face(j,1),face(j,2),face(j,3),face(j,4)],'EdgeColor','y','LineWidth',2);
end
hold off
toc
% for k = 1:m
%     imcrop(X,[face(k,1),face(k,2),face(k,3),face(k,4)]);
% end
    
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
img = getframe(gca);
[filename2,pathname2] = uiputfile(...
    {'*.bmp','bitmap image (*.bmp)';
    '*.jpg','jpeg image(*.bmp)';
    '*.*','All file(*.*)'},...
    'Save Image');
if ~isequal(filename2,0)
    imwrite(img.cdata,fullfile(pathname2,filename2));
else
    return
end

Sedangkan tampilan GUI nya adalah:

1. Membuka tampilan GUI awal
1

2. Melakukan deteksi wajah pada frontal face 1
2

3. Melakukan deteksi wajah pada frontal face 2
3

4. Melakukan deteksi wajah pada frontal face 3
4

5. Melakukan deteksi wajah pada frontal face 4
5

6.Melakukan deteksi wajah pada frontal face 5
6

Citra dan source code GUI deteksi wajah dengan algoritma viola-jones dapat diunduh melalui link di bawah ini:
>>Download face detection GUI source code<<

Posted on November 5, 2013, in Pengolahan Citra and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. 24 Comments.

  1. kelihatannya kita memiliki hobi yang mirip🙂 kalau ada waktu berkunjung yah ke blog saya

    http://visikomputer.wordpress.com/

  2. itu maskudnya vision itu apa ya??? sy coba kok “tidak ditemukan variabel vision”

  3. kl mau dapatkan matlab r2013 selain beli dimana ya ?
    heheheeee…. maklum mahasiswa.

  4. tutorialnya bagus mas !! semoga bermanfaat

  5. mas pengen nanya, biar pas pengecekan mata nya kiri dan kanan yg di kotakin gemana ya?
    *mata kiri dan kanan terpisah

    • Dalam computer vision toolbox matlab sdh dilengkapi perintah right eye dan left eye

    • iya sih, cuma yg saya bingung itu cara penulisan biar langsung pengecekan terhadap ke 2 nya sekaligus dalam 1 baris gitu.

      sementara sih penyelesaiannya dengan buat 2 iface 2 bbox dan 2 facedetector. ( jadi jatuhnya lebih panjang codingannya )

  6. Assalamualaikum mas, bisa bantu saya dalam menjelaskan maksud baris yang berada pada bagian detect parts yang ada dalam source code GUI yang mas share ga? Soalnya saya ga ngerti maksud dari semua baris koding yang ada dalam bagian detect parts itu

    • koding di atas, merupakan pengembangan dari koding yang ada pada help matlab
      informasi lebih detail bisa dipelajari pada help matlab dengan mengetikkan kata kunci “vision.CascadeObjectDetector”

  7. Aslamualaikaum
    mas mau tanya mengenai backpropagation mas
    nilai output citra dikenali atau tidak dikenali akan menghasilkan 0.9999
    dimana letak kesalahannya mas
    Mohon bantuannya mas

    • Waalaikumsalam sandri
      kemungkinan kesalahan bisa bermacam2
      di antaranya penyusunan nilai masukan dan nilai target
      jika penyusunan kedua nilai tsb sudah benar maka kesalahan bisa terjadi pada perancangan arsitektur jaringannya

  8. Target saya 0 – 1
    nilai masukan nya, untuk bobot awal dirandom secara acak dan untuk input saya menggunakan vektor hasil ekstraksu ciri
    untuk aesitekturnya saya menggunakan neuron 9900
    karena pada ekstraksi ciri minutiae sidik jari ada 1100 segmen x 9 = 9900
    ubtuk hidden layer nya saya menggunakan 1 hidden mas
    kira2 dimana kesalahannya mas

  9. Assalamualaikum mas, saya tau dari hasil membaca bahwa metode viola jones ini mempunyai 4 kunci utama yaitu fitur haar, integral image, adaboost learning dan cascade classifier, kalau untuk mengetahui nilai dari setiap 4 kunci utama itu gmn ya mas?

    • Maaf mas saya masih belum paham..
      Disini saya saya menggunakan metode viola jones dengan cara menggunakan vision.CascadeObjectDetector, hasil keluaran nya hanyalah citra dengan wajah yang terdeteksi akan diberikan kotak tanda dikenali, nah saya ingin bertanya bagaimana cara nya jika saya ingin mengetahui dan menampilkan nilai fitur haar, integral image, adaboost dan cascade classifier dari citra tersebut?

  10. mas adi permisi saya mau nanya

    caranya gimana ya detect mata kanan dan kiri secara langsung tapi terpisah.
    jadi kaya lefteyecart + righteyecart di 1 gambar

    • secara garis besar, bisa menggunakan kode berikut

      clc;clear;close all;

      LefteyeDetector = vision.CascadeObjectDetector(‘LeftEye’);
      RighteyeDetector = vision.CascadeObjectDetector(‘RightEye’);

      I = imread(‘visionteam.jpg’);
      Leftbboxes = step(LefteyeDetector, I);
      Rightbboxes = step(RighteyeDetector, I);

      Ieye = insertObjectAnnotation(I, ‘rectangle’, Leftbboxes, ‘Left eye’);
      Ieye = insertObjectAnnotation(Ieye, ‘rectangle’, Rightbboxes, ‘Right eye’);
      figure, imshow(Ieye), title(‘Detected faces’);

  11. mas boleh tau alur/penjelasan program yang mulai dari
    tic
    .
    .
    .
    .
    toc

    masih belum paham, terimakasih

  12. koding tersebut merupakan implementasi dari algoritma viola jones di mana pada function perintah yang digunakan sudah dilatihkan citra wajah dan non wajah
    sistem deteksi wajah tsb kemudian di-include-kan dalam computer vision toolbox matlab

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: