libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/detection_thread.h
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet *
00003  *   pierre.sermanet@gmail.com *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source
00014  *       Initiative (http://www.opensource.org) must display the
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef DETECTION_THREAD_H_
00034 #define DETECTION_THREAD_H_
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 
00039 #include "defines_tools.h"
00040 #include "thread.h"
00041 #include "netconf.h"
00042 #include "configuration.h"
00043 #include "bbox.h"
00044 #include "bootstrapping.h"
00045 
00046 using namespace std;
00047 
00048 namespace ebl {
00049 
00050 // switch between forward only buffers or also backward
00051 #define SFUNC fs
00052 #define SBUF fstate_idx
00053 #define SFUNC2(T) T,T,T,SBUF<T>
00054 #define SFUNC3(T) T,SBUF<T>,mstate<SBUF<T> >
00055 // backward
00056 // #define SFUNC bbs
00057 // #define SBUF bbstate_idx
00058 
00060   // A detection thread class
00061 
00062   template <typename Tnet>
00063   class detection_thread : public thread {
00064   public:
00071     detection_thread(configuration &conf, mutex *om = NULL,
00072                      const char *name = "",
00073                      const char *arg2 = NULL, bool sync = true,
00074                      t_chans tc = CHANS_RGB);
00075     ~detection_thread();
00076 
00078     virtual void execute();
00079 
00080     // thread communication ////////////////////////////////////////////////////
00081 
00085     virtual bool set_data(idx<ubyte> &frame, string &frame_fullname,
00086                           string &frame_name, uint frame_id);
00090     virtual bool set_data(string &frame_fullname, string &frame_name,
00091                           uint frame_id);
00099     virtual bool get_data(bboxes &bb, idx<ubyte> &frame,
00100                           uint &total_saved, string &frame_name,
00101                           uint &frame_id, svector<midx<Tnet> > &samples,
00102                           bboxes &bbsamples, bool &skipped);
00105     virtual bool available();
00107     virtual bool fed();
00109     static string get_output_directory(configuration &conf);
00110 
00112     static void init_detector(detector<SFUNC(Tnet)> &detector,
00113                               configuration &conf, string &odir);
00114 
00115     // internal methods ////////////////////////////////////////////////////////
00116   private:
00118     void clear_bboxes();
00121     void copy_bboxes(bboxes &bb);
00123     void copy_bootstrapping(svector<midx<Tnet> > &samples, bboxes &bb);
00126     void set_out_updated();
00128     void skip_frame();
00129 
00130     // private members /////////////////////////////////////////////////////////
00131   private:
00132     configuration        conf;
00133     const char          *arg2;
00134     idx<ubyte>           uframe;
00135     idx<Tnet>            frame;
00136     mutex                mutex_in;      // mutex for thread input
00137     mutex                mutex_out;     // mutex for thread output
00138     bboxes               bbs;
00139     bboxes::iterator     ibox;
00140     bool                 in_updated;    // thread input updated
00141     bool                 out_updated;   // thread output updated
00142     bool                 bavailable;    // thread is available
00143     bool                 bfed;  
00144     string               frame_name;    // name of current frame
00145     string               frame_fullname;
00146     uint                 frame_id;      
00147     string               outdir;        // output directory
00148     uint                 total_saved;
00149     using                thread::mout;  
00150     using                thread::merr;  
00151     t_chans              color_space;
00152     bool                 silent;
00153     svector<midx<Tnet> > returned_samples; 
00154     bboxes               returned_samples_bboxes;
00155     bootstrapping<Tnet>  boot; 
00156     bool                 frame_skipped; 
00157     bool                 frame_loaded; 
00158 
00159   public:
00160     detector<SFUNC(Tnet)>       *pdetect;
00161   };
00162 
00163 } // end namespace ebl
00164 
00165 #include "detection_thread.hpp"
00166 
00167 #endif /* DETECTION_THREAD_H_ */
00168