728x90
300x250
[C#](OpenCVSharp 4) - Face Detection(얼굴 인식)

 

Face Detection(얼굴 인식)을 C# GUI환경에서 구현하는 방법에 대해 소개하려고 합니다.

OpenCV 공식 github사이트에서 라이브러리를 내려받은 후 진행할 것입니다.

(Let me introduce how to implement Face Detection in C# GUI environment.We will proceed after downloading the library from the official OpenCV github site.)

 

 


1. OpenCV 프로젝트 내려받기

 

http://github.com/opencv/opencv

http://github.com/opencv/opencv/data

 

 

 

 

이 파일이 얼굴 인식에 사용되는 중요한 라이브러리입니다.

(This file is an important library for face recognition.)

얼굴 인식 기능을 사용하려면, 프로젝트에 포함시켜야 합니다.
(To use face detection, you must include it in your project.)

 

[첨부(Attachment)]

haarcascade_frontalface_alt.7z

 

 


2. Nuget으로 OpenCVSharp 4 설치하기(Installing OpenCVSharp 4 with Nuget)

 

프로젝트->Nuget 패키지 관리를 클릭합니다.

"OpenCvSharp4"를 검색하여 설치하도록 합니다.
(Click Project-> Nuget Package Management.Search for "OpenCvSharp4" and install it.)

 

 

 


3. 사용자 인터페이스 구성(Configure the User Interface)


OpenCvSharp.UserInterface 내에 있는 PictureBoxlpl 하나, Timer 하나를 배치합니다.

 

Form1은 Load 코드를 활성화합니다.

Timer1은 Enabled = true로 해주고, Interval은 33으로 해줍니다.

Timer1의 Tick를 활성화해줍니다.

(Form1 activates the Load code.)

(Timer1 sets Enabled = true and Interval sets 33.)

(Activate Tick of Timer1.)

 


4. 프로젝트 내에 중요한 설정하기(Make important settings within your project)

 

세 가지 사항을 별표로 하였습니다.(Three points are starred.)

 

 

 

 

 

 

 

haarcascade_frontalface_alt.xml, opencvSharpExtern.dll은 출력 디렉터리에 복사 항목을 "새 버전이면 복사"에 체크해줍니다.

OpenCVSharpExtern.dll은 아래의 사이트에서 내려받을 수 있습니다.
(haarcascade_frontalface_alt.xml, opencvSharpExtern.dll checks "Copy if newer version" to the output directory.

OpenCVSharpExtern.dll can be downloaded from the site below.)

 

https://github.com/shimat/opencvsharp/releases

 

 

 


5. 코드 작성

 

코드를 작성해봅니다.

 

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 OpenCvSharp;

namespace CsharpFaceDetection
{
    public partial class Form1 : Form
    {

        VideoCapture video;
        Mat frame = new Mat();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
                video = new VideoCapture(0);
                video.FrameWidth = 640;
                video.FrameHeight = 480;
            }
            catch
            {
                timer1.Enabled = false;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            int sleepTime = (int)Math.Round(1000 / video.Fps);

            String filenameFaceCascade = "haarcascade_frontalface_alt.xml";
            CascadeClassifier faceCascade = new CascadeClassifier();

            if (!faceCascade.Load(filenameFaceCascade))
            {
                Console.WriteLine("error");
                return;
            }

            video.Read(frame);

            // detect
            Rect[] faces = faceCascade.DetectMultiScale(frame);
            Console.WriteLine(faces.Length);

            foreach (var item in faces)
            {
                Cv2.Rectangle(frame, item, Scalar.Red); // add rectangle to the image
                Console.WriteLine("faces : " + item);
            }

            // display
            pictureBoxIpl1.ImageIpl = frame;

            Cv2.WaitKey(sleepTime);
        }
    }
}

 

[첨부(Attachment)]

Form1.7z

 


6. 시연하기(Demonstrate)

 

 


7. 참고자료(Reference)

 

1. [C#](OpenCVSharp 4) - c#에서 OpenCV 카메라 열기와 처음 시작하기, Last Modified 2019-11-10, Accessed by 2019-11-11, https://yyman.tistory.com/1350?category=809859

2. opencv/haarcascade_frontalface_alt.xml at master · opencv/opencv , Last Modified , Accessed by 2019-11-11,

https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_alt.xml

3. opencv/opencv: Open Source Computer Vision Library, Last Modified, Accessed by 2019-11-11,

https://github.com/opencv/opencv

4. Releases · shimat/opencvsharp, Last Modified 2019-11-10, Accessed by 2019-11-11, https://github.com/shimat/opencvsharp/releases

반응형

+ Recent posts