%% Train a Stop Sign Detector Using an ACF Object Detector
% Use the trainACFObjectDetector with training images to create an ACF object detector that can detect stop signs. Test the detector with a separate image.
% Load the training data.

load('stopSignsAndCars.mat')

%
% Select the ground truth for stop signs. These ground truth is the set of known locations of stop signs in the images.

stopSigns = stopSignsAndCars(:,1:2);

%
% Add the full path to the image files.

stopSigns.imageFilename = fullfile(toolboxdir('vision'),...
    'visiondata',stopSigns.imageFilename);

%
% Train the ACF detector. You can turn off the training progress output by specifying 'Verbose',false as a Name,Value pair.

acfDetector = trainACFObjectDetector(stopSigns,'NegativeSamplesFactor',2);

%
% Test the ACF detector on a test image.

img = imread('stopSignTest.jpg');

[bboxes,scores] = detect(acfDetector,img);

%
% Display the detection results and insert the bounding boxes for objects into the image.

for i = 1:length(scores)
   annotation = sprintf('Confidence = %.1f',scores(i));
   img = insertObjectAnnotation(img,'rectangle',bboxes(i,:),annotation);
end

figure
imshow(img)

% Copyright 2012 The MathWorks, Inc.