objectDetection.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "opencv2/objdetect.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include "opencv2/videoio.hpp"
  5. #include <iostream>
  6. using namespace std;
  7. using namespace cv;
  8. /** Function Headers */
  9. void detectAndDisplay( Mat frame );
  10. /** Global variables */
  11. CascadeClassifier face_cascade;
  12. CascadeClassifier eyes_cascade;
  13. /** @function main */
  14. int main( int argc, const char** argv )
  15. {
  16. CommandLineParser parser(argc, argv,
  17. "{help h||}"
  18. "{face_cascade|data/haarcascades/haarcascade_frontalface_alt.xml|Path to face cascade.}"
  19. "{eyes_cascade|data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|Path to eyes cascade.}"
  20. "{camera|0|Camera device number.}");
  21. parser.about( "\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
  22. "You can use Haar or LBP features.\n\n" );
  23. parser.printMessage();
  24. String face_cascade_name = samples::findFile( parser.get<String>("face_cascade") );
  25. String eyes_cascade_name = samples::findFile( parser.get<String>("eyes_cascade") );
  26. //-- 1. Load the cascades
  27. if( !face_cascade.load( face_cascade_name ) )
  28. {
  29. cout << "--(!)Error loading face cascade\n";
  30. return -1;
  31. };
  32. if( !eyes_cascade.load( eyes_cascade_name ) )
  33. {
  34. cout << "--(!)Error loading eyes cascade\n";
  35. return -1;
  36. };
  37. int camera_device = parser.get<int>("camera");
  38. VideoCapture capture;
  39. //-- 2. Read the video stream
  40. capture.open( camera_device );
  41. if ( ! capture.isOpened() )
  42. {
  43. cout << "--(!)Error opening video capture\n";
  44. return -1;
  45. }
  46. Mat frame;
  47. while ( capture.read(frame) )
  48. {
  49. if( frame.empty() )
  50. {
  51. cout << "--(!) No captured frame -- Break!\n";
  52. break;
  53. }
  54. //-- 3. Apply the classifier to the frame
  55. detectAndDisplay( frame );
  56. if( waitKey(10) == 27 )
  57. {
  58. break; // escape
  59. }
  60. }
  61. return 0;
  62. }
  63. /** @function detectAndDisplay */
  64. void detectAndDisplay( Mat frame )
  65. {
  66. Mat frame_gray;
  67. cvtColor( frame, frame_gray, COLOR_BGR2GRAY );
  68. equalizeHist( frame_gray, frame_gray );
  69. //-- Detect faces
  70. std::vector<Rect> faces;
  71. face_cascade.detectMultiScale( frame_gray, faces );
  72. for ( size_t i = 0; i < faces.size(); i++ )
  73. {
  74. Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
  75. ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4 );
  76. Mat faceROI = frame_gray( faces[i] );
  77. //-- In each face, detect eyes
  78. std::vector<Rect> eyes;
  79. eyes_cascade.detectMultiScale( faceROI, eyes );
  80. for ( size_t j = 0; j < eyes.size(); j++ )
  81. {
  82. Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
  83. int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
  84. circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4 );
  85. }
  86. }
  87. //-- Show what you got
  88. imshow( "Capture - Face detection", frame );
  89. }