Search

2012年5月4日 星期五

Real Time Face Detection - wxTimer

    人臉檢測的演算法可以參考這篇很多人引用的論文:『Robust Real-Time Face Detection
大致上就是先做Feature Extraction然後做Cascade Detection,也就是先取得特徵然後一步一步檢查直到最後才能判定是否為人臉,Feature在論文裡面有四種形式,然而有可能檢測對象會是歪頭之類的狀態,因此就必須要讓檢測視窗(四種特徵圖內的矩形)在搜尋視窗(包圍四種特徵圖內的矩形的方形)中改變角度搜尋。

    而用OpenCV可以用cvHaarDetectObjects來作人臉檢測,可以參考這邊有詳細的cvHaarDetectObjects參數說明。



RTFD.h

#include <wx/wx.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

enum{
   ID_SCREEN1 = 100,
   ID_SCREEN2,
   ID_START,
   ID_STOP,
   ID_TIMER
};

class App:public wxApp
{
   public:
      bool OnInit();
};

class Frame:public wxFrame
{
   public:
      Frame(const wxString&);
      ~Frame();

   private:
      wxPanel *screen;
      wxButton *start;
      wxButton *stop;
      wxTimer timer;

      CvCapture *camera;
      IplImage *frame;
      CvVideoWriter *video_writer;

      CvHaarClassifierCascade *classifier;
      CvMemStorage *face_storage;
      CvSeq *faces;
      int min_face_width;
      int min_face_height;
      wxString cascade_name;

      void CreateUI();
      void OnPaint(wxPaintEvent&);
      void OnTimer(wxTimerEvent&);
      void OnStart(wxCommandEvent&);
      void OnStop(wxCommandEvent&);
      void OnCapture(wxCommandEvent&);
      void OnExit(wxCommandEvent&);

      void FindFace();

      DECLARE_EVENT_TABLE()
};


RTFD.cpp


#include "RTFD.h"

IMPLEMENT_APP(App)
DECLARE_APP(App)

BEGIN_EVENT_TABLE(Frame,wxFrame)
   EVT_MENU(wxID_EXIT,Frame::OnExit)
   EVT_PAINT(Frame::OnPaint)
   EVT_TIMER(ID_TIMER,Frame::OnTimer)
   EVT_BUTTON(ID_START,Frame::OnStart)
   EVT_BUTTON(ID_STOP,Frame::OnStop)
END_EVENT_TABLE()

bool App::OnInit()
{
   wxFrame *frame = new Frame(wxT("Real Time Face Detection"));

   frame->Show(true);

   return true;
}

Frame::Frame(const wxString &title):wxFrame(NULL,wxID_ANY,title,wxDefaultPosition,wxSize(800,600),wxMINIMIZE_BOX | wxCLOSE_BOX | wxCAPTION | wxSYSTEM_MENU),timer(this,ID_TIMER)
{
   timer.Stop();

   wxInitAllImageHandlers();

   CreateUI();

   camera = NULL;
   frame = NULL;

   video_writer = cvCreateVideoWriter("video.avi",CV_FOURCC('P','I','M','1'),20,cvSize(640,480),1);

   min_face_width = 110;
   min_face_height = 110;

   cascade_name = wxT("./haarcascades/haarcascade_frontalface_alt.xml");
   classifier = (CvHaarClassifierCascade*)cvLoad(cascade_name.mb_str(),0,0,0);

   if(!classifier){
      wxMessageBox(wxT("Classifier Error!"));
      Close();
   }

   face_storage = cvCreateMemStorage(0);
   cvClearMemStorage(face_storage);
}

Frame::~Frame()
{
}

void Frame::CreateUI()
{
   wxMenu *file = new wxMenu;

   file->Append(wxID_EXIT,wxT("E&xit\tAlt-e"),wxT("Exit"));

   wxMenuBar *bar = new wxMenuBar;

   bar->Append(file,wxT("File"));
   SetMenuBar(bar);

   CreateStatusBar(2);
   SetStatusText(wxT("wxCV"));

   wxBoxSizer *top = new wxBoxSizer(wxVERTICAL);
   this->SetSizer(top);

   wxBoxSizer *screen_box = new wxBoxSizer(wxHORIZONTAL);
   top->Add(screen_box,0,wxALIGN_CENTER_HORIZONTAL | wxALL,5);

   screen = new wxPanel(this,ID_SCREEN1,wxDefaultPosition,wxSize(800,500));
   screen_box->Add(screen,0,wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL,5);

   wxBoxSizer *button_box = new wxBoxSizer(wxHORIZONTAL);
   top->Add(button_box,0,wxALIGN_CENTER_HORIZONTAL | wxALL,5);

   start = new wxButton(this,ID_START,wxT("&Start"),wxDefaultPosition,wxDefaultSize);
   button_box->Add(start,0,wxALIGN_CENTER_VERTICAL | wxALL,5);

   stop = new wxButton(this,ID_STOP,wxT("&Stop"),wxDefaultPosition,wxDefaultSize);
   button_box->Add(stop,0,wxALIGN_CENTER_VERTICAL | wxALL,5);
}

void Frame::OnPaint(wxPaintEvent &event)
{
   screen->Refresh();
   screen->Update();
}

void Frame::OnExit(wxCommandEvent &event)
{
   timer.Stop();

   if(frame){
      cvReleaseImage(&frame);
   }
   if(camera){
      cvReleaseCapture(&camera);
   }
   if(video_writer){
      cvReleaseVideoWriter(&video_writer);
   }
   if(classifier){
      cvReleaseHaarClassifierCascade(&classifier);
   }
   if(face_storage){
      cvReleaseMemStorage(&face_storage);
   }

   Close();
}

void Frame::OnStart(wxCommandEvent &event)
{
   camera = cvCaptureFromCAM(-1);

   if(!camera){
      wxMessageBox(wxT("Camera Error!"),wxT("Error"),wxOK,this);
   }
   else{
      timer.Start(40);
   }
}

void Frame::OnStop(wxCommandEvent &event)
{
   timer.Stop();

   if(camera){
      cvReleaseCapture(&camera);
   }
}

void Frame::OnTimer(wxTimerEvent &event)
{
   wxClientDC dc(screen);

   frame = cvQueryFrame(camera);

   FindFace();

   cvWriteFrame(video_writer,frame);

   cvConvertImage(frame,frame,CV_CVTIMG_SWAP_RB);

   unsigned char *data;

   cvGetRawData(frame,&data);

   wxImage *image = new wxImage(frame->width,frame->height,data,true);

   wxBitmap *bitmap = new wxBitmap(*image);

   int x,y,width,height;

   dc.GetClippingBox(&x,&y,&width,&height);
   dc.DrawBitmap(*bitmap,x,y);

   delete image;
   delete bitmap;
}

void Frame::FindFace()
{
   faces = cvHaarDetectObjects(frame,classifier,face_storage,
1.1,3,
CV_HAAR_DO_CANNY_PRUNING,
cvSize(min_face_width,min_face_height));

   if(faces){
      for(int i = 0;i < faces->total;++i){
CvPoint p1,p2;

CvRect *rect = (CvRect*)cvGetSeqElem(faces,i);

p1.x = rect->x;
p1.y = rect->y;
p2.x = rect->x + rect->width;
p2.y = rect->y + rect->height;

cvRectangle(frame,p1,p2,CV_RGB(255,0,0),3,8,0);
      }
   }
}

因為我沒有修過相關的課程,所以如果有名詞或觀念錯誤請指教:)

Download

沒有留言:

張貼留言