#OpenCV – Detect and blur faces 😁 using haar cascades in C#

Hi !

NET is in anniversary mode, so I think it will be a good idea to share some camera samples in C#. For these demos I’m using C#, NET 6 and OpenCVSharp as OpenCV wrapper for .NET.

Today post: detect a face using haar cascades and blur the face area. This demo also show the detected face in a separated window (just for fun).

Easy to implement, and also easy to read:

Code Sample

// Copyright (c) 2022
// Author : Bruno Capuano
// Create Time : 2022 Feb
// Change Log :
// – Open a camera feed from a local USB webcam and analyze each frame to detect faces using haar cascades
// – When a face is detected, the app will blur the face zone in the main window
// – When a face is detected, the app will show the original detected face in a separated window
// – Press [F] to enable / disable the face detection process
// – Press [Q] to quit the app
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using OpenCvSharp;
var faceCascade = new CascadeClassifier();
faceCascade.Load("haarcascade_frontalface_default.xml");
using var capture = new VideoCapture(0);
{
var image = new Mat();
var showFace = true;
var run = true;
using var window = new Window("El Bruno – Blur Face");
using var windowFace = new Window("El Bruno – Face");
while (run)
{
capture.Read(image);
if (image.Empty())
break;
var newSize = new Size(640, 480);
using var smallFrame = new Mat();
Cv2.Resize(image, smallFrame, newSize);
if (showFace)
{
using var gray = new Mat();
Cv2.CvtColor(smallFrame, gray, ColorConversionCodes.BGR2GRAY);
var faces = faceCascade.DetectMultiScale(gray, 1.3, 5);
foreach (var face in faces)
{
Cv2.Rectangle(smallFrame, face, Scalar.Red);
// create a new Mat with the detected face
var faceImg = new Mat(smallFrame,
new OpenCvSharp.Range(face.Top, face.Top + face.Width),
new OpenCvSharp.Range(face.Left, face.Left + face.Height));
windowFace.Image = faceImg;
// blur the face area in the original frame
var faceBlur = new Mat();
Cv2.GaussianBlur(faceImg, faceBlur, new Size(23, 23), 30);
smallFrame[face] = faceBlur;
}
}
window.Image = smallFrame;
switch ((char)Cv2.WaitKey(100))
{
case (char)27: // ESC
run = false;
break;
case 'f':
showFace = !showFace;
break;
}
}
}

Important: Haar cascades are easy to implement and learn, however, not recommented as a good solution to detect faces.

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.


Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: