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 a projectRun Visual Studio and create a new project New > Project > Windows Forms application (.NET FrameWork) > Next. Then define the name and location of our project. You'll see a Form Designer displayed in Visual Studio. Use this Form Designer to build a Windows Forms application.
Double-click the left mouse button on the form to create a new
Form1_Load
method in theForm1.cs
.Open Build > Configuration Manager. In the combobox
Platform
field 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 selectZoom
Size Mode.
#
Get a video from a webcamTo work with the webcam, 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 at the beginning of the code:Add the
GetFrame
method to the class and modifyForm1_Load
. Also, add thecamera
andcamera_id
veriables with a device number:Now build and start Build > Run Code Analysis on Solution program and make sure that the video from the camera is displayed.
#
Add 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 at 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
Application.Idle ...
line, add the creation of Face SDK service and the capturer to theForm1_Load
method:Finally, 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.