Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. void face_train()
  2. {
  3.  
  4. // Training Images for me
  5. images.push_back(imread("me/me1.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
  6. images.push_back(imread("me/me2.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
  7. images.push_back(imread("me/me3.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
  8. // Training Images for Brad Pitt
  9. images.push_back(imread("Pitt/Brad_Pitt_1.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
  10. images.push_back(imread("Pitt/Brad_Pitt_2.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
  11. images.push_back(imread("Pitt/Brad_Pitt_3.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
  12. }
  13.  
  14. void face_detect()
  15. {
  16. Mat image1;
  17. Mat gray;
  18.  
  19.  
  20.  
  21. //G E T I M A G E
  22.  
  23. if ( r_c % 2 == 0 )
  24. image1 = imread("Cam_Pic1.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
  25. else if ( r_c % 2 != 0 )
  26. image1 = imread("Cam_Pic2.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
  27.  
  28.  
  29. if (image1.empty())
  30. {
  31. cout << "!!! Failed imread(): image not found" << endl;
  32. }
  33.  
  34.  
  35. // D E T E C T F A C E
  36.  
  37. cvtColor(image1, gray, CV_BGR2GRAY);
  38. //equalizeHist(gray, gray);
  39.  
  40.  
  41. vector<Rect_<int>> faces;
  42.  
  43. face_cascade.load("haarcascade_frontalface_alt.xml"); // xml file with facial features algorithm
  44. face_cascade.detectMultiScale(gray, faces);
  45.  
  46. // T R A I N R E C O G N I Z E R
  47.  
  48. face_train();
  49.  
  50. int im_width = images[0].cols;
  51. int im_height = images[0].rows;
  52.  
  53. model_fisher = createFisherFaceRecognizer();
  54. model_fisher->train(images, labels);
  55.  
  56. Mat original = image1.clone();
  57.  
  58. // look for all faces on the image
  59. for(unsigned int i = 0; i < faces.size(); i++)
  60. {
  61.  
  62. Rect face_i = faces[i];
  63. Mat face = gray(face_i);
  64.  
  65. Mat face_resized;
  66. resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
  67.  
  68. int prediction = model_fisher->predict(face_resized);
  69.  
  70. // Draw rectangle around faces
  71. rectangle(original , face_i, CV_RGB(0, 255,0), 1);
  72.  
  73. //Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
  74. //Point pt2(faces[i].x, faces[i].y);
  75.  
  76. string box_text = format("Match found = %d", prediction);
  77. //string box_text = "Match Found: Alan Alban";
  78. // text position and output
  79. int pos_x = max(face_i.tl().x - 4, 0);
  80. int pos_y = max(face_i.tl().y - 4, 0);
  81.  
  82. putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
  83. }
  84.  
  85. // S H O W I M A G E
  86.  
  87. if ( r_c % 2 == 0 )
  88. imwrite("Detected_image1.jpeg", image1);
  89. else if ( r_c % 2 != 0 )
  90. imwrite("Detected_image2.jpeg", image1);
  91.  
  92. r_c++;
  93.  
  94. }
  95.  
  96. model_fisher->train(images, labels);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement