Face Detection using a webcam
In this tutorial, you'll learn how to detect faces in a video stream from your camera using Capturer object from Face SDK API. Detected faces are highlighted with a green rectangle.
Besides Face SDK and Visual Studio 2019, you'll need a camera connected to your PC (for example, a webcam). You can build and run this project on Windows.
You can find the tutorial project in Face SDK: examples/tutorials/csharp/FirstApp
#
Prepare projectRun Visual Studio and create new project New > Project > Windows Forms application (.NET FrameWork) > Next. Then you can to mention the name and location of our project. You will see a Form Designer displayed in Visual Studio. In this Form Designer, you will start building a Windows Forms application.
Double-click the left mouse button on the form. This will create a new
Form1_Load
method in theForm1.cs
. It will be needed further.Open Build > Configuration Manager. In the field
Platform
in the combobox select<New...>
and choose platformx64
.Click
OK > Close
.Open the Form Designer and add the PictureBox component to the form (located in View > Toolbox > All Windows Forms). For the created
pictureBox1
clickDock in parent container
and select Size ModeZoom
.
#
Getting a video from a webcamTo work with the webcam, we will use the Emgu.CV, Emgu.CV.Bitmap, Emgu.CV.runtime.windows libraries. To install the package use
NuGet Package Manager
Tools > NuGet Package Manager > Manage Nuget Packages for Solution. Select theBrowse
tab, find the packages and install them.Open the
Form1.cs
code editor and add imports to the beginning of the code:Add the
GetFrame
method to the class, modifyForm1_Load
. Also adds the variablescamera
andcamera_id
with device number:Now you can build and start program Build > Run Code Analysis on Solution and make sure that the video from the camera is displayed.
#
Adding FaceSDK to a projectOpen View > Solution Explorer > References (right-click) > Add Reference -> Browse and add
FacerecCSharpWrapper.dll
from<facesdk_root_dir>\bin
folder. ClickOK
.To automatically copy libraries into your project open Project > {PROJECT_NAME} Properties... > Build Events > Edit Pre-build... and add lines:
Note: replace
<facesdk_root_dir>
with the path where the installed FaceSDK is located.Click
OK
.In
Form1.cs
add imports to the beginning of the code:Add private variables into
Form1
class:Note: replace
<facesdk_root_dir>
with the path where the installed FaceSDK is located.Before the line
Application.Idle ...
add the creation of the FaceSDK service and the capturer to theForm1_Load
method:The final step is to add a
drawDetections
method that will detect the face in the image and draw a frame around the face.Now you can launch the application and check how it works.