%% Main file for VOC tests

clear all;close all;clc

addpath_recurse('../voc-release5')
addpath LBP
addpath bin
addpath LBP/disCLBP
addpath(genpath('../../mexopencv/'))

try
%     matlabpool close
    matlabpool open 4
catch
end


%%
% clear and close everything
clc

addpath '/home/jorge/Escritorio/kitti/devkit/matlab';


disp('======= KITTI Dataset test =======');

root_dir  = '/home/jorge/Escritorio/kitti/2012_object';
train_dir = fullfile(root_dir,'/training-testing/label_02/data');
test_dir  = fullfile(root_dir,'/training-testing/test_hog4d_2'); % location of your testing dir
images_dir = fullfile(root_dir,'/training-testing/image_02/data');
images_disp_dir = fullfile(root_dir,'/training-testing/image_disparity_filestorage/data');

test_set_start = 3741;
test_set_end = 4000;

load models/Pedestrian_mix
% load models/Pedestrian_final

t_start = tic;
% count = 0;
parfor n=test_set_start:test_set_end
% for n=test_set_start:test_set_end %(3.3sec -> 3.42h) (5.0sec (4p) ->
% 1.30h)
    
    fprintf('processing n: %d (%d-%d)\n',n,test_set_start,test_set_end);
    t1=tic;
    
    im = imread(sprintf('%s/%06d.png',images_dir,n));
    im = im2double(im);
    im = color(im);

    S=cv.FileStorage(sprintf('%s/%06d.yml',images_disp_dir,n));
    im_disp = double(S.disparity_image);
    im_disp = repmat(im_disp,[1 1 3]);
        
    [bbox, fbbox] = process(im, im_disp, model, -2.0);
    
    test_objects=[];
    
    for det=1:size(bbox,1)
        test_objects(det).type = model.class;
        test_objects(det).x1 = bbox(det,1);
        test_objects(det).y1 = bbox(det,2);
        test_objects(det).x2 = bbox(det,3);
        test_objects(det).y2 = bbox(det,4);
        test_objects(det).alpha = pi/2;
        test_objects(det).score = bbox(det,6);
    end

    writeLabels(test_objects,test_dir,n);
    
    dt=toc(t1);
    fprintf('done in %.1f sec\n',dt);
    
%     count=count+1;
%     
%     if ~mod(floor(toc(t_start)),5)
%         total_time = dt*((test_set_end-test_set_start)-count);
%         hours = floor(total_time/(60*60));
%         minutes = floor(total_time/60) - hours*60;
%         seconds = floor(total_time) - hours*60*60 - minutes*60;
% 
%         fprintf('ETA: %d hours %d minutes %d seconds\n',hours,minutes,seconds);
%     end
end

total_time = toc(t_start);
hours = floor(total_time/(60*60));
minutes = floor(total_time/60) - hours*60;
seconds = floor(total_time) - hours*60*60 - minutes*60;

fprintf('\n\nTesting finished in %d hours %d minutes %d seconds\n',hours,minutes,seconds);
