2017-07-19 3 views
0

Ich benutzte AForge, um Webcam-Geräte zu verwenden und den Code in Klassen zu trennen, um organisiert zu bleiben.Wie erstellt man eine Echtzeit-Video-Gesichtserkennung mit Emgu?

Ich bin Einfügen Emgu der Gesichtserkennung in NeorisForm.cs um zu sehen, ob es funktionieren wird, so dass ich eine Klasse für sich erstellen.

Was sind meine Probleme?

1) Wie kann ich "join" die AForge, die die Kamera mit Emgu zu Verwendung FaceRecognition verbindet?

2) In meinem NeorisForm.cs Konstruktor eingefügt ich zwei var von Emgu, aber ich weiß nicht, was es ist, und ich weiß nicht, wie dies zu schaffen HaarCascade zu sein, dass ich die Windows Form bin mit. Ich muss auch die Var-Kappe mit AForge beitreten.

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

Antwort

0

Sie brauchen nicht Aforge zu verwenden, nur für Kamera-Eingang, ein paar emgu Linien dagegen tun können Und Sie müssen nicht jeden Frame von Aforges Image zu Emgu's Image konvertieren.

Für here Code weitere Informationen besuchen Sie leicht

Put haarcascade_frontalface_alt_tree.xml im selben Verzeichnis der EXE-Datei oder den Ordner bin

Initialisierungen

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(); 
} 

Erkennung modifiziert

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