00001
00002
00003 #ifndef SAMS_ENGIN_HPP
00004 #define SAMS_ENGIN_HPP
00005
00006 #include <iostream>
00007 #include <string>
00008 #include <vector>
00009 #include "kernel/keyValueFile.hpp"
00010 #include "jmath/jblas.hpp"
00011 #include "jmath/ublasExtra.hpp"
00012 #include "jmath/pca.hpp"
00013 #include "sams/detector.hpp"
00014 #include "sams/descriptor.hpp"
00015 #include "sams/feature.hpp"
00016 #include "sams/frame.hpp"
00017 #include "sams/model.hpp"
00018 #include "sams/matching.hpp"
00019 #include "sams/hypothesis.hpp"
00020 #include "sams/hough.hpp"
00021 #include "sams/background.hpp"
00022
00023 namespace jafar {
00024 namespace sams {
00025
00026 class Engine : public jafar::kernel::KeyValueFileSaveLoad {
00027 public:
00028
00029
00030 jafar::sams::SSDetector detector1;
00031 jafar::sams::DOGDetector detector2;
00032 jafar::sams::GIFTFactory descriptor;
00033 jafar::sams::SNNMatcher matcher;
00034 jafar::sams::ModelDB db;
00035 jafar::sams::BackgroundAnalysis backdb;
00036
00038 Engine() : byDefault(true) {};
00039
00041 Engine(const std::string& file_);
00042
00043
00047 inline void loadDetectorParams(const std::string& file_) {
00048 detector1.load(file_);
00049 }
00050
00054 inline void saveDetectorParams(const std::string& file_) {
00055 detector1.save(file_);
00056 };
00057
00058
00059 inline void getFrame(const jafar::image::Image& src_, jafar::sams::Frame& f_) {
00060 f_.attachImage(src_);
00061 f_.generateFeatures(detector1);
00062 f_.generateFeatures(detector2);
00063 f_.generateDescriptors(descriptor);
00064 };
00065
00066 inline jafar::sams::Hypothesis modelMatch(const jafar::sams::Model& md_,
00067 const jafar::sams::Frame& f_,
00068 std::vector<Match> & m_) {
00069 matcher(md_,f_,m_);
00070 JFR_WARNING("found " << m_.size() << " matches");
00071 db.insert("test",&md_);
00072 JFR_DEBUG("m_pos.x: " << m_[0].modelRef->pos());
00073 jafar::sams::Hough hg;
00074 std::vector<jafar::sams::Hypothesis> res;
00075 res = hg.process(md_,f_,m_);
00076 Hypothesis::pruneByAff(m_,res);
00077 JFR_ASSERT( res.size() != 0, "No candidate");
00078 std::vector<jafar::sams::Hypothesis>::iterator it = std::max_element(res.begin(),res.end());
00079 JFR_DEBUG("best Hypothesis = " << std::distance(res.begin(),it) << " with weight " << it->m_wght << " and " << it->m_matches.size() << " matches");
00080 jafar::sams::Hypothesis hc = *it;
00081 JFR_ASSERT(hc.m_matches.size() > 3, "Engine::match: no match");
00082 hc.computeSimilarityTransform();
00083 hc.computeProbability();
00084 JFR_DEBUG("probabiliy = " << hc.m_p);
00085 return hc;
00086 };
00087
00088 inline jafar::sams::Hypothesis match(const jafar::sams::Frame& md_,
00089 const jafar::sams::Frame& f_,
00090 std::vector<Match> & m_) {
00091 Model model("generic",md_);
00092 matcher(model,f_,m_);
00093 JFR_WARNING("found " << m_.size() << " matches");
00094 db.insert("test",&model);
00095 JFR_DEBUG("m_pos.x: " << m_[0].modelRef->pos());
00096 jafar::sams::Hough hg;
00097 std::vector<jafar::sams::Hypothesis> res;
00098 res = hg.process(model,f_,m_);
00099 Hypothesis::pruneByAff(m_,res);
00100 JFR_ASSERT( res.size() != 0, "No candidate");
00101 std::vector<jafar::sams::Hypothesis>::iterator it = std::max_element(res.begin(),res.end());
00102 JFR_DEBUG("best Hypothesis = " << std::distance(res.begin(),it) << " with weight " << it->m_wght << " and " << it->m_matches.size() << " matches");
00103 jafar::sams::Hypothesis hc = *it;
00104 JFR_ASSERT(hc.m_matches.size() > 3, "Engine::match: no match");
00105 hc.computeSimilarityTransform();
00106 hc.computeProbability();
00107 JFR_DEBUG("probabiliy = " << hc.m_p);
00108 return hc;
00109 };
00110
00113 double checking(std::string const & name_,
00114 jafar::sams::Frame const & fr_,
00115 std::vector<Match> const & matches_,
00116 jafar::sams::Hypothesis const & hc_,
00117 jafar::sams::ModelDB const & db_) const;
00118
00121 std::string learn(jafar::image::Image const & img, std::string const & label="");
00122
00125 std::vector<jafar::sams::Hypothesis> recognize(jafar::sams::Frame const & f) const;
00126
00127 void loadDB(std::string const & path_) {
00128 db.loadDB(path_);
00129 };
00130
00131 void saveDB(std::string const & path_=".") const {
00132 db.saveDB(path_);
00133 };
00134
00135 protected:
00139 void loadKeyValueFile(jafar::kernel::KeyValueFile const& keyValueFile);
00140
00141
00145 void saveKeyValueFile(jafar::kernel::KeyValueFile & keyValueFile);
00146
00147 private:
00148 bool byDefault;
00149 };
00150
00151 }
00152 }
00153
00154 #endif // SAMS_ENGIN_HPP