% Perf=OCR_test(P)
% 
% Tests the function named OCR on the images found in directory P. The
% directory must not contain other files than valid image files.
% The performance is returned in the struct Perf. Perf.corr gives the
% probability for a correct classification.
% Perf.zero gives the probability for a correct classification of the
% number zero and so on
%
% The performance for each number is plotted with a bar plot, each bar
% showing the probability of correctly classifying that particular number

function Perf=OCR_test(P)


PRINT=1; %Set PRINT=0 if you do not want to see every image with result
if PRINT
    
    h=gcf;
end

Perf=struct('NumEval',0','corr',0,'zero',0,'one',0,'two',0,'three',0,'four',0,'five',0,'six',0,'seven',0,'eight',0,'nine',0);

D=dir(P);
D=D(3:end);
n=length(D);
for i=1:n
    warning off
    disp(sprintf('Testing image %d out of %d. Current performance: %3.3g',i,n,Perf.corr/Perf.NumEval))
    warning on
    [Image,Facit]=ReadNumberImage(P,D(i).name);
    if PRINT
        figure(h)
        imshow(Image,[]);
    end
    try
        s=OCR(Image); 
    catch
        if strncmp(lasterr,'Undefined function',18)
            disp('-------------------------------------------------------------')
            disp('In order to run the OCR_test function YOU must supply')
            disp('the OCR function according to the specification given.')
            disp('The OCR.m file must be stored in a directory used by MatLab.')
            disp('-------------------------------------------------------------')
            Perf=[];
            return
        end
        if strncmp(lasterr,'Input argument',14)
            disp('-------------------------------------------------')
            disp('The OCR function must have the following syntax:')
            disp('     s=OCR(I)')
            disp('where I is an image and s is a string')
            disp('NO OTHER SYNTAX WILL BE ALLOWED!!!')
            disp('-------------------------------------------------')
            Perf=[];
            return
        else
            rethrow(lasterror)
        end
    end
    if PRINT
        title(sprintf('Your interpretation <%s>',s))
        drawnow
    end
    p=EvalPerf(s,Facit);
    Perf=UpDatePerf(p,Perf);
   
end
if isempty(get(0,'children'))
    h=gcf;  
else
    figure(h+1)
end
plotPerf(Perf)



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotPerf(perf)
s='0123456789';
bar([perf.zero/perf.NumEval perf.one/perf.NumEval perf.two/perf.NumEval perf.three/perf.NumEval perf.four/perf.NumEval perf.five/perf.NumEval perf.six/perf.NumEval perf.seven/perf.NumEval perf.eight/perf.NumEval perf.nine/perf.NumEval] )
set(gca,'XtickLabel',s');

title(sprintf('Overall performance %3.3g', perf.corr/perf.NumEval))
xlabel('Number')
ylabel('Probability of correct classification')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function p=EvalPerf(s,Facit)

numbers=zeros(1,10);
j=0;
for i=1:length(Facit)
    nF=str2num(Facit(i));
    ns=str2num(s(min(i,length(s))));
    if ~isempty(nF) && ~isempty(ns) && (nF==ns)
        numbers(nF+1)=numbers(nF+1)+1;
    end
    if ~isempty(nF)
        j=j+1;
    end
end

p=struct('NumEval',j,'corr',sum(numbers),'zero',numbers(1),'one',numbers(2),'two',numbers(3),'three',numbers(4),'four',numbers(5),'five',numbers(6),'six',numbers(7),'seven',numbers(8),'eight',numbers(9),'nine',numbers(10));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function Perf=UpDatePerf(p,Perf)

Perf=struct('NumEval',Perf.NumEval+p.NumEval,'corr',Perf.corr+p.corr,'zero',Perf.zero+p.zero,'one',Perf.one+p.one,'two',Perf.two+p.two,'three',Perf.three+p.three,'four',Perf.four+p.four,'five',Perf.five+p.five,'six',Perf.six+p.six,'seven',Perf.seven+p.seven,'eight',Perf.eight+p.eight,'nine',Perf.nine+p.nine);
