00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <iostream>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <polyora/polyora.h>
00025 using namespace std;
00026
00027 bool load_patch_track(id_cluster_collection *clusters, const char *descr_fn, kmean_tree::node_t *root)
00028 {
00029 FILE *descr_f = fopen(descr_fn, "rb");
00030 if (!descr_f) {
00031 perror(descr_fn);
00032 return false;
00033 }
00034
00035 id_cluster *c = new id_cluster();
00036
00037 kmean_tree::descr_file_packet packet;
00038 while(fread(&packet, sizeof(packet), 1, descr_f) == 1) {
00039 unsigned id = root->get_id(&packet.d);
00040 if (packet.ptr <0 && c->total>0) {
00041 clusters->add_cluster(c);
00042 c = new id_cluster();
00043 }
00044 c->add(id, 1);
00045 }
00046 fclose(descr_f);
00047 return true;
00048 }
00049
00050 int main(int argc, char **argv)
00051 {
00052 float threshold = .7;
00053 const char *descr_fn = "descriptors.dat";
00054 const char *tree_fn = 0;
00055 const char *clusters_fn = 0;
00056 const char *db_fn = "visual.db";
00057
00058 bool cluster_format=false;
00059
00060 for (int i=1; i<argc; i++) {
00061 if (i<argc-1) {
00062 if (strcmp(argv[i],"-d")==0) {
00063 descr_fn = argv[++i];
00064 continue;
00065 } else if (strcmp(argv[i],"-C")==0) {
00066 ++i;
00067 continue;
00068 } else if (strcmp(argv[i],"-v")==0) {
00069 db_fn = argv[++i];
00070 continue;
00071 } else if (strcmp(argv[i],"-t")==0) {
00072 tree_fn = argv[++i];
00073 continue;
00074 } else if (strcmp(argv[i],"-T")==0) {
00075 threshold = atof(argv[++i]);
00076 continue;
00077 } else if (strcmp(argv[i],"-o")==0) {
00078 clusters_fn = argv[++i];
00079 continue;
00080 } else if (strcmp(argv[i],"-c")==0) {
00081 cluster_format=true;
00082 descr_fn = argv[++i];
00083 continue;
00084 }
00085 }
00086 cout << "Available options:\n"
00087 " -v <visual db> (database to write the result to)\n"
00088 " -C <visual db> (read clusters from this database)\n"
00089 " -c <cluster file>\n"
00090 " -d <descriptor file>\n"
00091 " -t <tree file>\n"
00092 " -T <threshold> (use -1 for convertion only)\n"
00093 " -o <output file>\n";
00094 return(-1);
00095 }
00096
00097 sqlite3 *db=0;
00098 if (tree_fn==0 || clusters_fn ==0) {
00099 int rc = sqlite3_open(db_fn, &db);
00100 if (rc) {
00101 cerr << db_fn << ": " << sqlite3_errmsg(db) << endl;
00102 return -1;
00103 }
00104 }
00105
00106 kmean_tree::node_t *root = (db ? kmean_tree::load(db) : kmean_tree::load(tree_fn));
00107 if (!root) {
00108 std::cerr << tree_fn << ": unable to load tree\n";
00109 if (db) sqlite3_close(db);
00110 return -1;
00111 }
00112
00113 id_cluster_collection *clusters = new id_cluster_collection(id_cluster_collection::QUERY_NORMALIZED_FREQ);
00114
00115 for (int i=1; i<argc; i++) {
00116 if (i<argc-1) {
00117 if (strcmp(argv[i],"-d")==0) {
00118 if (!load_patch_track(clusters, argv[++i], root)) {
00119 cerr << argv[i] << ": loading failed.\n";
00120 } else {
00121 cerr << argv[i] << ": loaded successfully.\n";
00122 }
00123 } else if (strcmp(argv[i],"-C")==0) {
00124 sqlite3 *db;
00125 const char *db_fn = argv[++i];
00126 int rc = sqlite3_open_v2(db_fn, &db, SQLITE_OPEN_READONLY, 0);
00127 if (rc) {
00128 cerr << db_fn << ": can't open database\n";
00129 } else {
00130 if (!clusters->load(db))
00131 cerr << db_fn << ": can't read clusters in database.\n";
00132 sqlite3_close(db);
00133 }
00134 } else if (strcmp(argv[i],"-c")==0) {
00135
00136 if (!clusters->load(argv[++i])) {
00137 cerr << argv[i] << ": loading failed.\n";
00138 } else {
00139 cerr << argv[i] << ": loaded successfully.\n";
00140 }
00141 }
00142 }
00143 }
00144
00145
00146 if (clusters->clusters.size() == 0) {
00147 cerr << "Nothing loaded!\n";
00148 if (db) sqlite3_close(db);
00149 return -1;
00150 }
00151 cout << clusters->clusters.size() << " clusters loaded." << endl;
00152
00153 if (threshold>=0)
00154 clusters->reduce(threshold);
00155
00156
00157 if (!clusters_fn && db) {
00158 clusters->save(db);
00159 sqlite3_close(db);
00160 } else {
00161 clusters->save(clusters_fn);
00162 }
00163
00164 return 0;
00165 }