00001 /*!@file NeovisionII/nv2-gui-server.C sample neovision2 label server that sends its image patches to an OutputFrameSeries */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/NeovisionII/nv2-gui-server.C $ 00035 // $Id: nv2-gui-server.C 9736 2008-05-10 02:42:36Z rjpeters $ 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> // for usleep() 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 // else... res == NV2_IMAGE_PATCH_VALID 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 /* So things look consistent in everyone's emacs... */ 00198 /* Local Variables: */ 00199 /* mode: c++ */ 00200 /* indent-tabs-mode: nil */ 00201 /* End: */ 00202 00203 #endif // NEOVISIONII_NV2_GUI_SERVER_C_DEFINED