[Winform]基於Emgu.CV人臉識別


摘要

“OpenCV是一個開源的計算機視覺庫。OpenCV采用C/C++語言編寫,可以運行在Linux/Windows/Mac等操作系統上。OpenCV還提供了Python、Ruby、MATLAB以及其他語言的接口。OpenCV的一個目標是構建一個簡單易用的計算機視覺框架,以幫助開發人員更便捷地設計更復雜得計算機視覺相關應用程序。OpenCV包含的函數有500多個,覆蓋了計算機視覺的許多應用。

Emgu

Emgu CV是將OpenCV使用.net編程語言(C#)封裝成的.net庫,使用Emgu CV就可以在.net平台上調用OpenCV的功能,同時,Emgu CV也是開源的。

Emgu CV官網:http://www.emgu.com

從官網上你可以下載最新版本,我采用的是3.2.0.2682版本的。

安裝成功之后,打開目錄

打開解決方案,你可以看到很多demo,可以根據自己需求研究下

新建winform程序,添加引用,Emgu安裝目錄下的dll

並將所需的文件從Emgu的bin目錄下拷貝到你的程序debug下

測試核心代碼

  public partial class MainFrm : Form
    {
        /// <summary>
        /// 攝像頭
        /// </summary>
        private VideoCapture _capture = null;
        private Mat _frame;
        private string _exePath = AppDomain.CurrentDomain.BaseDirectory;
        private string _trainedFacePath = string.Empty;
        /// <summary>
        /// 3.5s 保存一次圖片
        /// </summary>
        private const double Interval = 3.5;
        long detectionTime;
        List<Rectangle> faces = new List<Rectangle>();
        List<Rectangle> eyes = new List<Rectangle>();
        /// <summary>
        /// 上次保存圖片時間
        /// </summary>
        private DateTime _lastSaveDt = DateTime.Now;
        /// <summary>
        /// 窗口坐標
        /// </summary>
        private Point _frmPoint;
        /// <summary>
        /// 是否抓取到人臉
        /// </summary>
        private bool _isHavePersonFace = false;
        public MainFrm()
        {
            InitializeComponent();
            this.TopMost = true;
            CheckForIllegalCrossThreadCalls = false;
            _frmPoint = new Point();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            //無邊框
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            //不出現在任務欄
            this.ShowInTaskbar = false;
            //去掉滑輪放大或者縮小
            this.VideoImageBox.Enabled = false;
            this.MouseDown += MainFrm_MouseDown;
            this.MouseMove += MainFrm_MouseMove;

            _trainedFacePath = _exePath + "trainedfaces";
            this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 1,
Screen.PrimaryScreen.WorkingArea.Height - this.Height); if (!Directory.Exists(_trainedFacePath)) { Directory.CreateDirectory(_trainedFacePath); } CvInvoke.UseOpenCL = false; try { _capture = new VideoCapture(); _frame = new Mat(); _capture.ImageGrabbed += ProcessFrame; _capture.Start(); } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } void MainFrm_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point myPosittion = MousePosition; myPosittion.Offset(-_frmPoint.X, -_frmPoint.Y); Location = myPosittion; } } void MainFrm_MouseDown(object sender, MouseEventArgs e) { _frmPoint.X = e.X; _frmPoint.Y = e.Y; } private void ReleaseData() { if (_capture != null) _capture.Dispose(); } private void ProcessFrame(object sender, EventArgs e) { if (_capture != null && _capture.Ptr != IntPtr.Zero) { bool isOk = _capture.Retrieve(_frame, 0); VideoImageBox.Image = _frame; if (isOk && !_isHavePersonFace) { Run(_frame); } } } private void UploadFile(string filePath) { try { FileInfo file = new FileInfo(filePath); FileLogHelper.WriteFileLog(file.Name); } catch (Exception ex) { FileLogHelper.WriteFileLog(ex); } } private void Run(Mat mat) { IImage image = mat; DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, out detectionTime); string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; string path = Path.Combine(_trainedFacePath, fileName); foreach (Rectangle face in faces) { try { _isHavePersonFace = true; CvInvoke.Rectangle(image, face, new Bgr(Color.Red).MCvScalar, 1); using (Bitmap bm = new Bitmap(face.Width, face.Height)) using (Graphics g = Graphics.FromImage(bm)) { g.DrawImage(image.Bitmap, new Rectangle(0, 0, face.Width, face.Height),
new Rectangle(face.X + 1, face.Y + 1, face.Width - 1, face.Height - 1), GraphicsUnit.Pixel); bm.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); } break; } catch (Exception ex) { FileLogHelper.WriteFileLog(ex); } } _isHavePersonFace = false; } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM