Guest User

Untitled

a guest
Oct 10th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <windows.h>
  2. #include "string.h"
  3. #include <stdio.h>
  4.  
  5. #include "opencv2/objdetect/objdetect.hpp"
  6. #include "opencv2/highgui/highgui.hpp"
  7. #include "opencv2/imgproc/imgproc.hpp"
  8.  
  9. #include <cstdio>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13. using namespace cv;
  14.  
  15. bool detectFaces(Mat frame);
  16. void displayFaces(std::vector<Rect> faces, Mat &frame);
  17. void CALLBACK turnScreenOff(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime);
  18. void turnScreenOn();
  19.  
  20. bool timerIsTicking = false;
  21. bool screenIsOn = true;
  22. bool outputIsVisable = true;
  23.  
  24. String faceCascadeName = "haarcascade_frontalface_alt.xml";
  25. CascadeClassifier faceCascade;
  26. MSG msg;
  27.  
  28. //INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  29. int main(int argc, char** argv)
  30. {
  31.  
  32. //  displaying camera output and console for debugging
  33.     LPWSTR *argList;
  34.     int numberOfArgs;
  35.     argList = CommandLineToArgvW(GetCommandLineW(), &numberOfArgs);
  36.     if (argList != NULL)
  37.     {
  38.         for (int i = 0; i<numberOfArgs; ++i)
  39.         {
  40.             if (strcmp((char*) argList[i],"d")) outputIsVisable = true;
  41.         }
  42.     }
  43.     LocalFree(argList);
  44.  
  45.     //hide console
  46.     HWND hWnd = GetConsoleWindow();
  47.     if (!outputIsVisable) ShowWindow(hWnd, SW_HIDE);
  48.     //---------------------------------------------------
  49.  
  50.     CvCapture* capture;
  51.     Mat frame;
  52.  
  53.     if (!faceCascade.load(faceCascadeName)){ printf("--(!)Error loading\n"); return -1; };
  54.  
  55.     capture = cvCaptureFromCAM(CV_CAP_ANY);
  56.     if (capture)
  57.     {
  58.         while (true)
  59.         {
  60.             frame = cvQueryFrame(capture); //get single frame
  61.  
  62.             if (!frame.empty())
  63.             {
  64.                 bool someoneIsWatching = detectFaces(frame); //process frame
  65.                 if (!someoneIsWatching)
  66.                 {
  67.                     if (timerIsTicking) KillTimer(hWnd, 1);
  68.                     if (!timerIsTicking && screenIsOn) SetTimer(hWnd, 1, 5 * 1000, (TIMERPROC) &turnScreenOff); //set 5 sec timer
  69.                 }
  70.                 else
  71.                 {
  72.                     if (!screenIsOn) turnScreenOn;
  73.                     if (timerIsTicking) KillTimer(hWnd,1);
  74.                 }
  75.             }
  76.             else
  77.             {
  78.                 printf(" --(!) No captured frame -- Break!");
  79.                 break;
  80.             }
  81.              int esc = waitKey(10);
  82.              if ((char)esc == 27) { break; }
  83.              //if (GetMessage(&msg, NULL, 0, 0))
  84.              //{
  85.                 // TranslateMessage(&msg);
  86.                 // DispatchMessage(&msg);
  87.              //}
  88.         }
  89.     }
  90.     return 0;
  91. }
  92.  
  93. bool detectFaces(Mat frame)
  94. {
  95.     std::vector<Rect> faces;
  96.     Mat grayFrame;
  97.  
  98.     cvtColor(frame, grayFrame, CV_BGR2GRAY);
  99.     equalizeHist(grayFrame, grayFrame);
  100.  
  101.     faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  102.  
  103.     if (outputIsVisable) displayFaces(faces, frame);
  104.     if (faces.empty()) return false;
  105.     else return true;
  106. }
  107.  
  108. void displayFaces(std::vector<Rect> faces, Mat &frame)
  109. {
  110.     for (size_t i = 0; i < faces.size(); ++i)
  111.     {
  112.         Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
  113.         ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 4, 8, 0);
  114.     }
  115.     imshow("Camera output", frame);
  116. }
  117.  
  118. void CALLBACK turnScreenOff(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
  119. {
  120.     printf("Turning screen off...");
  121.     SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
  122.     screenIsOn = false;
  123. }
  124.  
  125. void turnScreenOn()
  126. {
  127.     SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1);
  128.     screenIsOn = true;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment