nv2-gui-server.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef NEOVISIONII_NV2_GUI_SERVER_C_DEFINED
00039 #define NEOVISIONII_NV2_GUI_SERVER_C_DEFINED
00040
00041 #include "Component/ModelManager.H"
00042 #include "Image/Image.H"
00043 #include "Image/Pixels.H"
00044 #include "Media/FrameSeries.H"
00045 #include "NeovisionII/nv2_common.h"
00046 #include "NeovisionII/nv2_label_server.h"
00047 #include "Util/sformat.H"
00048
00049 #include <cstdio>
00050 #include <fstream>
00051 #include <string>
00052 #include <unistd.h>
00053 #include <vector>
00054
00055 void load_words2(const size_t wordlen, std::vector<std::string>& words)
00056 {
00057 std::ifstream ifs("/usr/share/dict/words");
00058
00059 if (!ifs.is_open())
00060 LFATAL("couldn't open /usr/share/dict/words");
00061
00062 int c = 0;
00063 std::string line;
00064 while (std::getline(ifs, line))
00065 if (line.length() == wordlen)
00066 {
00067 words.push_back(line);
00068 ++c;
00069 }
00070
00071 LINFO("loaded %d words of length %"ZU" from /usr/share/dict/words\n",
00072 c, wordlen);
00073 }
00074
00075 int main (int argc, char* const argv[])
00076 {
00077 ModelManager manager("nv2-gui-server");
00078
00079 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00080 manager.addSubComponent(ofs);
00081
00082 if (manager.parseCommandLine(argc, argv,
00083 sformat("ident patch-reader-port=%d "
00084 "label-reader-ip-addr=127.0.0.1 "
00085 "label-reader-port=%d "
00086 "send-interval=1 "
00087 "do-send-labels=1",
00088 NV2_PATCH_READER_PORT,
00089 NV2_LABEL_READER_PORT).c_str(),
00090 1, 6) == false)
00091 return 1;
00092
00093 const std::string ident = manager.getExtraArg(0);
00094 const int patch_reader_port =
00095 manager.numExtraArgs() >= 2 ? manager.getExtraArgAs<int>(1) : NV2_PATCH_READER_PORT;
00096 const std::string label_reader_ip_addr =
00097 manager.numExtraArgs() >= 3 ? manager.getExtraArg(2) : std::string("127.0.0.1");
00098 const int label_reader_port =
00099 manager.numExtraArgs() >= 4 ? manager.getExtraArgAs<int>(3) : NV2_LABEL_READER_PORT;
00100 const int send_interval =
00101 manager.numExtraArgs() >= 5 ? manager.getExtraArgAs<int>(4) : 1;
00102 const bool do_send_labels =
00103 manager.numExtraArgs() >= 6 ? manager.getExtraArgAs<bool>(5) : true;
00104
00105 manager.start();
00106
00107 struct nv2_label_server* s =
00108 nv2_label_server_create(patch_reader_port,
00109 label_reader_ip_addr.c_str(),
00110 label_reader_port);
00111
00112
00113 const size_t wordlen = 13;
00114 std::vector<std::string> words;
00115
00116 if (do_send_labels)
00117 load_words2(wordlen, words);
00118
00119 double confidence = 0.0;
00120
00121 while (1)
00122 {
00123 struct nv2_image_patch p;
00124 const enum nv2_image_patch_result res =
00125 nv2_label_server_get_current_patch(s, &p);
00126
00127 if (res == NV2_IMAGE_PATCH_END)
00128 {
00129 fprintf(stdout, "ok, quitting\n");
00130 break;
00131 }
00132 else if (res == NV2_IMAGE_PATCH_NONE)
00133 {
00134 usleep(10000);
00135 continue;
00136 }
00137
00138
00139
00140 ofs->updateNext();
00141
00142 if (p.type == NV2_PIXEL_TYPE_GRAY8)
00143 {
00144 const Image<byte> im((const byte*) p.data,
00145 p.width, p.height);
00146
00147 ofs->writeGray(im, "gray8-on-label-server");
00148 }
00149 else if (p.type == NV2_PIXEL_TYPE_RGB24)
00150 {
00151 const Image<PixRGB<byte> > im((const PixRGB<byte>*) p.data,
00152 p.width, p.height);
00153
00154 ofs->writeRGB(im, "rgb24-on-label-server");
00155 }
00156
00157 if (do_send_labels)
00158 {
00159 confidence += 0.05 * drand48();
00160 if (confidence > 1.0) confidence = 0.0;
00161
00162 struct nv2_patch_label l;
00163 l.protocol_version = NV2_LABEL_PROTOCOL_VERSION;
00164 l.patch_id = p.id;
00165 l.confidence = (uint32_t)(0.5 + confidence
00166 * double(NV2_MAX_LABEL_CONFIDENCE));
00167 snprintf(l.source, sizeof(l.source), "%s",
00168 ident.c_str());
00169 snprintf(l.name, sizeof(l.name), "%s (%ux%u #%u)",
00170 words.at(rand() % words.size()).c_str(),
00171 (unsigned int) p.width,
00172 (unsigned int) p.height,
00173 (unsigned int) p.id);
00174 snprintf(l.extra_info, sizeof(l.extra_info),
00175 "auxiliary information");
00176
00177 if (l.patch_id % send_interval == 0)
00178 {
00179 nv2_label_server_send_label(s, &l);
00180 LINFO("sent label '%s (%s)'\n", l.name, l.extra_info);
00181 }
00182 else
00183 {
00184 LINFO("DROPPED label '%s (%s)'\n", l.name, l.extra_info);
00185 }
00186 }
00187
00188 nv2_image_patch_destroy(&p);
00189 }
00190
00191 nv2_label_server_destroy(s);
00192
00193 return 0;
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203 #endif // NEOVISIONII_NV2_GUI_SERVER_C_DEFINED