SOL9 Sample: SolVideoObjectDetector

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolVideoObjectDetector.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/04/10

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/FolderBrowser.h>
#include <sol/DropFiles.h>
#include <sol/Label.h>
#include <sol/ComboBox.h>
#include <sol/Profile.h>
#include <sol/StringT.h>
#include <sol/opencv/OpenCVObject.h>
#include <sol/opencv/OpenCVVideoApplication.h>
#include <sol/opencv/OpenCVVideoCaptureView.h>
#include <sol/opencv/OpenCVImageView.h>

#include <sol/PushButton.h>
#include <sol/FileDialog.h>
#include <vector>
#include "Resource.h"

namespace SOL {

class MainView :public OpenCVVideoCaptureView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  class VideoImageView :public OpenCVImageView {
  private:
    cv::Mat videoImage;

    CascadeClassifier classifier;
    bool              classifierLoaded;
    
    cv::Mat& getMat()
    {
      return videoImage;
    }
    
  public:  
    
    void display()
    {
      show(videoImage);
    }
    
  public:
    VideoImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    ,classifierLoaded(false)
    {
    }

    void setImage(cv::Mat& image)
    {
      videoImage = image;
    }

    void loadClassifier(const char* path)
    {
      classifierLoaded = false;
      try {
          classifier.load(path);
        classifierLoaded = true;
      } catch (SOL::Exception& ex) {
      }
    }
    
    void detect()
    {
      if (classifierLoaded == false) {
        display();
        return;
      }
      
      try {
       
          cv::vector<Rect> faces;
          classifier.detectMultiScale(videoImage, faces, 1.1, 3, 0, cv::Size(20,20));

        for (int i = 0; i < faces.size(); i++){
              rectangle(videoImage, Point(faces[i].x,                 faces[i].y), 
                               Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
                               Scalar(0, 200, 0), 3, CV_AA);
          }
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
  };
  //Inner class endss
  ////////////////////////////////////////////////////////////////////////////////////////
  
  SmartPtr<VideoImageView>    view;
  
  SmartPtr<Label>         label;
  SmartPtr<ComboBox>      comboBox;
  
  SmartPtr<FolderBrowser> folderBrowser;
  SmartPtr<PushButton>    classifierButton;
  SmartPtr<PushButton>    detectButton;
  String                  selectedFolder;
  
  void resize(int w, int h)
  {
    if (label && comboBox && view && detectButton) {
      classifierButton-> reshape(  w-100+10,   4,     80,   28);
      label           -> reshape(         0,   4,    100,   28);
      comboBox        -> reshape(        90,   4,  w-190,  120);
      view            -> reshape(         0,  40,  w-100, h-45);

      detectButton    -> reshape(w-100 + 10, 140,     80,    28);      
    }
  }
  
  void classifierFolder(Action& action)
  {
    if (folderBrowser->show(selectedFolder)) {        
      char pattern[MAX_PATH];
      sprintf_s(pattern, sizeof(pattern), "%s\\*.xml", (const char*)selectedFolder);
      comboBox->clear();

      comboBox->findFiles(pattern);
      comboBox->setCurSel(0);
    }
  }

  void detect(Action& action)
  {
    try {
      char path[MAX_PATH];
      String filterName  = comboBox->getCurrentSelection();
    
      sprintf_s(path, sizeof(path), "%s\\%s", 
              (const char*)selectedFolder, (const char*)filterName);
      if (view) {
        view->loadClassifier(path);
        view->detect();
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      exit(action);
    }
  }
  
public:
  MainView(OpenCVApplication& applet, const char* name, Args& args)
  :OpenCVVideoCaptureView(applet, name, args)
  {
    try {
      Args ar;
      label = new Label(this, "Classifier", ar);
      
      ar.reset();
      classifierButton = new PushButton(this, "...", ar);
      classifierButton -> addCallback(XmNactivateCallback, this, 
        (Callback)&MainView::classifierFolder, NULL); 

      ar.reset();
      ar.set(XmNstyle, CBS_SORT|CBS_DROPDOWNLIST);
      comboBox = new ComboBox(this, "", ar);
    
      ar.reset();
      view = new VideoImageView(this, "cvwindow", ar); 
      
      addCallback(XmNmenuCallback, IDM_FILTER, this,
        (Callback)&MainView::classifierFolder, NULL);

      ar.reset();
      detectButton = new PushButton(this, "Detect", ar);
      detectButton -> addCallback(XmNactivateCallback, this, 
          (Callback)&MainView::detect, NULL); 
    
      ar.reset();
      ar.set(XmNstartingDirectory, "C:\\dev\\opencv3.2");
      folderBrowser = new FolderBrowser(this, "FolderBrowser", ar);
    
      addCallback(XmNmenuCallback, IDM_OPEN, this,
          (Callback)&MainView::open, NULL);
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);
      
      setImageViewSize(view);  
            
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }

  //This method will be called from a OpenCVTimerThread::run.
  virtual void render()
  {
    cv::Mat frame;
    if (readVideoFrame(frame)) {
      if (!frame.empty() && view) {
        view -> setImage(frame);
        view -> detect();
      }
    }
  }
};

}

//
void main(int argc, char** argv) 
{
  try {
    ModuleFileName module(argv[0]);
    
    int capturingInterval = 40; //msec
    const char*  name = module.getAppName();
    
    OpenCVVideoApplication applet(name, argc, argv, capturingInterval);

    Args args;
    args.set(XmNwidth,  700);
    args.set(XmNheight, 500);
    args.set(XmNvideoDeviceIndex, 0);

    args.set(XmNcaptureAutoStart, true);
    
    MainView view(applet, name, args);
    view.realize();

    applet.run(view);
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.