コード例 #1
0
ファイル: camExample.cpp プロジェクト: Overdr0ne/motld
void* Run(cv::VideoCapture& capture)
{
  int size = ivWidth*ivHeight;
  int count = 1;
  DebugInfo dbgInfo;
  cv::CascadeClassifier cascade;
  std::vector<cv::Rect> detectedFaces;
  std::vector<ObjectBox> trackBoxes;
  ObjectBox detectBox;

  // Initialize MultiObjectTLD
  #if LOADCLASSIFIERATSTART
  MultiObjectTLD p = MultiObjectTLD::loadClassifier((char*)CLASSIFIERFILENAME);
  #else
  MOTLDSettings settings(COLOR_MODE_RGB);
  settings.useColor = false;
  MultiObjectTLD p(ivWidth, ivHeight, settings);
  #endif

  if(cascadePath != "")
    cascade.load( cascadePath );

  Matrix maRed;
  Matrix maGreen;
  Matrix maBlue;
  unsigned char img[size*3];
  while(!ivQuit)
  {
    /*
    if(reset){
      p = *(new MultiObjectTLD(ivWidth, ivHeight, COLOR_MODE_RGB));
      reset = false;
    }
    if(load){
      p = MultiObjectTLD::loadClassifier(CLASSIFIERFILENAME);
      load = false;
    }
    */

#if TIMING
    c_end = std::clock();
    std::cout << "Total: " << (c_end-c_start) << std::endl;
    c_start = std::clock();
#endif

    // Grab an image
    if(!capture.grab()){
      std::cout << "error grabbing frame" << std::endl;
      break;
    }
    cv::Mat frame;
    capture.retrieve(frame);
    frame.copyTo(curImage);
    //BGR to RGB
    // for(int j = 0; j<size; j++){
    //   img[j] = frame.at<cv::Vec3b>(j).val[2];
    //   img[j+size] = frame.at<cv::Vec3b>(j).val[1];
    //   img[j+2*size] = frame.at<cv::Vec3b>(j).val[0];
    // }
#if TIMING
    c_start1 = std::clock();
#endif
    for(int i = 0; i < ivHeight; ++i){
      for(int j = 0; j < ivWidth; ++j){
        img[i*ivWidth+j] = curImage.at<cv::Vec3b>(i,j).val[2];
        img[i*ivWidth+j+size] = curImage.at<cv::Vec3b>(i,j).val[1];
        img[i*ivWidth+j+2*size] = curImage.at<cv::Vec3b>(i,j).val[0];
      }
    }
#if TIMING
    c_end1 = std::clock();
    std::cout << "time1: " << (c_end1-c_start1) << std::endl;
#endif

    // for(int i = 0; i < ivHeight; ++i){
    //   for(int j = 0; j < ivWidth; ++j){
    //     curImage.at<cv::Vec3b>(i,j).val[2] = 0;
    //     curImage.at<cv::Vec3b>(i,j).val[1] = 0;
    //     curImage.at<cv::Vec3b>(i,j).val[0] = 0;
    //   }
    // }
    // cv::imshow("MOCTLD", curImage);

#if TIMING
    c_start2 = std::clock();
#endif
    // Process it with motld
    p.processFrame(img);
#if TIMING
    c_end2 = std::clock();
    std::cout << "time2: " << (c_end2-c_start2) << std::endl;
#endif

    // Add new box
    if(mouseMode == MOUSE_MODE_ADD_BOX){
      p.addObject(mouseBox);
      mouseMode = MOUSE_MODE_IDLE;
    }

    if(mouseMode == MOUSE_MODE_ADD_GATE){
      p.addGate(gate);
      mouseMode = MOUSE_MODE_IDLE;
    }

    if(((count%20)==0) && cascadeDetect)
    {
      cascade.detectMultiScale( frame, detectedFaces,
        1.1, 2, 0
        //|CASCADE_FIND_BIGGEST_OBJECT
        //|CASCADE_DO_ROUGH_SEARCH
        |cv::CASCADE_SCALE_IMAGE,
        cv::Size(30, 30) );

      Ndetections = detectedFaces.size();

      for( std::vector<cv::Rect>::const_iterator r = detectedFaces.begin(); r != detectedFaces.end(); r++ )
      {
        detectBox.x = r->x;
        detectBox.y = r->y;
        detectBox.width = r->width;
        detectBox.height = r->height;
        if(p.isNewObject(detectBox))
          p.addObject(detectBox);
      }
      //printf("size detectedFaces: %i\n", detectedFaces.size());
    }
    count++;

    // Display result
    HandleInput();
    p.getDebugImage(img, maRed, maGreen, maBlue, drawMode);
#if TIMING
    c_start3 = std::clock();
#endif
    BGR2RGB(maRed, maGreen, maBlue);
#if TIMING
    c_end3 = std::clock();
    std::cout << "time3: " << (c_end3-c_start3) << std::endl;
#endif
    drawGate();
    drawMouseBox();
    dbgInfo.NObjects = p.getObjectTotal();
    dbgInfo.side0Cnt = p.getSide0Cnt();
    dbgInfo.side1Cnt = p.getSide1Cnt();
    writeDebug(dbgInfo);
    cv::imshow("MOCTLD", curImage);
    p.enableLearning(learningEnabled);
    if(save){
      p.saveClassifier((char*)CLASSIFIERFILENAME);
      save = false;
    }
  }
  //delete[] img;
  capture.release();
  return 0;
}
コード例 #2
0
ファイル: batchExample.cpp プロジェクト: damped/motld
int main(int argc, char *argv[])
{
  std::string input_folder = DEFAULT_INPUT, output_folder = DEFAULT_OUTPUT;
  if(argc >= 2)
  {
    input_folder = argv[1];
    if(argc >= 3)
    {
      output_folder = argv[2];
    }
  }
  std::cout << "input folder: " << input_folder << std::endl;
  
  struct dirent **filelist;
  int fcount = -1;
  bool gray = false;
  fcount = scandir(input_folder.c_str(), &filelist, ppm_select, alphasort);
  if (fcount <= 0)
  {
    fcount = scandir(input_folder.c_str(), &filelist, pgm_select, alphasort);
    gray = true;
  }
  if (fcount <= 0)
  {
    std::cout << "There are no .ppm or .pgm files in this folder! Maybe you have to convert the images first e.g. using" << std::endl
      << "  mogrify -format ppm *.jpg" << std::endl;
    return 0;
  }
  std::cout << "found " << fcount << " files" << std::endl;
  char filename[255];
  sprintf(filename, "%s/init.txt", input_folder.c_str());
  std::ifstream aStream(filename);
  if(!aStream || aStream.eof())
  {
    std::cout << "please create the file \"" << filename 
        << "\" specifying the initial bounding box[es] (x1,y1,x2,y2)" << std::endl;
    return 0;
  }
  char line[255];
  int x1,y1,x2,y2,imgid, width,height;
  std::vector<ObjectBox> boxes;
  while(aStream.getline(line,255))
  {
    x1 = y1 = x2 = y2 = imgid = 0;
    int i = 0;
    for(;line[i] >= '0' && line[i] <= '9'; i++)
      x1 = x1*10 + (line[i] - '0');
    for(i++;line[i] >= '0' && line[i] <= '9'; i++)
      y1 = y1*10 + (line[i] - '0');
    for(i++;line[i] >= '0' && line[i] <= '9'; i++)
      x2 = x2*10 + (line[i] - '0');
    for(i++;line[i] >= '0' && line[i] <= '9'; i++)
      y2 = y2*10 + (line[i] - '0');
    if(line[i] == ',')
      for(i++;line[i] >= '0' && line[i] <= '9'; i++)
        imgid = imgid*10 + (line[i] - '0'); 
    ObjectBox b = {x1,y1,x2-x1,y2-y1,imgid};
    boxes.push_back(b);
  }
  aStream.close();
  
  std::cout << "output folder: " << output_folder << std::endl;
  if (access(output_folder.c_str(), 0) != 0)
  {
    std::cout << "\tdoes not exist -> try to create it" << std::endl;
    if(system(("mkdir "+output_folder).c_str()))
    {
      std::cout << "\t failed to create directory" << std::endl;
      return 0;
    }
  }

  sprintf(filename, "%s/%s", input_folder.c_str(), filelist[0]->d_name);
  int z;
  unsigned char* dummy = gray ? readFromPGM<unsigned char>(filename, width, height) :
                                readFromPPM<unsigned char>(filename, width, height, z);
  delete[] dummy;
    
  // Initialize MultiObjectTLD
#if LOADCLASSIFIERATSTART
  MultiObjectTLD p = MultiObjectTLD::loadClassifier((char*)CLASSIFIERFILENAME);
#else
  MOTLDSettings settings(gray ? COLOR_MODE_GRAY : COLOR_MODE_RGB);
  MultiObjectTLD p(width, height, settings);
#endif
  
#if LEARNMODEOFF
  p.enableLearning(false);
#endif
  
  std::vector<ObjectBox> addBoxes;
  std::vector<ObjectBox>::iterator boxIt = boxes.begin();
  
  sprintf(filename, "%s/output.txt", output_folder.c_str());
  std::ofstream outStream(filename);  

  for (int i=0; i < fcount && (!MAX_FILE_NUMBER || i<MAX_FILE_NUMBER); ++i)
  { 
    // first load the image
    sprintf(filename, "%s/%s", input_folder.c_str(), filelist[i]->d_name);
    int xS, yS, z;
    unsigned char* img = gray ? readFromPGM<unsigned char>(filename, xS, yS) :
                                  readFromPPM<unsigned char>(filename, xS, yS, z);
    // then process it with MultiObjectTLD
    p.processFrame(img);
    
    while(boxIt != boxes.end() && boxIt->objectId == i)
    {
      addBoxes.push_back(*boxIt);
      boxIt++;
    }
    if(addBoxes.size() > 0){
      p.addObjects(addBoxes);
      addBoxes.clear();
    }

    #if OUTPUT_IMAGES>0
    // and save debug image to file
    sprintf(filename, "%s/%s", output_folder.c_str(), filelist[i]->d_name);
    p.writeDebugImage(img,filename);
    #endif
    
    // print current box to output file
    if(p.getValid())
    {
      ObjectBox b = p.getObjectBox();
      if(i > 0)
        outStream << std::endl;
      outStream << b.x << "," << b.y << "," << (b.x + b.width) << "," << (b.y + b.height);
    }
    else
      outStream << std::endl << "NaN,NaN,NaN,NaN";
    delete[] img;
  }
  outStream.close();

  std::cout << "MultiObjectTLD finished!" << std::endl;
#if SAVECLASSIFIERATEND
  std::cout << "Saving ..." << std::endl;
  p.saveClassifier((char*)CLASSIFIERFILENAME);
#endif
  for(int i = 0; i < fcount; i++)
    free(filelist[i]);
  free(filelist);
  
  return 0;
}
コード例 #3
0
ファイル: camExample.cpp プロジェクト: Hengjingkiat14/motld
void* Run(void*)
{
  int size = ivWidth*ivHeight;
    
  // Initialize MultiObjectTLD
  #if LOADCLASSIFIERATSTART
  MultiObjectTLD p = MultiObjectTLD::loadClassifier((char*)CLASSIFIERFILENAME);
  #else
  MOTLDSettings settings(COLOR_MODE_RGB);
  settings.useColor = false;
  MultiObjectTLD p(ivWidth, ivHeight, settings);
  #endif
  
  Matrix maRed;
  Matrix maGreen;
  Matrix maBlue;
  unsigned char img[size*3];
  #ifdef FORCE_RESIZING
  CvSize wsize = {ivWidth, ivHeight};
  IplImage* frame = cvCreateImage(wsize, IPL_DEPTH_8U, 3);
  #endif
  while(!ivQuit)
  {    
    /*
    if(reset){
      p = *(new MultiObjectTLD(ivWidth, ivHeight, COLOR_MODE_RGB));
      reset = false;
    }
    if(load){
      p = MultiObjectTLD::loadClassifier(CLASSIFIERFILENAME);
      load = false;
    }
    */
    
    // Grab an image
    if(!cvGrabFrame(capture)){
      std::cout << "error grabbing frame" << std::endl;
      break;
    }
    #ifdef FORCE_RESIZING
    IplImage* capframe = cvRetrieveFrame(capture);
    cvResize(capframe, frame);
    #else
    IplImage* frame = cvRetrieveFrame(capture);
    #endif
    for(int j = 0; j<size; j++){
      img[j] = frame->imageData[j*3+2];
      img[j+size] = frame->imageData[j*3+1];
      img[j+2*size] = frame->imageData[j*3];
    }
    
    // Process it with motld
    p.processFrame(img);
    
    // Add new box
    if(mouseMode == MOUSE_MODE_ADD_BOX){
      p.addObject(mouseBox);
      mouseMode = MOUSE_MODE_IDLE;
    }
    
    // Display result
    HandleInput();
    p.getDebugImage(img, maRed, maGreen, maBlue, drawMode);    
    FromRGB(maRed, maGreen, maBlue);
    cvShowImage("MOCTLD", curImage);
    p.enableLearning(learningEnabled);
    if(save){
      p.saveClassifier((char*)CLASSIFIERFILENAME);
      save = false;
    }
  }
  //delete[] img;
  cvReleaseCapture(&capture);
  return 0;
}