Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Emgu.CV从网络摄像头检测人脸(C#)_C#_Emgucv_Face Detection - Fatal编程技术网

使用Emgu.CV从网络摄像头检测人脸(C#)

使用Emgu.CV从网络摄像头检测人脸(C#),c#,emgucv,face-detection,C#,Emgucv,Face Detection,我正在用C#构建一个测试应用程序,用于检测网络摄像头和视频中的人脸 我将其构建为一个Windows窗体,其中包含一个计时器(timer1),一个用于显示网络摄像头输出的图片框(pictureBox1),以及一个用于显示面数的文本框(textBox2)。我已经通过NuGet(v3.1.0.1)安装了EmguCV,并设置了所有内容。EmguCV的大部分教程都是早期版本的,必要的HaarCascade类已经贬值。但是,它为我的代码提供了必要的更新 我现在一切都准备好了。网络摄像头在pictureBox

我正在用C#构建一个测试应用程序,用于检测网络摄像头和视频中的人脸

我将其构建为一个Windows窗体,其中包含一个计时器(
timer1
),一个用于显示网络摄像头输出的图片框(
pictureBox1
),以及一个用于显示面数的文本框(
textBox2
)。我已经通过NuGet(v3.1.0.1)安装了EmguCV,并设置了所有内容。EmguCV的大部分教程都是早期版本的,必要的
HaarCascade
类已经贬值。但是,它为我的代码提供了必要的更新

我现在一切都准备好了。网络摄像头在
pictureBox1
中显示更新图像。探测器应该在每次
timer1
滴答声结束时在网络摄像头帧上工作,并且
Faces[]
数组中的矩形数作为字符串输出到
textBox2
。然而,似乎没有任何效果。我无法让它识别任何东西。程序正在运行,但检测到的面数始终显示为0。如果我最初将
NumberOfFaces
变量设置为类似5的值,Emgu代码会在输出中将其更改为0。所以,发生了一些事情。我正在使用EmguCV提供的haarxml,但没有用。有人能帮我吗

代码转储如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.CvEnum;

namespace FaceDetectTest
{
    public partial class Form1 : Form
    {
        public Capture cap;
        public CascadeClassifier haar;
        public int NumberOfFaces;
        public Rectangle[] Faces;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cap = new Emgu.CV.Capture(0);
            haar = new CascadeClassifier(@"C:\Users\Rob\AppData\Roaming\masterbeast\haarcascade_frontalface_alt_tree.xml");
            NumberOfFaces = 0;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
           using (Image<Bgr, byte> nextFrame = cap.QueryFrame().ToImage<Bgr, Byte>())
            {
                if (nextFrame != null)
                {
                Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                Faces = haar.DetectMultiScale(grayframe, 1.1, 1, new Size(100, 100), new Size(1000, 1000));
                NumberOfFaces = Faces.Length;

            }

            pictureBox1.Image = nextFrame.ToBitmap();
                textBox2.Text = NumberOfFaces.ToString();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Emgu.CV;
使用Emgu.CV.Structure;
使用Emgu.Util;
使用Emgu.CV.CvEnum;
命名空间FaceDetectTest
{
公共部分类Form1:Form
{
公共捕获帽;
哈尔邦公共图书馆;
公众面孔;
公共矩形[]面;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
cap=新Emgu.CV.Capture(0);
haar=新的级联分类器(@“C:\Users\Rob\AppData\Roaming\masterbeast\haarcascade\u frontalface\u alt_tree.xml”);
面数=0;
}
私有无效计时器1_Tick_1(对象发送方,事件参数e)
{
使用(Image nextFrame=cap.QueryFrame().ToImage())
{
if(nextFrame!=null)
{
Image grayframe=nextFrame.Convert();
Faces=haar.DetectMultiScale(灰度帧,1.1,1,新大小(100100),新大小(10001000));
NumberOfFaces=Faces.Length;
}
pictureBox1.Image=nextFrame.ToBitmap();
textBox2.Text=NumberOfFaces.ToString();
}
}
}
}

与堆栈溢出一样,我在发布问题几秒钟后找到了(部分)解决方案

导致问题的似乎是条件语句
if(nextFrame!=null)
。我不知道这是否与我的网络摄像头的刷新率有关,或者与
timer1
tickover有关。删除此选项后,现在可以检测人脸,不过我需要使用
DetectMultiScale
方法的参数,因为只有在完全打开且非常接近的情况下才能检测到这些参数


如果有人能进一步阐明这一点,请作客;然而,它是有效的,这对我来说才是最重要的。如果您来这里是想了解使用Emgu.CV 3.1进行人脸检测的示例,请在不使用该条件语句的情况下尝试上述代码。

我也在尝试使用相同的代码,您能告诉我如何找到haarcascade文件吗?我使用NuGet软件包安装了EMGU.CV,当我运行代码时,它说找不到haarcascade文件。