使用相机进行面部检测坐标

时间:2014-09-03 19:32:19

标签: c# windows-phone-8.1

我需要一种方法来获取相机视图中中Windows Phone 8.1 的C#中面部坐标。我还没有能够在网上找到任何东西,所以我认为这可能是不可能的。我需要的是"框"的x和y(以及可能的区域)。在摄像机视图中检测到时会在脸部周围形成。有人曾经这样做过吗?

1 个答案:

答案 0 :(得分:2)

代码段(请记住,这是我在代码下面链接的教程中的应用程序的一部分。它不是可复制粘贴的,但应该提供一些帮助)

const string MODEL_FILE = "haarcascade_frontalface_alt.xml";
FaceDetectionWinPhone.Detector m_detector;
public MainPage()
{
   InitializeComponent();
   m_detector = new FaceDetectionWinPhone.Detector(System.Xml.Linq.XDocument.Load(MODEL_FILE));
}

void photoChooserTask_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)  
    {
        BitmapImage bmp = new BitmapImage();
        bmp.SetSource(e.ChosenPhoto);
        WriteableBitmap btmMap = new WriteableBitmap(bmp);

        //find faces from the image
        List<FaceDetectionWinPhone.Rectangle> faces =
             m_detector.getFaces(
             btmMap, 10f, 1f, 0.05f, 1, false, false);

        //go through each face, and draw a red rectangle on top of it.
        foreach (var r in faces)
        {
            int x = Convert.ToInt32(r.X);
            int y = Convert.ToInt32(r.Y);
            int width = Convert.ToInt32(r.Width);
            int height = Convert.ToInt32(r.Height);
            btmMap.FillRectangle(x, y, x + height, y + width, System.Windows.Media.Colors.Red);
         }
        //update the bitmap before drawing it.
        btmMap.Invalidate();
        facesPic.Source = btmMap;
    }
}

这取自developer.nokia.com

要实时执行此操作,您需要截取取景器图像,可能使用NewCameraFrame方法(编辑:不确定是否应使用此方法或PhotoCamera.GetPreviewBufferArgb32,如下所述.I不得不把它留给你的研究) 所以基本上你的任务有两个部分:

  1. 获取取景器图像
  2. 检测上面的面孔(使用类似上面的代码)
  3. 如果我是你,我首先要对从磁盘加载的图像执行第2步,一旦你可以检测到面部,我就会看到如何获取当前取景器图像并检测面部那。一旦你检测到面部,X,Y坐标很容易获得 - 见上面的代码。

    (编辑):我认为您应该尝试使用PhotoCamera.GetPreviewBufferArgb32方法来获取取景器图像。看这里MSDN documentation。此外,请务必搜索MSDN文档和教程。这应该足以完成第1步。

    许多人脸检测算法都使用Haar分类器,Viola-Jones算法等。如果您熟悉它,您会对自己所做的事情更有信心,但是你可以做没有。另外,阅读我链接的材料 - 它们看起来相当不错。