Opencv implements intelligent control of Screen Saver programs

Source: Internet
Author: User
Opencv implements Smart screen saver Control By Guo shilong  JieshaoUsually 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 );

If (! Cascade)
{
Stream = fopen ("log. Out", "W ");
Fprintf (stream, "error: cocould not load classifier Cascade/N ");
Fclose (Stream );
Return-1;
}
Storage = cvcreatememstorage (0 );

// Capture = cvcapturefromcam (0 );
// Cvnamedwindow ("result", 1); // capture = cvcapturefromavi ("homes. Avi ");
// Capture = cvcapturefromfile ("homes. Avi ");
// Cvnamedwindow ("result", 1 );
If (capture)
{
Stream = fopen ("log. Out", "W ");
Fprintf (stream, "Capture is OK! /N ");
Fclose (Stream); (;;)
{
If (isstop) // external End Face Detection thread
{
Stream = fopen ("log. Out", "");
Fprintf (stream, "thread is stop! /N ");
Fclose (Stream );
Break ;}
If (! Cvgrabframe (capture ))
Break;
Frame = cvretrieveframe (capture );
If (! Frame)
Break;
If (! Frame_copy)
Frame_copy = cvcreateimage (cvsize (frame-> width, frame-> height ),
Ipl_depth_8u, frame-> nchannels );
If (frame-> origin = ipl_origin_tl)
Cvcopy (frame, frame_copy, 0 );
Else
Cvflip (frame, frame_copy, 0 );

Detect_and_draw (frame_copy); If (cvwaitkey (10)> = 0)
Break;
} Cvreleaseimage (& frame_copy );
// Cvreleasecapture (& capture );
}
// Cvdestroywindow ("result ");
Return 0;
} // Whether the face is detected by external calls
Bool facedetecting: isface ()
{
If (isface) // ensure that the same result is not returned twice
{
Isface = false;
Return true;
}
Return false;
}
Void facedetecting: detect_and_draw (iplimage * IMG)
{
Int scale = 1;
Cvseq * faces = 0;
Iplimage * temp = cvcreateimage (cvsize (IMG-> width/scale, IMG-> height/scale), 8, 3 );
Cvpoint pt1, pt2;
Int I; // cvpyrdown (IMG, temp, cv_gaussian_5x5 );
Cvclearmemstorage (storage); If (cascade)
{
Faces = cvhaardetectobjects (IMG, cascade, storage,
1.1, 2,/* cv_haar_do_canny_pruning */0,
Cvsize (40, 40 ));

Stream = fopen ("log. Out", "");
Fprintf (stream, "detect_and_draw may be OK! /N ");
Fclose (Stream );
// Isface = faces? True: false;

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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.