【发布时间】:2017-10-07 21:56:07
【问题描述】:
我正在用 C# 构建一个测试应用程序来检测来自网络摄像头的面部和
我已将其构建为带有计时器 (timer1)、用于显示网络摄像头输出的图片框 (pictureBox1) 和用于显示面孔数量的文本框 (textBox2) 的 Windows 窗体。我已经通过 NuGet (v3.1.0.1) 安装了 EmguCV 并设置了所有内容。 EmguCV 的大部分教程都是针对早期版本的,必要的HaarCascade 类已被贬低。但是,this Stack Overflow question 为我的代码提供了必要的更新。
我现在已经做好了一切准备工作。网络摄像头在pictureBox1 中显示更新图像。检测器应该在每次timer1 结束时对网络摄像头帧进行处理,并且Faces[] 数组中的矩形数量作为字符串输出到textBox2。但是,似乎没有任何效果。我无法让它识别任何东西。程序正在运行,但检测到的人脸数量始终为 0。如果我最初将 NumberOfFaces 变量设置为 5,Emgu 代码在输出中将其更改为 0。所以,有些事情正在发生。我正在使用 EmguCV 提供的 haar xml,但无济于事。有谁能够帮我?
下面的代码转储:
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();
}
}
}
}
【问题讨论】:
-
我也在尝试使用相同的代码,你能告诉我如何找到 haarcascade 文件吗?我使用 NuGet 包安装了 EMGU.CV,当我运行我的代码时,它说找不到 haarcascade 文件。
标签: c# emgucv face-detection