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:
Blurring a Face ๐ also in C# using .NET 6.
— El Bruno ๐ฟ๏ธ๐บโก (we don't talk about me ๐ค) (@elbruno) February 15, 2022
Blog post coming soon #dotNETLovesMe pic.twitter.com/k0PA0b1w87
Code Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.