DEV Community

El Bruno for Microsoft Azure

Posted on β€’ Originally published at elbruno.com on

9 2

#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).

🐿

Code Sample

Easy to implement, and also easy to read:

// 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.

Original Post

Happy coding!

Greetings

El Bruno


Top comments (3)

Collapse
 
alexisag profile image
AlexisAG β€’

Looks good, but I do not see the code.
Do you have a link ? I would like to know how it works.

Collapse
 
elbruno profile image
El Bruno β€’

That's weird πŸ™ƒ
The code snippet is here >> gist.github.com/elbruno/59b9495fb8...

Let me know if you can browse it, or check the code in the original post in elbruno.com/2022/02/15/opencv-dete...

Collapse
 
alexisag profile image
AlexisAG β€’

It's ok now, I can see the code sample πŸ™ƒ
Thx for the share !

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs