C# 如何使用Emgu创建实时视频人脸识别?

C# 如何使用Emgu创建实时视频人脸识别?,c#,emgucv,aforge,C#,Emgucv,Aforge,我使用了一个RGE来使用网络摄像头设备,并将代码分成几个类来保持组织 我正在将Emgu的人脸识别插入到NeorisForm.cs中,看看它是否有效,所以我将为它创建一个类 我的问题是什么? 1) 我如何“加入”将相机与Emgu连接到 使用人脸识别 2) 在我的NeorisForm.cs构造函数中,我插入了来自Emgu的两个变量, 然而,我不知道它是什么,也不知道如何创建它 HaarCascade表示我正在使用Windows窗体。我也需要加入 var cap带有一个RGE NeorisForm.c

我使用了一个RGE来使用网络摄像头设备,并将代码分成几个类来保持组织

我正在将Emgu的人脸识别插入到NeorisForm.cs中,看看它是否有效,所以我将为它创建一个类

我的问题是什么?

1) 我如何“加入”将相机与Emgu连接到 使用人脸识别

2) 在我的NeorisForm.cs构造函数中,我插入了来自Emgu的两个变量, 然而,我不知道它是什么,也不知道如何创建它 HaarCascade表示我正在使用Windows窗体。我也需要加入 var cap带有一个RGE

NeorisForm.cs

#region System Package
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;


using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
#endregion System Package
#region Classes Import
using VideoRecognition.FRAMEWORKS.AForge.Camera;
using VideoRecognition.FRAMEWORKS.AForge.Componentes;
#endregion Classes Import

namespace VideoRecognition
{
    public partial class NeorisForm : Form
    {
        #region Variáveis
        // Instanciamento de Classes
        private static Camera AForgeCamera;
        private static DTMovimento AForgeMotion;
        #endregion Variáveis

        // EMGU
        private Capture cap;
        private HaarCascade haar;

        #region Construtor
        public NeorisForm()
        {
            InitializeComponent();
            AForgeCamera = new Camera(this);
            AForgeMotion = new DTMovimento(this);

            AForgeCamera.BuscarDispositivos();
            AForgeMotion.LigarDetectorMovimento();

            // EMGU
            // passing 0 gets zeroth webcam
            cap = new Capture(0);
            // adjust path to find your xml
            haar = new HaarCascade("..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
        }
        #endregion Construtor

        // EMGU
        private void timer1_Tick(object sender, EventArgs e)
        {
            using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
            {
                if (nextFrame != null)
                {
                    // there's only one channel (greyscale), hence the zero index
                    //var faces = nextFrame.DetectHaarCascade(haar)[0];
                    Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                    var faces =
                            grayframe.DetectHaarCascade(
                                    haar, 1.4, 4,
                                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new System.Drawing.Size(nextFrame.Width / 8, nextFrame.Height / 8)
                                    )[0];
                    foreach (var face in faces)
                    {
                        nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
                    }
                    pictureBox1.Image = nextFrame.ToBitmap();
                }
            }
        }

        #region Botões
        private void btn_IniciarCamera_Click(object sender, EventArgs e)
        {
            AForgeCamera.LigarCamera();
        }

        private void btn_PararVideo_Click(object sender, EventArgs e)
        {
            AForgeCamera.DesligarCamera();
        }
        #endregion Botões

        #region Eventos
        // VideoSourcePlayer
        private void videoSourceCamerasPlayer_NewFrame(object sender, ref Bitmap image)
        {
            AForgeMotion.ProcessarFrameCamera(image);
        }
        #endregion Eventos
    }
}
#region AForge Framework
using AForge.Video.DirectShow;
#endregion AForge Framework

namespace VideoRecognition.FRAMEWORKS.AForge.Camera
{
    public class Camera
    {
        #region Variáveis
        private FilterInfoCollection DispositivosCamera; // Coleta as Informações de Dispositivos de Câmeras Conectados.
        private VideoCaptureDevice cameras; // Captura o Vídeo do Dispositivo da Câmera.

        // Instanciamento de Classes
        private NeorisForm NeorisForm;
        #endregion Variáveis

        #region Construtor
        public Camera(NeorisForm neorisForm) {
            this.NeorisForm = neorisForm; // Instancia o NeorisForm nesta Classe.
        }
        #endregion Construtor

        #region Eventos
        // Liga a Câmera.
        // depois de selecionar o Dispositivo para uso
        public void LigarCamera() {
            cameras = new VideoCaptureDevice(DispositivosCamera[NeorisForm.comboBox_ListaDispositivos.SelectedIndex].MonikerString);
            NeorisForm.videoSourceCamerasPlayer.VideoSource = cameras;
            NeorisForm.videoSourceCamerasPlayer.Start();
        }

        // Desliga a Câmera.
        public void DesligarCamera() {
            NeorisForm.videoSourceCamerasPlayer.SignalToStop();
        }

        // Lista Dispositivos de Câmeras conectados para uso.
        public void BuscarDispositivos() {
            DispositivosCamera = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo DispositivosCamerasEncontrados in DispositivosCamera)
            {
                NeorisForm.comboBox_ListaDispositivos.Items.Add(DispositivosCamerasEncontrados.Name);
            }

            NeorisForm.comboBox_ListaDispositivos.SelectedIndex = 0;
        }
        #endregion Eventos
    }
}
private Capture capture;
private HaarCascade haarCascade;
// if you are using emgucv 2.X then use HaarCascade and 
// if you are using emgucv 3.X then use CascadeClassifier
DispatcherTimer timer;

public MainWindow()
{
  InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  capture = new Capture();
  haarCascade = new HaarCascade(@"haarcascade_frontalface_alt_tree.xml");
  timer = new DispatcherTimer();
  timer.Tick += new EventHandler(timer_Tick);
  timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
  timer.Start();
}

您不需要仅将图像用于相机输入,只需使用几行emgu线即可,您不需要将图像中的每一帧转换为emgu图像

欲了解更多信息,请访问代码稍作修改

将haarcascade_frontalface_alt_tree.xml放在.exe或bin文件夹的同一目录中

初始化

#region System Package
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;


using System;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
#endregion System Package
#region Classes Import
using VideoRecognition.FRAMEWORKS.AForge.Camera;
using VideoRecognition.FRAMEWORKS.AForge.Componentes;
#endregion Classes Import

namespace VideoRecognition
{
    public partial class NeorisForm : Form
    {
        #region Variáveis
        // Instanciamento de Classes
        private static Camera AForgeCamera;
        private static DTMovimento AForgeMotion;
        #endregion Variáveis

        // EMGU
        private Capture cap;
        private HaarCascade haar;

        #region Construtor
        public NeorisForm()
        {
            InitializeComponent();
            AForgeCamera = new Camera(this);
            AForgeMotion = new DTMovimento(this);

            AForgeCamera.BuscarDispositivos();
            AForgeMotion.LigarDetectorMovimento();

            // EMGU
            // passing 0 gets zeroth webcam
            cap = new Capture(0);
            // adjust path to find your xml
            haar = new HaarCascade("..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
        }
        #endregion Construtor

        // EMGU
        private void timer1_Tick(object sender, EventArgs e)
        {
            using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
            {
                if (nextFrame != null)
                {
                    // there's only one channel (greyscale), hence the zero index
                    //var faces = nextFrame.DetectHaarCascade(haar)[0];
                    Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
                    var faces =
                            grayframe.DetectHaarCascade(
                                    haar, 1.4, 4,
                                    HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                                    new System.Drawing.Size(nextFrame.Width / 8, nextFrame.Height / 8)
                                    )[0];
                    foreach (var face in faces)
                    {
                        nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);
                    }
                    pictureBox1.Image = nextFrame.ToBitmap();
                }
            }
        }

        #region Botões
        private void btn_IniciarCamera_Click(object sender, EventArgs e)
        {
            AForgeCamera.LigarCamera();
        }

        private void btn_PararVideo_Click(object sender, EventArgs e)
        {
            AForgeCamera.DesligarCamera();
        }
        #endregion Botões

        #region Eventos
        // VideoSourcePlayer
        private void videoSourceCamerasPlayer_NewFrame(object sender, ref Bitmap image)
        {
            AForgeMotion.ProcessarFrameCamera(image);
        }
        #endregion Eventos
    }
}
#region AForge Framework
using AForge.Video.DirectShow;
#endregion AForge Framework

namespace VideoRecognition.FRAMEWORKS.AForge.Camera
{
    public class Camera
    {
        #region Variáveis
        private FilterInfoCollection DispositivosCamera; // Coleta as Informações de Dispositivos de Câmeras Conectados.
        private VideoCaptureDevice cameras; // Captura o Vídeo do Dispositivo da Câmera.

        // Instanciamento de Classes
        private NeorisForm NeorisForm;
        #endregion Variáveis

        #region Construtor
        public Camera(NeorisForm neorisForm) {
            this.NeorisForm = neorisForm; // Instancia o NeorisForm nesta Classe.
        }
        #endregion Construtor

        #region Eventos
        // Liga a Câmera.
        // depois de selecionar o Dispositivo para uso
        public void LigarCamera() {
            cameras = new VideoCaptureDevice(DispositivosCamera[NeorisForm.comboBox_ListaDispositivos.SelectedIndex].MonikerString);
            NeorisForm.videoSourceCamerasPlayer.VideoSource = cameras;
            NeorisForm.videoSourceCamerasPlayer.Start();
        }

        // Desliga a Câmera.
        public void DesligarCamera() {
            NeorisForm.videoSourceCamerasPlayer.SignalToStop();
        }

        // Lista Dispositivos de Câmeras conectados para uso.
        public void BuscarDispositivos() {
            DispositivosCamera = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            foreach (FilterInfo DispositivosCamerasEncontrados in DispositivosCamera)
            {
                NeorisForm.comboBox_ListaDispositivos.Items.Add(DispositivosCamerasEncontrados.Name);
            }

            NeorisForm.comboBox_ListaDispositivos.SelectedIndex = 0;
        }
        #endregion Eventos
    }
}
private Capture capture;
private HaarCascade haarCascade;
// if you are using emgucv 2.X then use HaarCascade and 
// if you are using emgucv 3.X then use CascadeClassifier
DispatcherTimer timer;

public MainWindow()
{
  InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  capture = new Capture();
  haarCascade = new HaarCascade(@"haarcascade_frontalface_alt_tree.xml");
  timer = new DispatcherTimer();
  timer.Tick += new EventHandler(timer_Tick);
  timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
  timer.Start();
}
检测

void timer_Tick(object sender, EventArgs e)
{
  Image<Bgr,Byte> currentFrame = capture.QueryFrame();

  if (currentFrame != null)
  {
    Image<Gray, Byte> grayFrame = currentFrame.Convert<Gray, Byte>();

    var detectedFaces = grayFrame.DetectHaarCascade(haarCascade)[0];

    foreach (var face in detectedFaces)
      currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);

    // derectedFaces[x].rect containes the rectangles around the faces
    // currentFrame have the image with marked faces
  }  
}
void timer\u Tick(对象发送方,事件参数e)
{
Image currentFrame=capture.QueryFrame();
如果(currentFrame!=null)
{
Image grayFrame=currentFrame.Convert();
var detectedFaces=grayFrame.DetectHarcascade(haarCascade)[0];
foreach(检测面中的变量面)
currentFrame.Draw(face.rect,新Bgr(0,double.MaxValue,0),3);
//derectedFaces[x]。rect包含面周围的矩形
//currentFrame具有带有标记面的图像
}  
}