www.pudn.com > firev0.01.rar > fire.cpp


/*
This file is part of the FIRE -- Flexible Image Retrieval System

FIRE is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

FIRE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FIRE; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/**
 * @file   dirt.cpp
 * @author Thomas Deselaers
 * @date   Mon Jun 23 18:55:09 2003
 * 
 * @brief  FIRE - Flexible Image Retrival Engine.
 * 
 * The core program of everything contained in this directory. This
 * program implements an image retrieval system and can do feature
 * extraction if needed.
 * 
 * The USAGE function should always be up to date.
 * 
 */

#include 
#include 
#include "diag.hpp"
#include "getpot.hpp"
#include "euclideandist.hpp"
#include "socket.hpp"
#include "serversocket.hpp"
#include "processCommand.hpp"
#include "retrieverstatistics.hpp"
#include "basetools.hpp"
#include "distances.hpp"
#include "multifeature-ir.hpp"
#include "makeadistance.hpp"

using namespace std;
using namespace diag;
using namespace net;

void USAGE(string error="") {
  cout
    << "ERROR: " << error << endl
    << "USAGE: fire " << endl
    << endl
    << "  options:" << endl
    << "     -h                 show this help" << endl
//     << "     -frgb     feature extraction (rgb version)" << endl
//     << "     -fgrey    feature extraction (grey version)" << endl
//     << "        can get: -featurefunction (X01X20, X01X100,X01-X100)" << endl
//     << "        can get: -suffix (default: .rgbfeat/.greyfeat)" << endl
//     << "        can get: -samples (default: -1=all)" << endl
//     << "        can get: -steps the number of steps per dimension of the histogram. defaults: rgb:8 grey:256" << endl
    << "     -s [port]          server mode [default port: 12960]" << endl
//    << "     -t                 terminal mode" << endl
    << "     -perf              performance evaluation mode" << endl
    << "         -testCls    uncommented, buggy, evil" << endl
    << "     -qperf             perf eval mode with query-db" << endl
    << "            qperf needs additionally: -queryFileList" << endl
    << "            needs   " << endl
    << "                    -filelist  filelist has to contain classinformation" << endl
    << "            can get -results" << endl
    << "                    -ROCoutfile  .. to write a file with distances to NN of each class for each query image" << endl
    << "                    -dist {euclidean, jensenshannon, kullbachleiber,chisquare,emd,histogramintersection,timeshift}" << endl
    << "                    -weight (double, default 1.0)" << endl
    << "     -distanceFile    write Distance file for fast classification tests" << endl
    << "            needs   " << endl
    << "                    -filelist  filelist has to contain classinformation" << endl
    << "                    -dist {euclidean, jensenshannon, kullbachleiber,chisquare,emd,histogramintersection,timeshift}" << endl
    << "     -qdistanceFile   combination of -qperf and  -distanceFile" << endl
    << "     -startWith  - to start a distance file at a certain position" << endl
    << "     -stopWith  - to stop a distance file at a certain position" << endl
    << "     -nnFile  - to write a file with nearest neighbors" << endl
    << endl;
}

int main(int argc, char **argv) {
  GetPot cl(argc, argv);

//help requested  
  vector unidentified=cl.unidentified_options(117,
                                                      "-h",
///                                                       "-frgb", "-fgrey",
//                                                       "-featurefunction",
//                                                       "-suffix",
//                                                       "-samples",
                                                      "-s",
                                                      "-t",
                                                      "-perf","-distanceFile","-qperf","-qdistanceFile" ,"-queryFileList","-ROCoutfile","-startWith","-stopWith","-testCls",
                                                      "-nnFile",
                                                      "-filelist",
                                                      "-results",
                                                      "-weight",
                                                      "-steps",
                                                      "-dist0","-dist1", "-dist2", "-dist3", "-dist4", "-dist5", "-dist6", "-dist7", "-dist8", "-dist9",
                                                      "-dist10", "-dist11", "-dist12", "-dist13", "-dist14", "-dist15", "-dist16", "-dist17", "-dist18", "-dist19",
                                                      "-dist20","-dist21","-dist22","-dist23","-dist24","-dist25","-dist26","-dist27","-dist28","-dist29",
                                                      "-dist30","-dist31","-dist32","-dist33","-dist34","-dist35","-dist36","-dist37","-dist38","-dist39",
                                                      "-dist40","-dist41","-dist42","-dist43","-dist44","-dist45","-dist46","-dist47","-dist48","-dist49",
                                                      "-weight0", "-weight1", "-weight2", "-weight3", "-weight4", "-weight5", "-weight6","-weight7", "-weight8", "-weight9", 
                                                      "-weight10", "-weight11", "-weight12", "-weight13","-weight14", "-weight15", "-weight16", "-weight17", "-weight18", "-weight19",
                                                      "-weight20","-weight21", "-weight22", "-weight23", "-weight24", "-weight25", "-weight26", "-weight27", "-weight28","-weight29",
                                                      "-weight30","-weight31","-weight32","-weight33","-weight34","-weight35","-weight36","-weight37","-weight38","-weight39",
                                                      "-weight40","-weight41","-weight42","-weight43","-weight44","-weight45","-weight46","-weight47","-weight48","-weight49"
                                                      );
  if(unidentified.size()!=0) {
    cout << "Unknown parameters: ";
    for(unsigned int i=0;i");
      exit(20);
    }
    ir->fileListName()=cl.follow("","-filelist");
    ir->loadFileList();
    
    for(int no=0;nonOfSufficesFromFileList();++no) {
      ostringstream number;
      number << no;
      string numberStr=number.str();
      string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
      ir->dist(no)=makeADistance(dist);
    }
    ir->writeDistanceFile(cl.follow("dists.dat","-distanceFile"),cl.follow(-1,"-startWith"), cl.follow(-1,"-stopWith"));
    delete ir;
    
  } else if(cl.search("-qdistanceFile")) {
    MultiFeatureIR *ir=new MultiFeatureIR();
    if(!cl.search("-filelist") || !cl.search("-queryFileList")) {
      USAGE("qdistancefile mode needs -filelist  and -queryFileList");
      exit(20);
    }
    ir->fileListName()=cl.follow("","-filelist");
    ir->loadFileList();
    
    for(int no=0;nonOfSufficesFromFileList();++no) {
      ostringstream number;
      number << no;
      string numberStr=number.str();
      string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
      ir->dist(no)=makeADistance(dist);
    }
    ir->writeQueryDistanceFile(cl.follow("qdists.dat","-qdistanceFile"),cl.follow("querylist","-queryFileList"),cl.follow(-1,"-startWith"), cl.follow(-1,"-stopWith"));
    delete ir;


  } else if(cl.search("-perf")) {
    MultiFeatureIR *ir=new MultiFeatureIR();
    if(!cl.search("-filelist")) {
      USAGE("Performance evaluation mode needs -filelist ");
      exit(20);
    }
    ir->fileListName()=cl.follow("","-filelist");
    ir->loadFileList();


    for(int no=0;nonOfSufficesFromFileList();++no) {
      string weightStr="weight";
      ostringstream number;
      number << no;
      string numberStr=number.str();
      string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
      double weight=cl.follow(1.0,string("-"+weightStr+numberStr).c_str());
      
      ir->dist(no)=makeADistance(dist);
      ir->distWeight(no)=weight;
    }
    ir->results()=cl.follow(9,"-results");

    ir->status();
    cout << ir->getResult() << endl;
    
    
    cout << ir->performanceEvaluation(cl.follow(-1,"-testCls")) << endl;
    delete ir;
    //more parameterhandling here
  } else if(cl.search("-qperf")) {
    MultiFeatureIR *ir=new MultiFeatureIR();
    if(!cl.search("-filelist")) {
      USAGE("Performance evaluation mode needs -filelist ");
      exit(20);
    }
    ir->fileListName()=cl.follow("","-filelist");
    ir->loadFileList();
    
    
    for(int no=0;nonOfSufficesFromFileList();++no) {
      string weightStr="weight";
      ostringstream number;
      number << no;
      string numberStr=number.str();
      string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
      double weight=cl.follow(1.0,string("-"+weightStr+numberStr).c_str());
      
      ir->dist(no)=makeADistance(dist);
      ir->distWeight(no)=weight;
    }
    ir->results()=cl.follow(9,"-results");
    
    ir->status();
    cout << ir->getResult() << endl;
    
    
    cout << ir->queryEvaluation(cl.follow("querylist","-queryFileList"),cl.follow("","-ROCoutfile")) << endl;
    delete ir;
    //more parameterhandling here
  } else if(cl.search("-nnFile")) {
    MultiFeatureIR *ir=new MultiFeatureIR();
    if(!cl.search("-filelist")) {
      USAGE("Performance evaluation mode needs -filelist ");
      exit(20);
    }
    ir->fileListName()=cl.follow("","-filelist");
    ir->loadFileList();
        
    for(int no=0;nonOfSufficesFromFileList();++no) {
      string weightStr="weight";
      ostringstream number;
      number << no;
      string numberStr=number.str();
      string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
      double weight=cl.follow(1.0,string("-"+weightStr+numberStr).c_str());
      
      ir->dist(no)=makeADistance(dist);
      ir->distWeight(no)=weight;
    }
    ir->results()=cl.follow(9,"-results");
    
    ir->status();
    cout << ir->getResult() << endl;

    string outfile=cl.follow("nnFile.dat","-nnFile");    
    string queryList=cl.follow("querylist","-queryFileList");
    
    ir->nnFile(queryList, outfile);
    delete ir;
    //more parameterhandling here
  } else if(cl.search("-s")) {
    DBG(DBG_MESSAGE) << "Starting in server mode" << endl;
    int port=cl.follow(12960,"-s");
    ServerSocket server(port);
    if(!server.listening()) {exit(10);}
    string result;
    MultiFeatureIR *ir=new MultiFeatureIR();

    if(cl.search("-results")) {
      ir->results()=cl.follow(9,"-results");
    }
    if(cl.search("-filelist")) {
      DBG(DBG_MESSAGE) << "Loading files .";
      ir->fileListName()=cl.follow("fl","-filelist");
      BLINK(DBG_MESSAGE) << ".";
      ir->loadFileList();
      BLINK(DBG_MESSAGE) << ". done " << endl;
      
      for(int no=0;nonOfSufficesFromFileList();++no) {
        DBG(DBG_MESSAGE) << "Setting Distances." << endl;
        string weightStr="weight";
        ostringstream number;
        number << no;
        string numberStr=number.str();
        string dist=cl.follow("basedist",string("-dist"+numberStr).c_str());
        double weight;
        if(dist!="basedist") {
          weight=cl.follow(1.0,string("-"+weightStr+numberStr).c_str());
        } else {
          weight=cl.follow(0.0,string("-"+weightStr+numberStr).c_str());
        }
        
        ir->dist(no)=makeADistance(dist);
        ir->distWeight(no)=weight;
      }
    }
    
    bool runFurther=true;
    while (runFurther || result != "quiting\n") {
      Socket client=server.accept();
      runFurther=true;
      string command;
      while(runFurther) {
        command=client.getline();
        runFurther=processCommand(command,result,ir );
        result+="\n";
        cout << "Result: '" <> command;
//       runFurther=processCommand(command, result, ir);
//       DBG(DBG_RESULT) << result << endl;
//     }
    
    //no valid input parameter
  } else {
    USAGE("No valid parameter given");
    exit(20);
  }
  
  printCmdline(argc,argv);

}