65.9K
CodeProject 正在变化。 阅读更多。
Home

智能屏保

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.87/5 (16投票s)

2007 年 7 月 19 日

CPOL

2分钟阅读

viewsIcon

190642

downloadIcon

10975

一个实用工具,使用计算机视觉(人脸检测)而不是空闲计时器来控制您计算机上的屏幕保护程序。

Screenshot - facede.png

引言

这是一个使用人脸检测来控制屏幕保护程序的实用工具。 人脸检测是使用 OpenCV Haar-Cascade 方法执行的。 该软件主要是一个驻留在系统托盘中的守护进程,并不断观察来自网络摄像头的输入,以分析用户是否不再可见。 目前支持两个角度,即正面姿势和侧面姿势。

背景

智能屏幕保护程序的想法并不新鲜。 过去,由于计算机视觉算法的准确性问题和计算机的计算能力,这是无法实现的。 OpenCV(开源计算机视觉库)实现了由 Paul Viola 最初提出的对象检测算法。 我采用了该库自带的人脸检测示例,并使用了它附带的级联文件。

Using the Code

人脸检测由混合模式 DLL 执行,即它使用非托管代码来执行人脸检测并提供托管包装器。 因此,现在可以从任何启用 CLR 的语言(C#、VB.NET 等)调用此 DLL。 此 DLL 需要一个托管 System.Drawing.Image 对象并执行人脸检测。

此托管 System.Drawing.Image 对象取自 ctlDxCam 库(一个自定义库,由 CodeProject.com 上一篇文章中发布的代码构建,它可以从网络摄像头捕获帧作为 System.Drawing.Image 对象),该库从网络摄像头捕获视频帧。

屏幕保护程序由一个类控制,该类使用从 CodeProject.com 上的另一篇文章借用的代码。 请参阅参考资料部分了解详细信息。

// On the first thread, we perform polling for face detection
CommonVariables.FaceDetected = (faceLocator.WrapDetectFaces(LatestFrame)>0);

// On the second thread, we evaluate conditions to trigger screen saver
private void Run()
  {
   bool IsScreenSaverRunning;
   while(KeepRunning)
   {
    //check if screen saver is currently active
    IsScreenSaverRunning = ScreenSaverHandler.IsScreenSaverRunning();
    
    //if last captured frame has not been evaluated
    if(!CommonVariables.IsConsumed)
    {
    //was faces detected in the last polled frame
     if(CommonVariables.FaceDetected)
     {
      CommonVariables.VoteCount = 0;
    //yes face was detected, kill screen saver if running
      if(IsScreenSaverRunning)
        ScreenSaverHandler.StopScreenSaver();
     }
     else if(!IsScreenSaverRunning)
     {     
    //face was not detected, screen saver isnt running either
    //collect votes of no_face_detected
      CommonVariables.VoteCount++;
    //if votes exceed tolerated amount of no_face detected condition
      if(CommonVariables.VoteCount >= CommonVariables.MinVoteRequired)
      {
    //reset votescount and launch screen saver
       CommonVariables.VoteCount = 0;
       ScreenSaverHandler.LaunchScreenSaver();
      }
     }
    }
 
    if(IsScreenSaverRunning)
      Thread.Sleep(1000);     //shorter polling timer
    else             //longer polling timer
      Thread.Sleep(CommonVariables.PollingTimer); 
   }
  }  

源代码对摄像头输入执行轮询,并在所需的时间间隔评估捕获的帧。 对于每个没有脸部的帧,计数器 (VoteCount) 会递增。 在指定数量的此类投票后,启动屏幕保护程序。

如果轮询计时器设置为 5 秒,所需投票数设置为 2,则如果一个人离开座位,屏幕保护程序将在大约 (5*2) 10 秒内开始运行。

关注点

此源代码演示了一个有趣的概念,即从托管代码(C#、VB.NET、托管 VC++.NET)中使用 OpenCV。 人脸检测的算法非常强大,但仍然有其局限性。 可以通过设置环境和摄像头位置的灯光条件等来调整算法的错误检测。用户可以选择他是否想要正面人脸检测或侧面人脸检测,因此将摄像头放置在相应的角度。

参考文献

历史

  • 第一个版本,概念验证
© . All rights reserved.