fork download
  1.  
  2. #include "opencv2\opencv.hpp"
  3.  
  4.  
  5.  
  6. #include <iostream>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10. using namespace cv;
  11.  
  12. /** Function Headers */
  13. void detectAndDisplay( Mat frame );
  14.  
  15. /** Global variables */
  16. String face_cascade_name = "haarcascade_frontalface_alt.xml";
  17. String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
  18. CascadeClassifier face_cascade;
  19. CascadeClassifier eyes_cascade;
  20. string window_name = "Capture - Face detection";
  21. RNG rng(12345);
  22.  
  23. /** @function main */
  24. int main( int argc, const char** argv )
  25. {
  26. VideoCapture capture;
  27. Mat frame;
  28.  
  29. //-- 1. Load the cascades
  30. if( !face_cascade.load( face_cascade_name ) )
  31. {
  32. printf("--(!)Error loading\n");
  33. return -1;
  34. };
  35. if( !eyes_cascade.load( eyes_cascade_name ) )
  36. {
  37. printf("--(!)Error loading\n");
  38. return -1;
  39. };
  40.  
  41. //-- 2. Read the video stream
  42. capture.open(0);
  43. if( capture.isOpened() )
  44. {
  45. while( true )
  46. {
  47. capture >> frame;
  48. //-- 3. Apply the classifier to the frame
  49. if( !frame.empty() )
  50. {
  51. detectAndDisplay( frame );
  52. }
  53. else
  54. {
  55. printf(" --(!) No captured frame -- Break!");
  56. break;
  57. }
  58.  
  59. int c = waitKey(10);
  60. if( (char)c == 'c' )
  61. {
  62. break;
  63. }
  64. }
  65. }
  66. return 0;
  67. }
  68.  
  69. /** @function detectAndDisplay */
  70. void detectAndDisplay( Mat frame )
  71. {
  72. std::vector<Rect> faces;
  73. Mat frame_gray;
  74.  
  75. cvtColor( frame, frame_gray, CV_BGR2GRAY );
  76. equalizeHist( frame_gray, frame_gray );
  77.  
  78. //-- Detect faces
  79. face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  80.  
  81. for( int i = 0; i < faces.size(); i++ )
  82. {
  83. Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
  84. ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
  85.  
  86. Mat faceROI = frame_gray( faces[i] );
  87. std::vector<Rect> eyes;
  88.  
  89. //-- In each face, detect eyes
  90. eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  91.  
  92. for( int j = 0; j < eyes.size(); j++ )
  93. {
  94. Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
  95. int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
  96. circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
  97. }
  98. }
  99. //-- Show what you got
  100. imshow( window_name, frame );
  101. }
  102.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty