Advertisement
Guest User

Untitled

a guest
Oct 28th, 2015
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #include <opencv2/objdetect/objdetect.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/imgproc/imgproc.hpp>
  4.  
  5. #include <iostream>
  6. #include <queue>
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #include "constants.h"
  11. #include "findEyeCenter.h"
  12. #include "findEyeCorner.h"
  13.  
  14.  
  15. /** Constants **/
  16.  
  17.  
  18. /** Function Headers */
  19. void detectAndDisplay( cv::Mat frame );
  20.  
  21. /** Global variables */
  22. //-- Note, either copy these two files from opencv/data/haarscascades to your current folder, or change these locations
  23. cv::String face_cascade_name = "haarcascade_frontalface_alt.xml";
  24. cv::CascadeClassifier face_cascade;
  25. std::string main_window_name = "Capture - Face detection";
  26. std::string face_window_name = "Capture - Face";
  27. cv::RNG rng(12345);
  28. cv::Mat debugImage;
  29. cv::Mat skinCrCbHist = cv::Mat::zeros(cv::Size(256, 256), CV_8UC1);
  30.  
  31. /**
  32. * @function main
  33. */
  34. int main( ) {
  35.  
  36. cv::Mat frame;
  37.  
  38. // Load the cascades
  39. if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade, please change face_cascade_name in source code.\n"); return -1; };
  40.  
  41. cv::namedWindow(main_window_name,CV_WINDOW_NORMAL);
  42. cv::moveWindow(main_window_name, 400, 100);
  43. cv::namedWindow(face_window_name,CV_WINDOW_NORMAL);
  44. cv::moveWindow(face_window_name, 10, 100);
  45. cv::namedWindow("Right Eye",CV_WINDOW_NORMAL);
  46. cv::moveWindow("Right Eye", 10, 600);
  47. cv::namedWindow("Left Eye",CV_WINDOW_NORMAL);
  48. cv::moveWindow("Left Eye", 10, 800);
  49. cv::namedWindow("aa",CV_WINDOW_NORMAL);
  50. cv::moveWindow("aa", 10, 800);
  51. cv::namedWindow("aaa",CV_WINDOW_NORMAL);
  52. cv::moveWindow("aaa", 10, 800);
  53.  
  54. createCornerKernels();
  55. ellipse(skinCrCbHist, cv::Point(113, 155.6), cv::Size(23.4, 15.2),
  56. 43.0, 0.0, 360.0, cv::Scalar(255, 255, 255), -1);
  57.  
  58. // Read the video stream
  59. capture = cvCaptureFromCAM( 0 );
  60.  
  61. if( capture)
  62.  
  63. {
  64. while( true )
  65.  
  66. {
  67.  
  68. frame = cvQueryFrame( capture );
  69. // mirror it
  70. imshow("Video",frame);
  71. cv::flip(frame, frame, 1);
  72. frame.copyTo(debugImage);
  73.  
  74. // Apply the classifier to the frame
  75. if( !frame.empty() ) {
  76. detectAndDisplay( frame );
  77. }
  78. else {
  79. printf(" --(!) No captured frame -- Break!");
  80. break;
  81. }
  82.  
  83. imshow(main_window_name,debugImage);
  84.  
  85. int c = cv::waitKey(10);
  86. if( (char)c == 'c' ) { break; }
  87. if( (char)c == 'f' ) {
  88. imwrite("frame.png",frame);
  89. }
  90.  
  91. }
  92. }
  93.  
  94. releaseCornerKernels();
  95.  
  96. return 0;
  97. }
  98.  
  99. void findEyes(cv::Mat frame_gray, cv::Rect face) {
  100. cv::Mat faceROI = frame_gray(face);
  101. cv::Mat debugFace = faceROI;
  102.  
  103. if (kSmoothFaceImage) {
  104. double sigma = kSmoothFaceFactor * face.width;
  105. GaussianBlur( faceROI, faceROI, cv::Size( 0, 0 ), sigma);
  106. }
  107. //-- Find eye regions and draw them
  108. int eye_region_width = face.width * (kEyePercentWidth/100.0);
  109. int eye_region_height = face.width * (kEyePercentHeight/100.0);
  110. int eye_region_top = face.height * (kEyePercentTop/100.0);
  111. cv::Rect leftEyeRegion(face.width*(kEyePercentSide/100.0),
  112. eye_region_top,eye_region_width,eye_region_height);
  113. cv::Rect rightEyeRegion(face.width - eye_region_width - face.width*(kEyePercentSide/100.0),
  114. eye_region_top,eye_region_width,eye_region_height);
  115.  
  116. //-- Find Eye Centers
  117. cv::Point leftPupil = findEyeCenter(faceROI,leftEyeRegion,"Left Eye");
  118. cv::Point rightPupil = findEyeCenter(faceROI,rightEyeRegion,"Right Eye");
  119. // get corner regions
  120. cv::Rect leftRightCornerRegion(leftEyeRegion);
  121. leftRightCornerRegion.width -= leftPupil.x;
  122. leftRightCornerRegion.x += leftPupil.x;
  123. leftRightCornerRegion.height /= 2;
  124. leftRightCornerRegion.y += leftRightCornerRegion.height / 2;
  125. cv::Rect leftLeftCornerRegion(leftEyeRegion);
  126. leftLeftCornerRegion.width = leftPupil.x;
  127. leftLeftCornerRegion.height /= 2;
  128. leftLeftCornerRegion.y += leftLeftCornerRegion.height / 2;
  129. cv::Rect rightLeftCornerRegion(rightEyeRegion);
  130. rightLeftCornerRegion.width = rightPupil.x;
  131. rightLeftCornerRegion.height /= 2;
  132. rightLeftCornerRegion.y += rightLeftCornerRegion.height / 2;
  133. cv::Rect rightRightCornerRegion(rightEyeRegion);
  134. rightRightCornerRegion.width -= rightPupil.x;
  135. rightRightCornerRegion.x += rightPupil.x;
  136. rightRightCornerRegion.height /= 2;
  137. rightRightCornerRegion.y += rightRightCornerRegion.height / 2;
  138. rectangle(debugFace,leftRightCornerRegion,200);
  139. rectangle(debugFace,leftLeftCornerRegion,200);
  140. rectangle(debugFace,rightLeftCornerRegion,200);
  141. rectangle(debugFace,rightRightCornerRegion,200);
  142. // change eye centers to face coordinates
  143. rightPupil.x += rightEyeRegion.x;
  144. rightPupil.y += rightEyeRegion.y;
  145. leftPupil.x += leftEyeRegion.x;
  146. leftPupil.y += leftEyeRegion.y;
  147. // draw eye centers
  148. circle(debugFace, rightPupil, 3, 1234);
  149. circle(debugFace, leftPupil, 3, 1234);
  150.  
  151. //-- Find Eye Corners
  152. if (kEnableEyeCorner) {
  153. cv::Point2f leftRightCorner = findEyeCorner(faceROI(leftRightCornerRegion), true, false);
  154. leftRightCorner.x += leftRightCornerRegion.x;
  155. leftRightCorner.y += leftRightCornerRegion.y;
  156. cv::Point2f leftLeftCorner = findEyeCorner(faceROI(leftLeftCornerRegion), true, true);
  157. leftLeftCorner.x += leftLeftCornerRegion.x;
  158. leftLeftCorner.y += leftLeftCornerRegion.y;
  159. cv::Point2f rightLeftCorner = findEyeCorner(faceROI(rightLeftCornerRegion), false, true);
  160. rightLeftCorner.x += rightLeftCornerRegion.x;
  161. rightLeftCorner.y += rightLeftCornerRegion.y;
  162. cv::Point2f rightRightCorner = findEyeCorner(faceROI(rightRightCornerRegion), false, false);
  163. rightRightCorner.x += rightRightCornerRegion.x;
  164. rightRightCorner.y += rightRightCornerRegion.y;
  165. circle(faceROI, leftRightCorner, 3, 200);
  166. circle(faceROI, leftLeftCorner, 3, 200);
  167. circle(faceROI, rightLeftCorner, 3, 200);
  168. circle(faceROI, rightRightCorner, 3, 200);
  169. }
  170.  
  171. imshow(face_window_name, faceROI);
  172. // cv::Rect roi( cv::Point( 0, 0 ), faceROI.size());
  173. // cv::Mat destinationROI = debugImage( roi );
  174. // faceROI.copyTo( destinationROI );
  175. }
  176.  
  177.  
  178. cv::Mat findSkin (cv::Mat &frame) {
  179. cv::Mat input;
  180. cv::Mat output = cv::Mat(frame.rows,frame.cols, CV_8U);
  181.  
  182. cvtColor(frame, input, CV_BGR2YCrCb);
  183.  
  184. for (int y = 0; y < input.rows; ++y) {
  185. const cv::Vec3b *Mr = input.ptr<cv::Vec3b>(y);
  186. // uchar *Or = output.ptr<uchar>(y);
  187. cv::Vec3b *Or = frame.ptr<cv::Vec3b>(y);
  188. for (int x = 0; x < input.cols; ++x) {
  189. cv::Vec3b ycrcb = Mr[x];
  190. // Or[x] = (skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) > 0) ? 255 : 0;
  191. if(skinCrCbHist.at<uchar>(ycrcb[1], ycrcb[2]) == 0) {
  192. Or[x] = cv::Vec3b(0,0,0);
  193. }
  194. }
  195. }
  196. return output;
  197. }
  198.  
  199. /**
  200. * @function detectAndDisplay
  201. */
  202. void detectAndDisplay( cv::Mat frame ) {
  203. std::vector<cv::Rect> faces;
  204. //cv::Mat frame_gray;
  205.  
  206. std::vector<cv::Mat> rgbChannels(3);
  207. cv::split(frame, rgbChannels);
  208. cv::Mat frame_gray = rgbChannels[2];
  209.  
  210. //cvtColor( frame, frame_gray, CV_BGR2GRAY );
  211. //equalizeHist( frame_gray, frame_gray );
  212. //cv::pow(frame_gray, CV_64F, frame_gray);
  213. //-- Detect faces
  214. face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150, 150) );
  215. // findSkin(debugImage);
  216.  
  217. for( int i = 0; i < faces.size(); i++ )
  218. {
  219. rectangle(debugImage, faces[i], 1234);
  220. }
  221. //-- Show what you got
  222. if (faces.size() > 0) {
  223. findEyes(frame_gray, faces[0]);
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement