nv2-test-server.c

Go to the documentation of this file.
00001 /*!@file NeovisionII/nv2-test-server.c */
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-test-server.c $
00035 // $Id: nv2-test-server.c 8661 2007-08-06 23:45:24Z rjpeters $
00036 //
00037 
00038 #ifndef NEOVISIONII_NV2_TEST_SERVER_C_DEFINED
00039 #define NEOVISIONII_NV2_TEST_SERVER_C_DEFINED
00040 
00041 #include "NeovisionII/nv2_common.h"
00042 #include "NeovisionII/nv2_label_server.h"
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <unistd.h>
00047 
00048 static char** load_words(const size_t wordlen, size_t* nwords_ret)
00049 {
00050         const size_t nwords = *nwords_ret;
00051 
00052         char** const result =
00053                 malloc(sizeof(char*) * nwords + nwords * (wordlen + 1));
00054 
00055         char* const wordarea = (char*)(result + nwords);
00056 
00057         for (size_t i = 0; i < nwords; ++i)
00058         {
00059                 result[i] = wordarea + i * (wordlen + 1);
00060                 result[i][0] = '\0';
00061         }
00062 
00063         if (result == 0)
00064         {
00065                 fprintf(stderr, "malloc() failed\n");
00066                 exit(-1);
00067         }
00068 
00069         FILE* f = fopen("/usr/share/dict/words", "r");
00070 
00071         if (f == 0)
00072         {
00073                 fprintf(stderr, "couldn't open /usr/share/dict/words\n");
00074                 exit(-1);
00075         }
00076 
00077         size_t i = 0;
00078 
00079         size_t p = 0;
00080 
00081         while (1)
00082         {
00083                 int c = getc_unlocked(f);
00084                 if (c == EOF)
00085                 {
00086                         result[i][p] = '\0';
00087                         break;
00088                 }
00089                 else if (c == '\n')
00090                 {
00091                         if (p == wordlen)
00092                         {
00093                                 result[i][p] = '\0';
00094                                 if (++i >= nwords) break;
00095                         }
00096 
00097                         p = 0;
00098                 }
00099                 else
00100                 {
00101                         if (p < wordlen)
00102                         {
00103                                 result[i][p] = c;
00104                         }
00105 
00106                         ++p;
00107                 }
00108         }
00109 
00110         fclose(f);
00111 
00112         *nwords_ret = i;
00113 
00114         fprintf(stderr, "loaded %d words of length %d "
00115                 "from /usr/share/dict/words\n",
00116                 (int) *nwords_ret, (int) wordlen);
00117 
00118         return result;
00119 }
00120 
00121 int main (int argc, char* const argv[])
00122 {
00123         if (argc < 2 || argc > 6)
00124         {
00125                 fprintf(stderr,
00126                         "usage: %s ident patch-reader-port=%d "
00127                         "label-reader-ip-addr=127.0.0.1 "
00128                         "label-reader-port=%d "
00129                         "send-interval=1\n",
00130                         argv[0], NV2_PATCH_READER_PORT,
00131                         NV2_LABEL_READER_PORT);
00132                 return 0;
00133         }
00134 
00135         const char* const ident = argv[1];
00136         const int patch_reader_port =
00137                 argc >= 3 ? atoi(argv[2]) : NV2_PATCH_READER_PORT;
00138         const char* const label_reader_ip_addr =
00139                 argc >= 4 ? argv[3] : "127.0.0.1";
00140         const int label_reader_port =
00141                 argc >= 5 ? atoi(argv[4]) : NV2_LABEL_READER_PORT;
00142         const int send_interval =
00143                 argc >= 6 ? atoi(argv[5]) : 1;
00144 
00145         struct nv2_label_server* s =
00146                 nv2_label_server_create(patch_reader_port,
00147                                         label_reader_ip_addr,
00148                                         label_reader_port);
00149 
00150 
00151         const size_t wordlen = 13;
00152         size_t nwords = 40000;
00153 
00154         char** const words = load_words(wordlen, &nwords);
00155 
00156         double confidence = 0.0;
00157 
00158         while (1)
00159         {
00160                 struct nv2_image_patch p;
00161                 const enum nv2_image_patch_result res =
00162                         nv2_label_server_get_current_patch(s, &p);
00163 
00164                 if (res == NV2_IMAGE_PATCH_END)
00165                 {
00166                         fprintf(stdout, "ok, quitting\n");
00167                         break;
00168                 }
00169                 else if (res == NV2_IMAGE_PATCH_NONE)
00170                 {
00171                         usleep(10000);
00172                         continue;
00173                 }
00174 
00175                 // else... res == NV2_IMAGE_PATCH_VALID
00176 
00177                 confidence += 0.05 * drand48();
00178                 if (confidence > 1.0) confidence = 0.0;
00179 
00180                 struct nv2_patch_label l;
00181                 l.protocol_version = NV2_LABEL_PROTOCOL_VERSION;
00182                 l.patch_id = p.id;
00183                 l.confidence = (uint32_t)(0.5 + confidence
00184                                           * (double)(NV2_MAX_LABEL_CONFIDENCE));
00185                 snprintf(l.source, sizeof(l.source), "%s",
00186                          ident);
00187                 snprintf(l.name, sizeof(l.name), "%s (%ux%u #%u)",
00188                          words[rand() % nwords],
00189                          (unsigned int) p.width,
00190                          (unsigned int) p.height,
00191                          (unsigned int) p.id);
00192                 snprintf(l.extra_info, sizeof(l.extra_info),
00193                          "auxiliary information");
00194 
00195                 if (l.patch_id % send_interval == 0)
00196                 {
00197                         nv2_label_server_send_label(s, &l);
00198 
00199                         fprintf(stdout, "sent label '%s (%s)'\n",
00200                                 l.name, l.extra_info);
00201                 }
00202                 else
00203                 {
00204                         fprintf(stdout, "DROPPED label '%s (%s)'\n",
00205                                 l.name, l.extra_info);
00206                 }
00207 
00208                 nv2_image_patch_destroy(&p);
00209         }
00210 
00211         nv2_label_server_destroy(s);
00212 
00213         return 0;
00214 }
00215 
00216 // ######################################################################
00217 /* So things look consistent in everyone's emacs... */
00218 /* Local Variables: */
00219 /* indent-tabs-mode: nil */
00220 /* c-file-style: "linux" */
00221 /* End: */
00222 
00223 #endif // NEOVISIONII_NV2_TEST_SERVER_C_DEFINED
Generated on Sun May 8 08:05:23 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3