Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dlib/image_processing/frontal_face_detector.h>
- #include <dlib/image_processing/render_face_detections.h>
- #include <dlib/gui_widgets.h>
- #include <dlib/image_io.h>
- #include <iostream>
- #include <string> // for strings
- #include <iomanip> // for controlling float print precision
- #include <sstream> // string to number conversion
- #include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
- #include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
- #include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
- #include <dlib/opencv.h>
- #include <opencv2/opencv.hpp>
- using namespace std;
- using namespace cv;
- using namespace dlib;
- double getPSNR ( const Mat& I1, const Mat& I2);
- Scalar getMSSIM( const Mat& I1, const Mat& I2);
- // ----------------------------------------------------------------------------------------
- int main(int argc, char** argv)
- { VideoCapture camera;
- if(!camera.open(0))
- {cout<<"camera not open"; return 0;}
- Mat current;
- camera >> current;//reading current frame as earliest as possible
- Mat face;int i;
- string text="face";
- Mat frameReference, frameUnderTest,next,graycurrent,graynext;array2d<unsigned char> img;
- double psnrV;
- Scalar mssimV;
- while(true)
- { if( current.empty() ){cout<<"current frame empty"; break;}
- camera>>next;
- cvtColor(current, graycurrent, CV_RGB2GRAY);
- cvtColor(next, graynext, CV_RGB2GRAY); // frame changed to gray
- frameReference=current;
- frameUnderTest=next;
- psnrV = getPSNR(frameReference,frameUnderTest);
- if(psnrV>17){//start face recog psnrV caluculated by experimenting on pairs of similar/still pics & pics with motion
- // cv::Mat cvimg = cv::imread("asd.jpg"); //cv added due error, code added from facedetect
- dlib::cv_image<rgb_pixel> cimg(frameUnderTest);
- imwrite("a.jpg",frameUnderTest);
- frontal_face_detector detector = get_frontal_face_detector();
- //load_image(img,"a.jpg");
- // pyramid_up(cimg);//inrcease image to detect smaller image
- // Now tell the face detector to give us a list of bounding boxes
- // around all the faces it can find in the image.
- std::vector<rectangle> dets = detector(cimg);
- // Number of faces detected: dets.size()
- for(i=dets.size()-1;i>-1;i--){
- dlib::rectangle r = dets[i];
- Rect roi(r.left(), r.top(), r.width(), r.height());
- cv::Mat face = frameUnderTest(roi);
- string text="face";
- text += std::to_string(i);
- imwrite(text.append(".jpg"),face);
- // program to crop the images with faces & face recog
- }
- }
- else{ //motion not detected
- }
- current=next;
- }
- }
- double getPSNR(const Mat& I1, const Mat& I2)
- {
- Mat s1;
- absdiff(I1, I2, s1); // |I1 - I2|
- s1.convertTo(s1, CV_32F); // cannot make a square on 8 bits
- s1 = s1.mul(s1); // |I1 - I2|^2
- Scalar s = sum(s1); // sum elements per channel
- double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels
- if( sse <= 1e-10) // for small values return zero
- return 0;
- else
- {
- double mse = sse / (double)(I1.channels() * I1.total());
- double psnr = 10.0 * log10((255 * 255) / mse);
- return psnr;
- }
- }
- Scalar getMSSIM( const Mat& i1, const Mat& i2)
- {
- const double C1 = 6.5025, C2 = 58.5225;
- /***************************** INITS **********************************/
- int d = CV_32F;
- Mat I1, I2;
- i1.convertTo(I1, d); // cannot calculate on one byte large values
- i2.convertTo(I2, d);
- Mat I2_2 = I2.mul(I2); // I2^2
- Mat I1_2 = I1.mul(I1); // I1^2
- Mat I1_I2 = I1.mul(I2); // I1 * I2
- /*************************** END INITS **********************************/
- Mat mu1, mu2; // PRELIMINARY COMPUTING
- GaussianBlur(I1, mu1, Size(11, 11), 1.5);
- GaussianBlur(I2, mu2, Size(11, 11), 1.5);
- Mat mu1_2 = mu1.mul(mu1);
- Mat mu2_2 = mu2.mul(mu2);
- Mat mu1_mu2 = mu1.mul(mu2);
- Mat sigma1_2, sigma2_2, sigma12;
- GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
- sigma1_2 -= mu1_2;
- GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
- sigma2_2 -= mu2_2;
- GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
- sigma12 -= mu1_mu2;
- ///////////////////////////////// FORMULA ////////////////////////////////
- Mat t1, t2, t3;
- t1 = 2 * mu1_mu2 + C1;
- t2 = 2 * sigma12 + C2;
- t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
- t1 = mu1_2 + mu2_2 + C1;
- t2 = sigma1_2 + sigma2_2 + C2;
- t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
- Mat ssim_map;
- divide(t3, t1, ssim_map); // ssim_map = t3./t1;
- Scalar mssim = mean(ssim_map); // mssim = average of ssim map
- return mssim;
- }
- // ----------------------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment