Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include "string.h"
- #include <stdio.h>
- #include "opencv2/objdetect/objdetect.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <cstdio>
- #include <iostream>
- using namespace std;
- using namespace cv;
- bool detectFaces(Mat frame);
- void displayFaces(std::vector<Rect> faces, Mat &frame);
- void CALLBACK turnScreenOff(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime);
- void turnScreenOn();
- bool timerIsTicking = false;
- bool screenIsOn = true;
- bool outputIsVisable = true;
- String faceCascadeName = "haarcascade_frontalface_alt.xml";
- CascadeClassifier faceCascade;
- MSG msg;
- //INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- int main(int argc, char** argv)
- {
- // displaying camera output and console for debugging
- LPWSTR *argList;
- int numberOfArgs;
- argList = CommandLineToArgvW(GetCommandLineW(), &numberOfArgs);
- if (argList != NULL)
- {
- for (int i = 0; i<numberOfArgs; ++i)
- {
- if (strcmp((char*) argList[i],"d")) outputIsVisable = true;
- }
- }
- LocalFree(argList);
- //hide console
- HWND hWnd = GetConsoleWindow();
- if (!outputIsVisable) ShowWindow(hWnd, SW_HIDE);
- //---------------------------------------------------
- CvCapture* capture;
- Mat frame;
- if (!faceCascade.load(faceCascadeName)){ printf("--(!)Error loading\n"); return -1; };
- capture = cvCaptureFromCAM(CV_CAP_ANY);
- if (capture)
- {
- while (true)
- {
- frame = cvQueryFrame(capture); //get single frame
- if (!frame.empty())
- {
- bool someoneIsWatching = detectFaces(frame); //process frame
- if (!someoneIsWatching)
- {
- if (timerIsTicking) KillTimer(hWnd, 1);
- if (!timerIsTicking && screenIsOn) SetTimer(hWnd, 1, 5 * 1000, (TIMERPROC) &turnScreenOff); //set 5 sec timer
- }
- else
- {
- if (!screenIsOn) turnScreenOn;
- if (timerIsTicking) KillTimer(hWnd,1);
- }
- }
- else
- {
- printf(" --(!) No captured frame -- Break!");
- break;
- }
- int esc = waitKey(10);
- if ((char)esc == 27) { break; }
- //if (GetMessage(&msg, NULL, 0, 0))
- //{
- // TranslateMessage(&msg);
- // DispatchMessage(&msg);
- //}
- }
- }
- return 0;
- }
- bool detectFaces(Mat frame)
- {
- std::vector<Rect> faces;
- Mat grayFrame;
- cvtColor(frame, grayFrame, CV_BGR2GRAY);
- equalizeHist(grayFrame, grayFrame);
- faceCascade.detectMultiScale(grayFrame, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
- if (outputIsVisable) displayFaces(faces, frame);
- if (faces.empty()) return false;
- else return true;
- }
- void displayFaces(std::vector<Rect> faces, Mat &frame)
- {
- for (size_t i = 0; i < faces.size(); ++i)
- {
- Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
- ellipse(frame, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 4, 8, 0);
- }
- imshow("Camera output", frame);
- }
- void CALLBACK turnScreenOff(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
- {
- printf("Turning screen off...");
- SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);
- screenIsOn = false;
- }
- void turnScreenOn()
- {
- SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)-1);
- screenIsOn = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment