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

时间:2017-07-19 14:14:38

标签: c# emgucv aforge

我使用 AForge 来使用网络摄像头设备并将代码分成几类以保持井井有条。

我将 Emgu的面部识别插入 NeorisForm.cs 以查看它是否有效,因此我将为其创建一个类。

我有什么问题?

  

1)如何“加入”连接相机与Emgu的AForge   使用FaceRecognition?

     

2)在我的NeorisForm.cs构造函数中,我插入了两个来自Emgu的var,   但我不知道它是什么,我不知道如何创建它   HaarCascade是我正在使用Windows窗体。我也需要加入   AForge的变量上限。

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
    }
}

Camera.cs

#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
    }
}

1 个答案:

答案 0 :(得分:0)

您不需要仅仅为相机输入使用Aforge,一些emgu线可以做到这一点,您不需要将每个帧转换为Aforge的图像到Emgu的图像。

有关详情,请访问here代码略有修改

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

<强>初始化

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
  }  
}