Guest User

program with error

a guest
Apr 29th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #include <dlib/image_processing/frontal_face_detector.h>
  2. #include <dlib/image_processing/render_face_detections.h>
  3. #include <dlib/gui_widgets.h>
  4. #include <dlib/image_io.h>
  5. #include <iostream>
  6. #include <string> // for strings
  7. #include <iomanip> // for controlling float print precision
  8. #include <sstream> // string to number conversion
  9.  
  10. #include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
  11. #include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
  12. #include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
  13. #include <dlib/opencv.h>
  14. #include <opencv2/opencv.hpp>
  15.  
  16.  
  17.  
  18. using namespace std;
  19. using namespace cv;
  20. using namespace dlib;
  21.  
  22.  
  23. double getPSNR ( const Mat& I1, const Mat& I2);
  24. Scalar getMSSIM( const Mat& I1, const Mat& I2);
  25.  
  26. // ----------------------------------------------------------------------------------------
  27.  
  28. int main(int argc, char** argv)
  29. { VideoCapture camera;
  30. if(!camera.open(0))
  31. {cout<<"camera not open"; return 0;}
  32. Mat current;
  33. camera >> current;//reading current frame as earliest as possible
  34. Mat face;int i;
  35. string text="face";
  36.  
  37. Mat frameReference, frameUnderTest,next,graycurrent,graynext;array2d<unsigned char> img;
  38. double psnrV;
  39. Scalar mssimV;
  40.  
  41. while(true)
  42. { if( current.empty() ){cout<<"current frame empty"; break;}
  43.  
  44. camera>>next;
  45. cvtColor(current, graycurrent, CV_RGB2GRAY);
  46. cvtColor(next, graynext, CV_RGB2GRAY); // frame changed to gray
  47. frameReference=current;
  48. frameUnderTest=next;
  49. psnrV = getPSNR(frameReference,frameUnderTest);
  50. if(psnrV>17){//start face recog psnrV caluculated by experimenting on pairs of similar/still pics & pics with motion
  51. // cv::Mat cvimg = cv::imread("asd.jpg"); //cv added due error, code added from facedetect
  52. dlib::cv_image<rgb_pixel> cimg(frameUnderTest);
  53. imwrite("a.jpg",frameUnderTest);
  54.  
  55. frontal_face_detector detector = get_frontal_face_detector();
  56. //load_image(img,"a.jpg");
  57.  
  58. // pyramid_up(cimg);//inrcease image to detect smaller image
  59.  
  60. // Now tell the face detector to give us a list of bounding boxes
  61. // around all the faces it can find in the image.
  62. std::vector<rectangle> dets = detector(cimg);
  63. // Number of faces detected: dets.size()
  64.  
  65.  
  66. for(i=dets.size()-1;i>-1;i--){
  67. dlib::rectangle r = dets[i];
  68. Rect roi(r.left(), r.top(), r.width(), r.height());
  69. cv::Mat face = frameUnderTest(roi);
  70. string text="face";
  71. text += std::to_string(i);
  72. imwrite(text.append(".jpg"),face);
  73.  
  74. // program to crop the images with faces & face recog
  75.  
  76. }
  77.  
  78.  
  79. }
  80. else{ //motion not detected
  81. }
  82.  
  83. current=next;
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }
  92.  
  93. double getPSNR(const Mat& I1, const Mat& I2)
  94. {
  95. Mat s1;
  96. absdiff(I1, I2, s1); // |I1 - I2|
  97. s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
  98. s1 = s1.mul(s1); // |I1 - I2|^2
  99.  
  100. Scalar s = sum(s1); // sum elements per channel
  101.  
  102. double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
  103.  
  104. if( sse <= 1e-10) // for small values return zero
  105. return 0;
  106. else
  107. {
  108. double mse = sse / (double)(I1.channels() * I1.total());
  109. double psnr = 10.0 * log10((255 * 255) / mse);
  110. return psnr;
  111. }
  112. }
  113.  
  114. Scalar getMSSIM( const Mat& i1, const Mat& i2)
  115. {
  116. const double C1 = 6.5025, C2 = 58.5225;
  117. /***************************** INITS **********************************/
  118. int d = CV_32F;
  119.  
  120. Mat I1, I2;
  121. i1.convertTo(I1, d); // cannot calculate on one byte large values
  122. i2.convertTo(I2, d);
  123.  
  124. Mat I2_2 = I2.mul(I2); // I2^2
  125. Mat I1_2 = I1.mul(I1); // I1^2
  126. Mat I1_I2 = I1.mul(I2); // I1 * I2
  127.  
  128. /*************************** END INITS **********************************/
  129.  
  130. Mat mu1, mu2; // PRELIMINARY COMPUTING
  131. GaussianBlur(I1, mu1, Size(11, 11), 1.5);
  132. GaussianBlur(I2, mu2, Size(11, 11), 1.5);
  133.  
  134. Mat mu1_2 = mu1.mul(mu1);
  135. Mat mu2_2 = mu2.mul(mu2);
  136. Mat mu1_mu2 = mu1.mul(mu2);
  137.  
  138. Mat sigma1_2, sigma2_2, sigma12;
  139.  
  140. GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
  141. sigma1_2 -= mu1_2;
  142.  
  143. GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
  144. sigma2_2 -= mu2_2;
  145.  
  146. GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
  147. sigma12 -= mu1_mu2;
  148.  
  149. ///////////////////////////////// FORMULA ////////////////////////////////
  150. Mat t1, t2, t3;
  151.  
  152. t1 = 2 * mu1_mu2 + C1;
  153. t2 = 2 * sigma12 + C2;
  154. t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  155.  
  156. t1 = mu1_2 + mu2_2 + C1;
  157. t2 = sigma1_2 + sigma2_2 + C2;
  158. t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  159.  
  160. Mat ssim_map;
  161. divide(t3, t1, ssim_map); // ssim_map = t3./t1;
  162.  
  163. Scalar mssim = mean(ssim_map); // mssim = average of ssim map
  164. return mssim;
  165. }
  166. // ----------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment