Lập trình nhận dạng khuôn mặt sử dụng EmguCV bằng Csharp

Related Articles

Hôm nay, mình hướng dẫn các bạn viết chương trình nhận dạng khuôn mặt, sử dụng thư viện EmguCV 3.0 bằng ngôn ngữ lập trình Csharp. Đầu tiên các bạn cần download EmguCV tại đường dẫn Download EmguCV.

– Sau khi tải về về, những bạn setup EmguCV ra một thư mục, như video mình hướng dẫn bên dưới mình để ngoài Desktop .

– Tiếp theo, mình imports class DetectFace. cs đi kèm trong bộ thiết lập vào ứng dụng .

– Tiếp đến mình cần imports các thư viện EmguCV vào: Emgu.CV, Emgu.Cuda, Emgu.Until, Emgu.CV.UI.

– Mình chép 2 file nhận dạng khuôn mặt, và mắt trong thư mục OpenCV vào project : haarcascade_frontalface_default. xml và haarcascade_eye. xml

– Mình liên tục chép thư viện x64 vào project, ở đây win mình đang sử dụng là window 64 bit, còn máy bạn nào sử dụng window 32 bit thì imports thư viện x86 vào. Và nhớ sau khi copy những file này vào những bạn cần phải chọn Property cho những file này sang CopyAlways như video mình hướng dẫn bên dưới .

– Để sử dụng được Camera những bạn cần phải import Emgu. CV.UI.dll vào toolbox, sau đó mình lấy control ImageBox ra để hiện thị video của webcam .

Tại sử dụng import thư viện EmguCV này khá là phức tạp nên mình có quay video hướng dẫn bên dưới cho các bạn.

– Dưới đây là code của Form main

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.Cuda;
using FaceDetection;
namespace Nhan_dang_khuon_mat
{ public partial class Form1 : Form { //declaring global variables private Capture capture = null; //takes images from camera as image frames private bool captureInProgress; // checks if capture is executing public Form1() { InitializeComponent(); CvInvoke.UseOpenCL = false; try { capture = new Capture(); capture.ImageGrabbed += ProcessFrame; } catch (NullReferenceException excpt) { MessageBox.Show(excpt.Message); } } private void ProcessFrame(object sender, EventArgs arg) { Mat frame = new Mat(); capture.Retrieve(frame, 0); Mat image = frame; //Read the files as an 8-bit Bgr image long detectionTime; List faces = new List(); List eyes = new List(); //The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release //disabling CUDA module for now bool tryUseCuda = false; bool tryUseOpenCL = true; DetectFace.Detect( image, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, tryUseCuda, tryUseOpenCL, out detectionTime); foreach (Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(Color.Purple).MCvScalar, 3); Bitmap c = frame.Bitmap; Bitmap bmp = new Bitmap(face.Size.Width, face.Size.Height); Graphics g = Graphics.FromImage(bmp); g.DrawImage(c, 0, 0, face, GraphicsUnit.Pixel); } foreach (Rectangle eye in eyes) CvInvoke.Rectangle(image, eye, new Bgr(Color.Green).MCvScalar, 2); imageBox1 .Image = frame; } private void ReleaseData() { if (capture != null) capture.Dispose(); } private void btnStart_Click(object sender, EventArgs e) { if (capture != null) { if (captureInProgress) { //stop the capture btnStart.Text = "Start Capture"; capture.Pause(); } else { //start the capture btnStart.Text = "Stop"; capture.Start(); } captureInProgress = !captureInProgress; } } private void button1_Click(object sender, EventArgs e) { if (capture != null) capture.FlipVertical = !capture.FlipVertical; } private void button2_Click_1(object sender, EventArgs e) { if (capture != null) capture.FlipHorizontal = !capture.FlipHorizontal; } }
}

Chúc các bạn thành công. Hãy nhớ like and share để ủng hộ diễn đàn viết nhiều bài hay hơn nữa heart

Mọi thắc mắc về bài viết xin đặt câu hỏi tại http://hoidap.laptrinhvb.net mình sẽ giải đáp.

Xin cảm ơn các bạn đã theo dõi.

Download Source Detect Face

More on this topic

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Advertismentspot_img

Popular stories