Advertisement
Guest User

OpenCV

a guest
Oct 20th, 2012
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/objdetect/objdetect.hpp>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. using namespace cv;
  8.  
  9. const String haarcascade_face = "haarcascade_frontalface_alt2.xml";
  10. const String window_name_original = "OpenCV Demo Camera Face Detection - Original";
  11. const String window_name = "OpenCV Demo Camera Face Detection";
  12.  
  13. int main(int, char**)
  14. {
  15.  
  16.     Mat frame;
  17.  
  18.     VideoCapture capture;
  19.  
  20.     CascadeClassifier cascade;
  21.  
  22.     if(! cascade.load(haarcascade_face))
  23.     {
  24.  
  25.         cout << "Could not open the Haar Cascade file!" << endl;
  26.  
  27.         return -1;
  28.  
  29.     }
  30.  
  31.     if(! capture.open(0))
  32.     {
  33.  
  34.         cout << "Could not open or find the camera!" << endl;
  35.  
  36.         return -1;
  37.  
  38.     }
  39.  
  40.     namedWindow(window_name, CV_WINDOW_AUTOSIZE);
  41.  
  42.     namedWindow(window_name_original, CV_WINDOW_AUTOSIZE);
  43.  
  44.     for(;;)
  45.     {
  46.  
  47.         capture >> frame;
  48.  
  49.         if(! frame.data) break;
  50.  
  51.         imshow(window_name_original, frame);
  52.  
  53.         vector<Rect> faces;
  54.  
  55.         cascade.detectMultiScale(
  56.                     frame,
  57.                     faces,
  58.                     1.1,
  59.                     3,
  60.                     0,
  61.                     Size(20, 20));
  62.  
  63.         vector<Rect>::const_iterator i;
  64.  
  65.         for(i = faces.begin(); i != faces.end(); i++)
  66.         {
  67.  
  68.             rectangle(frame, Point(i->x, i->y), Point(i->x + i->width, i->y + i->height), CV_RGB(255, 0, 0), 2);
  69.  
  70.         }
  71.  
  72.         imshow(window_name, frame);
  73.  
  74.         char c = cvWaitKey(1);
  75.  
  76.         if(c == 27) break;
  77.  
  78.     }
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement