Skip to main content
Version: 3.13.0

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 project

  1. Run 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.

  2. Double-click the left mouse button on the form to create a new Form1_Load method in the Form1.cs.

  3. Open Build > Configuration Manager. In the combobox Platform field select <New...> and choose platform x64.

    Click OK > Close.

  4. Open the Form Designer and add the PictureBox component to the form (located in View > Toolbox > All Windows Forms). For the created pictureBox1 click Dock in parent container and select Zoom Size Mode.

Get a video from a webcam

  1. To 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 the Browse tab, find the packages and install them.

  2. Open the Form1.cs code editor and add imports at the beginning of the code:

    using Emgu.CV;
    using Emgu.CV.Structure;
  3. Add the GetFrame method to the class and modify Form1_Load. Also, add the camera and camera_id veriables with a device number:

    namespace FirstApp
    {
    public partial class Form1 : Form
    {
    private int camera_id = 0;
    private VideoCapture camera;
    ...
    private void GetFrame(object sender, EventArgs e)
    {
    Image<Bgr, byte> image = camera.QueryFrame().ToImage<Bgr, byte>();
    pictureBox1.Image = image.ToBitmap();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    camera = new VideoCapture(camera_id);
    if (camera.IsOpened)
    {
    Application.Idle += GetFrame;
    }else
    {
    MessageBox.Show("Camera not opened!", "Camera error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    Application.Exit();
    }
    }
    ...
    }
    }
  4. 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 project

  1. Open View > Solution Explorer > References (right-click) > Add Reference -> Browse and add FacerecCSharpWrapper.dll from <facesdk_root_dir>\bin folder. Click OK.

  2. To automatically copy libraries into your project, open Project > {PROJECT_NAME} Properties... > Build Events > Edit Pre-build... and add lines:

    set facesdk_root_dir=<facesdk_root_dir>
    xcopy /Y "%facesdk_root_dir%\bin\facerec.dll" "$(TargetDir)"
    xcopy /Y "%facesdk_root_dir%\bin\sense4.dll" "$(TargetDir)"
    xcopy /Y "%facesdk_root_dir%\bin\tensorflow.dll" "$(TargetDir)"

    Note: replace <facesdk_root_dir> with the path where the installed FaceSDK is located.

    Click OK.

  3. In Form1.cs add imports at the beginning of the code:

    using System.Runtime.InteropServices;
    using VDT.FaceRecognition.SDK;
  4. Add private variables into Form1 class:

    private String faceSDKRootDir = "<facesdk_root_dir>";
    private Capturer capturer;

    Note: replace <facesdk_root_dir> with the path where the installed FaceSDK is located.

  5. Before the Application.Idle ... line, add the creation of Face SDK service and the capturer to the Form1_Load method:

    private void Form1_Load_1(object sender, EventArgs e)
    {
    ...
    if (camera.IsOpened)
    {
    FacerecService service = FacerecService.createService(faceSDKRootDir + "\\conf\\facerec", "");
    FacerecService.Config capturerConfig = new FacerecService.Config("fda_tracker_capturer_uld.xml");
    capturer = service.createCapturer(capturerConfig);
    Application.Idle += GetFrame;
    }
    else
    ...
    }
  6. Finally, add a drawDetections method that will detect the face in the image and draw a frame around the face.

    private void GetFrame(object sender, EventArgs e)
    {
    Image<Bgr, byte> image = camera.QueryFrame().ToImage<Bgr, byte>();
    drawDetections(image);
    pictureBox1.Image = image.ToBitmap();
    }

    private void drawDetections(Image<Bgr, byte> image)
    {
    Mat frame_m = image.Mat.Clone();
    byte[] data = new byte[frame_m.Total.ToInt32() * frame_m.NumberOfChannels];
    Marshal.Copy(frame_m.DataPointer, data, 0, (int)data.Length);
    RawImage ri_frame = new RawImage(frame_m.Width, frame_m.Height, RawImage.Format.FORMAT_BGR, data);
    List<RawSample> detected = capturer.capture(ri_frame);
    foreach (RawSample sample in detected)
    {
    RawSample.Rectangle rect = sample.getRectangle();
    image.Draw(new Rectangle((int)rect.x,
    (int)rect.y,
    (int)rect.width,
    (int)rect.height),
    new Bgr(0, 255, 0),
    2);
    }
    }
  7. Now you can launch the application and check how it works.