Beispiel #1
0
// ######################################################################
void VisualTracker::setTargets(const Image<byte>& grey, const Point2D<int> loc)
{
  if (!loc.isValid())
  {
    itsTracking = false;
    return;
  }

  if (itsInitTracker)
    initTracker(grey.getDims());

#ifdef HAVE_OPENCV
  itsCurrentNumPoints = itsMaxNumPoints;

  IplImage* currentImg = img2ipl(grey);
  itsCurrentPoints[0].x = loc.i;
  itsCurrentPoints[0].y = loc.j;

  //cvFindCornerSubPix(currentImg, itsCurrentPoints, itsCurrentNumPoints,
  //    cvSize(itsInitTrackWindowSize.getVal(),itsInitTrackWindowSize.getVal()),
  //    cvSize(-1,-1),
  //    cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,
  //      20,0.03));
  cvReleaseImageHeader(&currentImg);

  itsPreviousGreyImg = grey;

  itsTrackFlags = 0;
  IplImage *swap_temp;
  CV_SWAP( itsPreviousPyramid, itsCurrentPyramid, swap_temp );

  CvPoint2D32f* swap_points;
  CV_SWAP( itsPreviousPoints, itsCurrentPoints, swap_points );

  if (itsUseKalman)
  {
    itsKalman->state_post->data.fl[0] = loc.i;
    itsKalman->state_post->data.fl[1] = loc.j;
  }

#endif

  itsTracking = true;
}
Beispiel #2
0
int main(const int argc, const char **argv)
{

  MYLOGVERB = LOG_INFO;
  mgr = new ModelManager("Test ObjRec");

  nub::soft_ref<SimEventQueueConfigurator>
    seqc(new SimEventQueueConfigurator(*mgr));
  mgr->addSubComponent(seqc);

  //our brain
  nub::ref<StdBrain>  brain(new StdBrain(*mgr));
  mgr->addSubComponent(brain);

  mgr->exportOptions(MC_RECURSE);
  mgr->setOptionValString(&OPT_VisualCortexType, "IOC");
  //mgr.setOptionValString(&OPT_VisualCortexType, "I");
  //mgr->setOptionValString(&OPT_VisualCortexType, "GNO");
  //mgr.setOptionValString(&OPT_VisualCortexType, "N");
  //manager.setOptionValString(&OPT_UseOlderVersion, "false");
  // set the FOA and fovea radii
  mgr->setOptionValString(&OPT_SaliencyMapType, "Fast");
  mgr->setOptionValString(&OPT_SMfastInputCoeff, "1");

  mgr->setOptionValString(&OPT_WinnerTakeAllType, "Fast");
  mgr->setOptionValString(&OPT_SimulationTimeStep, "0.2");

  mgr->setModelParamVal("FOAradius", 50, MC_RECURSE);
  mgr->setModelParamVal("FoveaRadius", 50, MC_RECURSE);


  mgr->setOptionValString(&OPT_IORtype, "Disc");

  if (mgr->parseCommandLine(
        (const int)argc, (const char**)argv, "<Network file> <server ip>", 2, 2) == false)
    return 1;

  // catch signals and redirect them to terminate for clean exit:
  signal(SIGHUP, terminateProc); signal(SIGINT, terminateProc);
  signal(SIGQUIT, terminateProc); signal(SIGTERM, terminateProc);
  signal(SIGALRM, terminateProc);

  mgr->start();

  ComplexChannel *cc =
    &*dynCastWeak<ComplexChannel>(brain->getVC());

  //Get a new descriptor vector
  DescriptorVec descVec(*mgr, "Descriptor Vector", "DecscriptorVec", cc);
  //Get  new classifier
  Bayes bayesNet(descVec.getFVSize(), 0);

  //get command line options
  const char *bayesNetFile = mgr->getExtraArg(0).c_str();
  const char *server_ip = mgr->getExtraArg(1).c_str();
  bool train = false;

  int foveaRadius = mgr->getModelParamVal<int>("FoveaRadius", MC_RECURSE);

  printf("Setting fovea to %i, train = %i\n", foveaRadius, train);

  //load the network if testing
  //if (!train)
    bayesNet.load(bayesNetFile);

  descVec.setFoveaSize(foveaRadius);

  xwin  = new XWinManaged(Dims(256,256),
      -1, -1, "ILab Robot Head Demo");


  server = nv2_label_server_create(9930,
        server_ip,
        9931);

  nv2_label_server_set_verbosity(server,1); //allow warnings


  int send_interval = 1;

  while(!terminate)
  {
    double prob = 0, statSig = 0;

    Point2D clickLoc = xwin->getLastMouseClick();
    if (clickLoc.isValid())
      train = !train;

    struct nv2_image_patch p;
    const enum nv2_image_patch_result res =
      nv2_label_server_get_current_patch(server, &p);

    std::string objName = "nomatch";
    if (res == NV2_IMAGE_PATCH_END)
    {
      fprintf(stdout, "ok, quitting\n");
      break;
    }
    else if (res == NV2_IMAGE_PATCH_NONE)
    {
      usleep(10000);
      continue;
    }
    else if (res == NV2_IMAGE_PATCH_VALID &&
       p.type == NV2_PIXEL_TYPE_RGB24)
    {
      printf("Valid patch %s %ix%i\n", p.training_label,
          p.width, p.height);

      //showimg
      Image<PixRGB<byte> > img(p.width, p.height, NO_INIT);
     // unsigned char *imgPtr = const_cast<unsigned char*>
     //   (reinterpret_cast<const unsigned char*>(img.getArrayPtr()));

      memcpy(img.getArrayPtr(), p.data, p.width*p.height*3);

      Image<PixRGB<byte> > objImg = rescale(img, 256, 256);

      int cls = classifyImage(objImg, descVec, bayesNet, &prob, &statSig);
      if (cls != -1 && prob > -150)
        objName = bayesNet.getClassName(cls);
      else
        objName = "nomatch";

      printf("This is %s: Class %i prob %f\n",
          objName.c_str(), cls, prob);


     // if (strcmp(p.training_label, "none") != 0 &&
     //     false) { //training
     if (cls == -1)
     {
        printf("Can you tell me what this is?\n");
        std::getline(std::cin, objName);
        learnImage(objImg, 0, descVec, bayesNet, objName.c_str());
        bayesNet.save(bayesNetFile);
      } else {
        printf("Is this a %s?\n", objName.c_str());

        if (train)
        {
          std::string tmp;
          std::getline(std::cin, tmp);
          if (tmp != "")
            objName = tmp;

          LINFO("Learning %s\n", objName.c_str());
          fflush(stdout);

          learnImage(objImg, 0, descVec, bayesNet, objName.c_str());
          bayesNet.save(bayesNetFile);
        }

      }

    }

    if (objName != "nomatch")
    {
      printf("Object is %s\n", objName.c_str());

      struct nv2_patch_label l;
      l.protocol_version = NV2_LABEL_PROTOCOL_VERSION;
      l.patch_id = p.id;
      snprintf(l.source, sizeof(l.source), "%s",
          "ObjRec");
      snprintf(l.name, sizeof(l.name), "%s", // (%ux%u #%u)",
          objName.c_str());
      //(unsigned int) p.width,
      //(unsigned int) p.height,
      //(unsigned int) p.id);
      snprintf(l.extra_info, sizeof(l.extra_info),
          "%i", (int)statSig);

      if (l.patch_id % send_interval == 0)
      {
        nv2_label_server_send_label(server, &l);

        fprintf(stdout, "sent label '%s (%s)'\n",
            l.name, l.extra_info);
      }
      else
      {
        fprintf(stdout, "DROPPED label '%s (%s)'\n",
            l.name, l.extra_info);
      }
    }

    nv2_image_patch_destroy(&p);
  }

  nv2_label_server_destroy(server);

}
int main(const int argc, const char **argv)
{

  MYLOGVERB = LOG_INFO;
  mgr = new ModelManager("Test ObjRec");

  if (mgr->parseCommandLine(
        (const int)argc, (const char**)argv, "<vdb file> <server ip>", 2, 2) == false)
    return 1;

  mgr->start();

  // catch signals and redirect them to terminate for clean exit:
  signal(SIGHUP, terminateProc); signal(SIGINT, terminateProc);
  signal(SIGQUIT, terminateProc); signal(SIGTERM, terminateProc);
  signal(SIGALRM, terminateProc);

  //get command line options
  const char *vdbFile = mgr->getExtraArg(0).c_str();
  const char *server_ip = mgr->getExtraArg(1).c_str();
  bool train = false;

  LINFO("Loading db from %s\n", vdbFile);
  //vdb.loadFrom(std::string(vdbFile));

  xwin  = new XWinManaged(Dims(256,256),
      -1, -1, "ILab Robot Head Demo");


   labelServer =
    nv2_label_server_create(9930,
        server_ip,
        9931);

  nv2_label_server_set_verbosity(labelServer,1); //allow warnings


  int send_interval = 1;

  while(!terminate)
  {

    Point2D clickLoc = xwin->getLastMouseClick();
    if (clickLoc.isValid())
      train = !train;

    struct nv2_image_patch p;
    const enum nv2_image_patch_result res =
      nv2_label_server_get_current_patch(labelServer, &p);

    std::string objName;
    if (res == NV2_IMAGE_PATCH_END)
    {
      fprintf(stdout, "ok, quitting\n");
      break;
    }
    else if (res == NV2_IMAGE_PATCH_NONE)
    {
      usleep(10000);
      continue;
    }
    else if (res == NV2_IMAGE_PATCH_VALID &&
        p.type == NV2_PIXEL_TYPE_RGB24)
    {
      Image<PixRGB<byte> > img(p.width, p.height, NO_INIT);
      memcpy(img.getArrayPtr(), p.data, p.width*p.height*3);

      Image<PixRGB<byte> > inputImg = rescale(img, 256, 256);

      std::string objName = matchObject(inputImg);

      Image<PixRGB<byte> > disp(320, 240, ZEROS);

      xwin->drawImage(inputImg);

      if (objName == "nomatch")
      {
        if (train)
        {
          printf("Is this %s\n", objName.c_str());
          std::string tmp;
          std::getline(std::cin, tmp);
          if (tmp == "exit") break;
          if (tmp == "no")
          {
            printf("Can you tell me what this is?\n");
            std::getline(std::cin, objName);

            rutz::shared_ptr<VisualObject>
              vo(new VisualObject(objName.c_str(), "NULL", inputImg,
                    Point2D(-1,-1),
                    std::vector<double>(),
                    std::vector< rutz::shared_ptr<Keypoint> >(),
                    USECOLOR));
            vdb.addObject(vo);
            vdb.saveTo(vdbFile);
          }
        }
      } else {
        printf("Object is %s\n", objName.c_str());
        struct nv2_patch_label l;
        l.protocol_version = NV2_LABEL_PROTOCOL_VERSION;
        l.patch_id = p.id;
        snprintf(l.source, sizeof(l.source), "%s",
            "ObjRec");
        snprintf(l.name, sizeof(l.name), "%s", // (%ux%u #%u)",
            objName.c_str());
        //(unsigned int) p.width,
        //(unsigned int) p.height,
        //(unsigned int) p.id);
        snprintf(l.extra_info, sizeof(l.extra_info),
            "auxiliary information");

        if (l.patch_id % send_interval == 0)
        {
          nv2_label_server_send_label(labelServer, &l);

          fprintf(stdout, "sent label '%s (%s)'\n",
              l.name, l.extra_info);
        }
        else
        {
          fprintf(stdout, "DROPPED label '%s (%s)'\n",
              l.name, l.extra_info);
        }
      }

      nv2_image_patch_destroy(&p);
    }

  }
  nv2_label_server_destroy(labelServer);

}
// ######################################################################
void CenterSurroundHistogramSegmenter::drawCurrentCSbelief
(Point2D<int> pt, Rectangle grC, Rectangle grS)
{
  uint width  = itsImage.getWidth();
  uint height = itsImage.getHeight();
  if(itsWin.is_invalid())
    itsWin.reset(new XWinManaged(Dims(2*width, height), 0, 0, "CSHse"));
  else itsWin->setDims(Dims(2*width, height));

  uint gwidth  = width/GRID_SIZE;
  uint gheight = height/GRID_SIZE;

  // display the window      
  Image<PixRGB<byte> > disp(2*width, height, ZEROS);
  inplacePaste(disp, itsImage, Point2D<int>(0,0));
  if(pt.isValid())
    {
      drawCross(disp, pt, PixRGB<byte>(255,0,0), 10, 1);
    }
  if(grC.isValid())
    {
      drawRect(disp, grC*GRID_SIZE, PixRGB<byte>(255,0,0), 1);
      drawRect(disp, grS*GRID_SIZE, PixRGB<byte>(0,255,0), 1);
    }

  float mVal = 32;
  float bVal = 255 - mVal;
  
  Image<byte> dImaR, dImaG, dImaB;
  getComponents(itsImage, dImaR, dImaG, dImaB);
  inplaceNormalize(dImaR, byte(0), byte(mVal));
  inplaceNormalize(dImaG, byte(0), byte(mVal));
  inplaceNormalize(dImaB, byte(0), byte(mVal));
  Image<PixRGB<byte> > dIma  = makeRGB(dImaR,dImaG,dImaB);      
  
  // Image<float> dImaCf = itsGridCenterBelief;
  // inplaceNormalize(dImaCf, 0.0F, bVal);
  // Image<byte> dImaCb(dImaCf);
  // Image<PixRGB<byte> > dImaC = makeRGB(dImaCb,dImaCb,dImaCb);
  
  // Image<float> dImaSf = itsGridSurroundBelief;
  // inplaceNormalize(dImaSf, 0.0F, bVal);
  // Image<byte> dImaSb(dImaSf);
  // Image<PixRGB<byte> > dImaS = makeRGB(dImaSb,dImaSb,dImaSb);

  // Image<PixRGB<byte> > tdImaC(dIma+zoomXY(dImaC,GRID_SIZE));
  // Image<PixRGB<byte> > tdImaS(dIma+zoomXY(dImaS,GRID_SIZE));
  // inplacePaste (disp, tdImaC, Point2D<int>(width,0));
  // inplacePaste (disp, tdImaS, Point2D<int>(2*width,0));
 
  Image<float> dImaCSf = 
    clampedDiff((itsGridCenterBelief - itsGridSurroundBelief), 
              Image<float>(gwidth,gheight,ZEROS));
  inplaceNormalize(dImaCSf, 0.0F, bVal);
  Image<byte> dImaCSb(dImaCSf);
  Image<PixRGB<byte> > dImaCS = makeRGB(dImaCSb,dImaCSb,dImaCSb);
  Image<PixRGB<byte> > tdImaCS(dIma+zoomXY(dImaCS,GRID_SIZE));
  inplacePaste (disp, tdImaCS, Point2D<int>(width,0));

  Point2D<int> noff (width,0);
  drawCross(disp, pt+noff, PixRGB<byte>(255,0,0), 10, 1);
 
  if(itsCSrectangle.isValid())
    {
      drawRect(disp, itsCSrectangle*GRID_SIZE,        PixRGB<byte>(255,0,0), 1);
      drawRect(disp, (itsCSrectangle*GRID_SIZE)+noff, PixRGB<byte>(255,0,0), 1);
    }

  itsWin->drawImage(disp,0,0);
  Raster::waitForKey();
}