Opencv implements intelligent control of Screen Saver programs
Source: Internet
Author: User
Opencv implements Smart screen saver ControlBy Guo shilongJieshaoUsually we end the Screen Saver By shaking the mouse or hitting the keyboard. Is there a way to stop it? For example, the screen saver should be automatically ended when we sit in front of the computer. This article describes how to use a camera to automatically control screen protection. The principle is very simple. It detects whether the face is captured by the camera. If yes, it determines whether the Screen Saver program is running. If yes, it ends the Screen Saver program. I have introduced and provided C ++ encapsulated source code in the previous article. The part of face detection uses the Haar face detection routine in the opencv library.
ImplementationThe program consists of the Main Dialog Box class, screen saver control class, and face detection encapsulation class. The main dialog box is used to set screen protection control parameters, including whether to turn off, wait time, and query time. The screen saver control class completes the screen control function. Face Detection is used to detect whether the camera captures a face. In the main dialog box, a timer is enabled to send the wm_timer message every second. In the timer message response function, a counter is responsible for dynamically displaying the current time (automatically querying the screen saver cycle) in the dialog box ), check whether the Screen Saver is running after a screen saver period. If yes, check whether the Face Detection thread is enabled. If yes, check whether the face is detected, otherwise, enable the thread and check whether the face is detected.
Encapsulation of Face Detection FunctionsFor more information about this routine of opencv, see opencv China. The following is the c ++ encapsulation code of the function.
Header file# Ifndef _ facedetecting _
# DEFINE _ facedetecting _ # include "cv. H"
# Include "highgui. H" # include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <assert. h>
# Include <math. h>
# Include <float. h>
# Include <limits. h>
# Include <time. h>
# Include <ctype. h> class facedetecting
{
Public:
Facedetecting ();
~ Facedetecting ();
Int detectingface ();
Void stopdetecting (bool isstop );
Bool isface ();
PRIVATE:
Void detect_and_draw (iplimage * IMG );
Bool isstop;
Bool isface;
Cvcapture * capture;
File * stream; // for running log
Iplimage * frame, * frame_copy;
Const char * cascade_name;
Static cvmemstorage * storage;
Static cvhaarclassifiercascade * cascade;
Static int facecounter;
}; # Endif
Implementation File# Include "stdafx. H"
# Include "facedetecting. H"
# Include "stdio. H" cvmemstorage * facedetecting: storage = 0;
Cvhaarclassifiercascade * facedetecting: cascade = 0;
Int facedetecting: facecounter = 0; facedetecting: facedetecting (): capture (0), frame (0), frame_copy (0), isface (false), isstop (false ), cascade_name ("haarcascade_frontalface_alt.xml") // haarcascade_frontalface_alt.xml is the training file // under the opencv installation directory .. /.. /data/haarcascades/file // folder. Copy the file to the current directory.
{
Capture = cvcapturefromcam (0); // 0 indicates the device ID
} Facedetecting ::~ Facedetecting ()
{
If (null! = Capture) cvreleasecapture (& capture );
} Int facedetecting: detectingface ()
{
Isstop = false;
Cascade = (cvhaarclassifiercascade *) cvload (cascade_name, 0, 0, 0 );
For (I = 0; I <(faces? Faces-> total: 0); I ++)
{
Cvrect * r = (cvrect *) cvgetseqelem (faces, I );
Pt1.x = r-> X * scale;
Pt2.x = (R-> X + R-> width) * scale;
Pt1.y = r-> y * scale;
Pt2.y = (R-> Y + R-> height) * scale;
Cvrectangle (IMG, pt1, pt2, cv_rgb (255, 0), 3, 8, 0 );
} // If faces is not empty, it indicates that the face is detected for two consecutive frames.
If (faces-> total)
{
// Facecounter ++;
If (2 = ++ facecounter)
{
Facecounter = 0;
Isface = true;
// Sleep (1, 1000 );
}
Else
{
Isface = false;
}
}
Else
{
// Facecounter = 0;
Isface = false;
}
} // Cvshowimage ("result", IMG );
Cvsaveimage ("result.bmp", IMG );
Cvreleaseimage (& temp );
} // External call stop Detection
Void facedetecting: stopdetecting (bool isstop)
{
This-> isstop = isstop ;}
OtherThis program uses the tray function. After the program is started, the chart is automatically displayed on the tray and executed. It supports Right-click Control of the tray. For more information about the functions of the tray icon, see my previous articles and source code. This program is completed on the basis of the previous Screen Protection API testing program, so the two program UIS are the same :).
Program downloadOnly download of the release program is provided. If you need a source program, leave a message.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.