How To Train your cascade classifier using Matlab...
Training isn't that easy when the subject is complicated, but good teachers deliver it efficiently, just like how Matlab does the thing here..
How does it work.... ?
So Let's start with an existing cascade classifier file and see how it works. 'Frontal Face Detector ' is the one available (and is the default in Matlab) in net. Check out the video to see how it does its job.
And here is the code behind the above video.
%%
% read a video file ,process it and save the
% modified video file
%%
gun_det=vision.CascadeObjectDetector();
vid_reader=vision.VideoFileReader('raj_blog.avi');
vid_writer=VideoWriter('raj_blog_mdfd_1.avi');
open(vid_writer);
while ~isDone(vid_reader)
frame=step(vid_reader);
bbox=step(gun_det,frame);
frame=insertObjectAnnotation(frame,'rectangle',bbox,'Face');
writeVideo(vid_writer,frame);
end
close(vid_writer);
release(vid_reader);
% see the o/p video file in your directory
C++ also do the same job and is flexible in all platforms. Soon i will update the C++ code also...........
Any way it captures the face. What if you wanna capture something of your choice . So you have to develop a classifier and the solution is 'Training '.
So let's start with the procedures for Training.
Requirements / Arguments
I am going to find out the presence of a GUN in my images or how about a Real Time Security System with webcam...............!!!!!
I am going to find out the presence of a GUN in my images or how about a Real Time Security System with webcam...............!!!!!
Matlab command that does this cumbersome process is given below
trainCascadeObjectDetector('findgun_ani_new_lbp5.xml',s,neg_folder,'FalseAlarmRate',0.1,'NumCascadeStages',10,'FeatureType','LBP')
Before proceeding with this, we need to set up its arguments
findgun_ani_new_lbp5.xml: Name of the cascade classifier file
s : 'Structure ' storing the details of Positive Samples (will be explained later)
neg_folder : Path pointing to a folder with Negative Images (Background without the object)
findgun_ani_new_lbp5.xml: Name of the cascade classifier file
s : 'Structure ' storing the details of Positive Samples (will be explained later)
neg_folder : Path pointing to a folder with Negative Images (Background without the object)
FalseAlarmRate :
NumCascadeStages :
FeatureType :
Let's analyse each Arguments
findgun_ani_new_lbp5.xml- Just a Filename. It can be anything. But he is the product of this Training process and he is the one used to identify the desired object in the given image.
s - Is a structure with 2 fields which are 'imageFilename' and 'objectBoundingBoxes' . We define the positive samples in this structure. Positive samples only contain images with object of interest.
>> s=struct('imageFilename',{'D:\MATLAB2013\bin\gun_detection\pos_img\g (1).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (2).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (3).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (4).JPG',...
},'objectBoundingBoxes',{[201 301 295 162],[135 162 313 156],...
[140 79 341 180],[115 59 350 177]})
First field refer to the location of positive images in your machine and the second field points to the location of desired object in each image [x y width height]. Getting these values manually is time consuming. But you have got friends and Adobe Photoshop to help.
To have better o/p classifier file, you need to provide hundreds of images with accurate [x y w h].
neg_folder - Path pointing to a folder with Negative Images (Background without the object).
You should 100's of pictures here. Preferably images of background.
>> neg_folder=fullfile(matlabroot, 'toolbox', 'vision','visiondemos', 'neg_img')
FeatureType :
Let's analyse each Arguments
findgun_ani_new_lbp5.xml- Just a Filename. It can be anything. But he is the product of this Training process and he is the one used to identify the desired object in the given image.
s - Is a structure with 2 fields which are 'imageFilename' and 'objectBoundingBoxes' . We define the positive samples in this structure. Positive samples only contain images with object of interest.
>> s=struct('imageFilename',{'D:\MATLAB2013\bin\gun_detection\pos_img\g (1).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (2).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (3).JPG',...
'D:\MATLAB2013\bin\gun_detection\pos_img\g (4).JPG',...
},'objectBoundingBoxes',{[201 301 295 162],[135 162 313 156],...
[140 79 341 180],[115 59 350 177]})
First field refer to the location of positive images in your machine and the second field points to the location of desired object in each image [x y width height]. Getting these values manually is time consuming. But you have got friends and Adobe Photoshop to help.
To have better o/p classifier file, you need to provide hundreds of images with accurate [x y w h].
neg_folder - Path pointing to a folder with Negative Images (Background without the object).
You should 100's of pictures here. Preferably images of background.
>> neg_folder=fullfile(matlabroot, 'toolbox', 'vision','visiondemos', 'neg_img')
Make sure that the folder 'neg_img' is kept in the path 'rootpath\toolbox\vision\visiondemos'
Since the arguments are ready, Let's start the training for developing our cascade classifier.
Here is a small video showing how the things are done...
Just run these three highlighted commands in Matlab Command window..(Don't forget to modify the paths accordingly).
Ones the training process is done you have the '.xml' file (which they call technically as cascade classifier, I am n't sure anyway )..
How To Identify the Desired Object..
You have done with the important part of object detection.. Now you can Identify the object (or multiple occurrence of the same object ) in the given Image or Video.. You can do it in Real Time also.
Check out my Result. My object of interest was a hand gun and see how I figured it out from the Image...
And here is the code behind this Image
gun_det=vision.CascadeObjectDetector('findgun_lbp14.xml')
i=imread('D:\MATLAB2013\bin\gun_detection\pos_img\p (143).JPG');
bbox = step(gun_det, i);
detectedImg = insertObjectAnnotation(i, 'rectangle', bbox, 'gun', 'FontSize', 18);
image(detectedImg)
Here I have used a different classifier.. Not the one that i mentioned in the above code.. Because it isn't trained with enough samples to produce a good result...
Will be back....
good work!!! helpfull
ReplyDeleteexcellent work on image processing. keep going.
ReplyDeleteHelpful ....tnx
ReplyDelete