saveimage-server.C

00001 /*!@file HMAX/test-hmax5.C Test Hmax class and compare to original code */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // 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: Dan Parks <danielfp@usc.edu>
00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/CUDA/saveimage-server.C $
00035 // $Id: saveimage-server.C 14154 2010-10-21 05:07:25Z dparks $
00036 //
00037 
00038 #include "Component/ModelManager.H"
00039 #include "Learn/Bayes.H"
00040 #include "GUI/DebugWin.H"
00041 #include "NeovisionII/nv2_common.h"
00042 #include "NeovisionII/nv2_label_server.h"
00043 
00044 #include "GUI/XWindow.H"
00045 #include "CUDA/CudaHmaxFL.H"
00046 #include "CUDA/CudaHmax.H"
00047 #include "CUDA/CudaImage.H"
00048 #include "Image/Image.H"
00049 #include "Image/ImageSet.H"
00050 #include "Image/ColorOps.H"
00051 #include "Image/ShapeOps.H"
00052 #include "Image/CutPaste.H"
00053 #include "Image/FilterOps.H"
00054 #include "Image/Rectangle.H"
00055 #include "Image/MathOps.H"
00056 #include "Image/DrawOps.H"
00057 #include "Image/MatrixOps.H"
00058 #include "Image/Transforms.H"
00059 #include "Image/Convolutions.H"
00060 #include "Learn/SVMClassifier.H"
00061 #include "Media/FrameSeries.H"
00062 #include "nub/ref.h"
00063 #include "Raster/GenericFrame.H"
00064 #include "Raster/Raster.H"
00065 #include "Util/Types.H"
00066 #include "Util/log.H"
00067 
00068 #include <signal.h>
00069 
00070 #include "rutz/fstring.h"
00071 #include "rutz/time.h"
00072 #include "rutz/timeformat.h"
00073 
00074 
00075 #include <fstream>
00076 #include <map>
00077 #include <vector>
00078 #include <utility>
00079 #include <iostream>
00080 #include <iomanip>
00081 #include <string>
00082 #include <unistd.h>
00083 #include <cstdlib>
00084 
00085 
00086 // number of orientations to use in HmaxFL
00087 #define NORI 4
00088 #define NUM_PATCHES_PER_SIZE 250
00089 
00090 
00091 const bool USECOLOR = false;
00092 
00093 bool terminate = false;
00094 
00095 void terminateProc(int s)
00096 {
00097   terminate = true;
00098 }
00099 
00100 
00101 int main(const int argc, const char **argv)
00102 {
00103 
00104   MYLOGVERB = LOG_INFO;
00105 
00106   ModelManager *mgr = new ModelManager("Hmax with Feature Learning Server");
00107 
00108 
00109   mgr->exportOptions(MC_RECURSE);
00110 
00111 
00112   if (mgr->parseCommandLine(
00113                             (const int)argc, (const char**)argv, "<sampleimagesdir> <localport> <server_ip> <serverport> <prefix>", 4, 5) == false)
00114     return 1;
00115 
00116   std::string serverIP,serverPortStr,localPortStr;
00117   std::string sampleImagesDir,prefixStr;
00118 
00119   // Now we run
00120   mgr->start();
00121 
00122   // catch signals and redirect them to terminate for clean exit:
00123   signal(SIGHUP, terminateProc); signal(SIGINT, terminateProc);
00124   signal(SIGQUIT, terminateProc); signal(SIGTERM, terminateProc);
00125   signal(SIGALRM, terminateProc);
00126 
00127 
00128   sampleImagesDir = mgr->getExtraArg(0);
00129   localPortStr = mgr->getExtraArg(1);
00130   serverIP = mgr->getExtraArg(2);
00131   serverPortStr = mgr->getExtraArg(3);
00132   prefixStr = std::string("Sample");
00133   if(mgr->numExtraArgs() == 5)
00134     prefixStr = mgr->getExtraArg(4);
00135 
00136   XWinManaged xwin(Dims(256,256),
00137                    -1, -1, "ILab Robot Head Demo");
00138 
00139   int serverPort = strtol(serverPortStr.c_str(),NULL,0);
00140   int localPort = strtol(localPortStr.c_str(),NULL,0);
00141   struct nv2_label_server* labelServer =
00142     nv2_label_server_create(localPort,
00143                             serverIP.c_str(),
00144                             serverPort);
00145 
00146   nv2_label_server_set_verbosity(labelServer,1); //allow warnings
00147   int curPatch = 0;
00148 
00149   //const size_t max_label_history = 1;
00150   //std::deque<std::string> recent_labels;
00151 
00152   //Image<PixRGB<byte> > colorbars = makeColorbars(256, 256);
00153 
00154 
00155   while(!terminate)
00156     {
00157       //Point2D<int> clickLoc = xwin.getLastMouseClick();
00158 
00159       struct nv2_image_patch p;
00160       const enum nv2_image_patch_result res =
00161         nv2_label_server_get_current_patch(labelServer, &p);
00162 
00163       std::string objName;
00164       if (res == NV2_IMAGE_PATCH_END)
00165         {
00166           LINFO("ok, quitting");
00167           break;
00168         }
00169       else if (res == NV2_IMAGE_PATCH_NONE)
00170         {
00171           usleep(10000);
00172           continue;
00173         }
00174       else if (res == NV2_IMAGE_PATCH_VALID)
00175         {
00176           if (p.type != NV2_PIXEL_TYPE_RGB24)
00177             {
00178               LINFO("got a non-rgb24 patch; ignoring %i", p.type);
00179               continue;
00180             }
00181 
00182           if (p.width * p.height == 1)
00183             {
00184               //xwin.drawImage(addLabels(colorbars, p.id));
00185               continue;
00186             }
00187 
00188           Image<PixRGB<byte> > img(p.width, p.height, NO_INIT);
00189           // Get the test image from the socket
00190           memcpy(img.getArrayPtr(), p.data, p.width*p.height*3);
00191 
00192           Image<PixRGB<byte> > inputImg = rescale(img, 256, 256);
00193 
00194           //xwin.drawImage(inputImg);
00195 
00196           //Image<float> inputf = luminance(inputImg);
00197           char fname[200];
00198           sprintf(fname,"%s/%s-%d.png",sampleImagesDir.c_str(),prefixStr.c_str(),curPatch);
00199           Raster::WriteRGB(inputImg,fname);
00200           curPatch++;
00201         }
00202     }
00203   if (terminate)
00204     LINFO("Ending application because a signal was caught");
00205 
00206   //nv2_label_server_destroy(labelServer);
00207   LINFO("Got Here");
00208 
00209   return 0;
00210 }
00211 
00212 
00213 
00214 
00215 // ######################################################################
00216 /* So things look consistent in everyone's emacs... */
00217 /* Local Variables: */
00218 /* indent-tabs-mode: nil */
00219 /* End: */
Generated on Sun May 8 08:40:36 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3