首页

C#人脸识别源码

c#

2020-9-7

【文件目录】


├─人脸识别
│  └─FaceDetect
│      │  FaceDetect.sln
│      │  
│      └─FaceDetect
│          │  Emgu.CV.dll
│          │  Emgu.CV.ML.dll
│          │  Emgu.CV.UI.dll
│          │  Emgu.Util.dll
│          │  FaceDetect.csproj
│          │  FaceDetect.csproj.user
│          │  haarcascade_eye.xml
│          │  haarcascade_frontalface_alt_tree.xml
│          │  Mainframe.cs
│          │  Mainframe.Designer.cs
│          │  Mainframe.resx
│          │  Program.cs
│          │  ZedGraph.dll
│          │  
│          ├─bin
│          │  ├─Debug
│          │  │      Emgu.CV.dll
│          │  │      Emgu.CV.ML.dll
│          │  │      Emgu.CV.UI.dll
│          │  │      Emgu.Util.dll
│          │  │      FaceSimilarity.exe
│          │  │      FaceSimilarity.pdb
│          │  │      FaceSimilarity.vshost.exe
│          │  │      haarcascade_eye.xml
│          │  │      haarcascade_frontalface_alt_tree.xml
│          │  │      ZedGraph.dll
│          │  │      
│          │  └─x86
│          │      └─Debug
│          │              Emgu.CV.dll
│          │              Emgu.CV.ML.dll
│          │              Emgu.CV.ML.xml
│          │              Emgu.CV.UI.dll
│          │              Emgu.CV.UI.xml
│          │              Emgu.CV.xml
│          │              Emgu.Util.dll
│          │              Emgu.Util.xml
│          │              FaceSimilarity.exe
│          │              FaceSimilarity.pdb
│          │              FaceSimilarity.vshost.exe
│          │              haarcascade_eye.xml
│          │              haarcascade_frontalface_alt_tree.xml
│          │              ZedGraph.dll
│          │              
│          ├─obj
│          │  │  FaceDetect.csproj.FileListAbsolute.txt
│          │  │  FaceSimilarity.csproj.FileListAbsolute.txt
│          │  │  
│          │  ├─Debug
│          │  │  │  FaceDetect.csproj.GenerateResource.Cache
│          │  │  │  FaceSimilarity.csproj.GenerateResource.Cache
│          │  │  │  FaceSimilarity.exe
│          │  │  │  FaceSimilarity.Mainframe.resources
│          │  │  │  FaceSimilarity.pdb
│          │  │  │  FaceSimilarity.Properties.Resources.resources
│          │  │  │  ResolveAssemblyReference.cache
│          │  │  │  
│          │  │  ├─Refactor
│          │  │  └─TempPE
│          │  └─x86
│          │      └─Debug
│          │          │  FaceSimilarity.csproj.GenerateResource.Cache
│          │          │  FaceSimilarity.exe
│          │          │  FaceSimilarity.Mainframe.resources
│          │          │  FaceSimilarity.pdb
│          │          │  FaceSimilarity.Properties.Resources.resources
│          │          │  ResolveAssemblyReference.cache
│          │          │  
│          │          ├─Refactor
│          │          └─TempPE
│          └─Properties
│                  AssemblyInfo.cs
│                  Resources.Designer.cs
│                  Resources.resx
│                  Settings.Designer.cs
│                  Settings.settings
│                  
└─训练库
        haarcascade_eye.xml
        haarcascade_frontalface_alt_tree.xml
        

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV.Structure;
using System.Drawing;
using System.Collections;
using System.IO;

namespace FaceSimilarity
{
    public partial class Mainframe : Form
    {
        public Mainframe()
        {
            InitializeComponent();
        }

        // variables
        private string haarXmlPath = @"haarcascade_frontalface_alt_tree.xml";

        private Image<Bgr, byte> currentImage;

        // components
        // - webcam
        private Capture webcam;
        private bool cameraInUse = false;

        // list of faces history
        List<PictureBox> faceHistory = new List<PictureBox>();

        // - face detection
        private HaarCascade haar;

        // show message in status bar
        private void showMessage(string msg)
        {
            resultText.Text = msg;
            resultText.Update();
        }

        // initiate webcam
        private void initiateWebCam(int index)
        {
            if (webcam != null)
                return;

            try
            {
                showMessage("Initializing WebCam #"   index   "...");
                webcam = new Capture(index);
                showMessage("WebCam #"   index   " initialized.");
                startButton.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to load webcam!");
                Console.WriteLine("WEBCAM:"   ex.Message);
                startButton.Enabled = false;
            }
        }

        // initiate HaarCascade obj.
        private void initiateHaar(string fileName)
        {
            if (haar != null)
                return;

            showMessage("Loading HaarCascade data...");
            haar = new HaarCascade(fileName);
            showMessage("HaarCascade data loaded.");
        }

        // analyze one frame
        private MCvAvgComp[] getFaces(Image<Bgr, byte> img, HaarCascade haar)
        {
            if (haar == null || img == null)
                return null;

            MCvAvgComp[] faces = haar.Detect(img.Convert<Gray, byte>(), 1.4, 1, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
            if (faces != null)
            {
                resultText.Text = string.Format("Found {0} face(s).", faces.Length);
            }

            return faces;
        }

        // mark faces
        private void markFaces(MCvAvgComp[] faces)
        {
            foreach (MCvAvgComp face in faces)
            {
                currentImage.Draw(face.rect, new Bgr(Color.Lime), 2);
                PictureBox pic = new PictureBox();
                pic.Image = currentImage.Copy(face.rect).ToBitmap();
                pic.Width = pic.Image.Width;
                pic.Height = pic.Image.Height;
                faceHistory.Add(pic);
            }
            imageBox.Image = currentImage;
            flowPanel.Controls.Clear();
            flowPanel.Controls.AddRange(faceHistory.ToArray());
            if (faceHistory.Count > 0) 
                flowPanel.ScrollControlIntoView(faceHistory[faceHistory.Count - 1]);
        }

        // process camera
        private void processCamera(object sender, EventArgs e)
        {
            currentImage = webcam.QueryFrame();
            currentImage = currentImage.Flip(Emgu.CV.CvEnum.FLIP.HORIZONTAL);
            //currentImage = currentImage.Resize(imageBox.Width, imageBox.Height, Emgu.CV.CvEnum.INTER.CV_INTER_NN);
            imageBox.Image = currentImage;
        }

        // process faces
        private void processFaces(object sender, EventArgs arg)
        {
            markFaces(getFaces(currentImage, haar));
        }

        // first load form
        private void Mainframe_Activated(object sender, EventArgs e)
        {
            initiateHaar(haarXmlPath);
            initiateWebCam(0);
        }

        // start/stop
        private void startButton_Click(object sender, EventArgs e)
        {
            if (webcam == null)
                return;

            destGroupBox.Text = "Real-time Camera";

            if (cameraInUse)
            {
                showMessage("Stopped.");
                Application.Idle -= new EventHandler(processCamera);
                timer1.Tick -= new EventHandler(processFaces);
                startButton.Text = "Start";
            }
            else
            {
                showMessage("Start capture...");
                Application.Idle  = new EventHandler(processCamera);
                timer1.Tick  = new EventHandler(processFaces);
                startButton.Text = "Stop";
            }
            cameraInUse = !cameraInUse;
        }

        // open a pic & analyze
        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                destGroupBox.Text = "Picture: "   Path.GetFileName(openFileDialog.FileName);
                currentImage = new Image<Bgr, byte>(openFileDialog.FileName);
                imageBox.Image = currentImage;
                processFaces(null, null);
            }
        }


        // clear history
        private void clearButton_Click(object sender, EventArgs e)
        {
            flowPanel.Controls.Clear();
            faceHistory.Clear();
        }

    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
╤╡┴╖┐Γ/haarcascade_eye.xml , 506314
╤╡┴╖┐Γ/haarcascade_frontalface_alt_tree.xml , 3644763
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/Emgu.CV.dll , 266240
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/Emgu.CV.ML.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/Emgu.CV.UI.dll , 126976
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/Emgu.Util.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/FaceSimilarity.exe , 24576
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/FaceSimilarity.pdb , 30208
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/FaceSimilarity.vshost.exe , 5632
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/haarcascade_eye.xml , 506314
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/haarcascade_frontalface_alt_tree.xml , 3644763
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/Debug/ZedGraph.dll , 307200
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.dll , 266240
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.ML.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.ML.xml , 113345
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.UI.dll , 126976
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.UI.xml , 35149
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.CV.xml , 1094587
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.Util.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/Emgu.Util.xml , 21208
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/FaceSimilarity.exe , 24576
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/FaceSimilarity.pdb , 30208
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/FaceSimilarity.vshost.exe , 5632
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/haarcascade_eye.xml , 506314
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/haarcascade_frontalface_alt_tree.xml , 3644763
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/bin/x86/Debug/ZedGraph.dll , 307200
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Emgu.CV.dll , 266240
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Emgu.CV.ML.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Emgu.CV.UI.dll , 126976
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Emgu.Util.dll , 32768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/FaceDetect.csproj , 6154
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/FaceDetect.csproj.user , 241
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/haarcascade_eye.xml , 506314
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/haarcascade_frontalface_alt_tree.xml , 3644763
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Mainframe.cs , 5768
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Mainframe.Designer.cs , 8735
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Mainframe.resx , 6211
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceDetect.csproj.GenerateResource.Cache , 846
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceSimilarity.csproj.GenerateResource.Cache , 846
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceSimilarity.exe , 24576
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceSimilarity.Mainframe.resources , 180
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceSimilarity.pdb , 30208
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/FaceSimilarity.Properties.Resources.resources , 180
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/Debug/ResolveAssemblyReference.cache , 57652
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/FaceDetect.csproj.FileListAbsolute.txt , 1750
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/FaceSimilarity.csproj.FileListAbsolute.txt , 4593
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/FaceSimilarity.csproj.GenerateResource.Cache , 846
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/FaceSimilarity.exe , 24576
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/FaceSimilarity.Mainframe.resources , 180
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/FaceSimilarity.pdb , 30208
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/FaceSimilarity.Properties.Resources.resources , 180
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/obj/x86/Debug/ResolveAssemblyReference.cache , 57323
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Program.cs , 477
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Properties/AssemblyInfo.cs , 1198
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Properties/Resources.Designer.cs , 2884
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Properties/Resources.resx , 5612
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Properties/Settings.Designer.cs , 1099
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/Properties/Settings.settings , 249
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect/ZedGraph.dll , 307200
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect.sln , 1273
╚╦┴│╩╢▒≡/FaceDetect/FaceDetect.suo , 15360
╚╦┴│╩╢▒≡/FaceDetect/FaceSimilarity.suo , 17920
没有账号? 忘记密码?

社交账号快速登录