Image Face Detection with C# NET application demo and tutorial.
Project Files: Download
In the battle against the pandemic, a lot of programmers turned their attention into developing software for detecting face masks. As a result, in this two part tutorial we will build an application that will be capable of detecting if the person on the image wears a face mask.
In the first part, I will explain how to implement face detection functionality. Later I will train a convolutional neural network to classify the extracted face image as a person who wears a face mask or not.
This tutorial is part of a bigger project and it continues with Face Mask Detection using ML.NET and Model Builder
Face Detection
Face Detection is a technology that is used in a variety of applications. The sole purpose is to identify a human face in a digital image. The task is to find the locations and sizes of all objects in an image that belong to a given class. As a result, the algorithm will draw a bounding box around the desired object.
The Face Detection algorithm focus on the detection of frontal human faces. It is ML based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images (in our case that would be faces).
Image Face Detection with C# NET
This is the application I created to accompany this blog post. It will allow the user to select an image. In addition, the user will be able to set the face detection algorithm parameters. And as a result, the application will draw a bounding boxes around the detected faces.
In the second part of this tutorial we will build a Convolutional Neural Network to detect if the person in the bounding box wears a mask.
Face Detection .NET Project
Before moving on let’s look at the dependency list we are going to need
- Accord
- Accord.Imaging
- Accord.MachineLearning
- Accord.Math
- Accord.Statistics
- Accord.Video
- Accord.Vision
Accord.NET
The Accord.NET Framework is a .NET machine learning framework combined with audio and image processing libraries completely written in C#.
Face Detection Implementation
Object Detection
There are two classes from the Accord.NET library we are going to be using. The first one being HaarObjectDetector Class. This class is the implementation on the Viola-Jones Object Detector based on Haar-like features.
As a result, this class allows you to use different cascades for detecting different types of objects in an image.
For example, you can use this class to detect faces, eyes, nose, pedestrians etc.
Face Detection
The Accord NET library has a FaceHaarCascade Class that is specifically trained to detect faces in an image.
The code combining both of these classes looks something like this:
var faceDetector = new HaarObjectDetector(new FaceHaarCascade());
The HaarObjectDetector class has the implementation on the Viola-Jones object detection framework. Although it can be trained to detect a variety of object classes, it was motivated primarily by the problem of face detection.
It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.
But, because we are using the FaceHaarCascade instance, in our case the HaarObjectDetector will be looking for faces.
Face Detection Parameters
Before we move on to detect faces, we need to setup some parameters
- MaxSize – Maximum window size to consider when searching objects.
- MinSize – Minimum window size to consider when searching objects.
- ScalingFactor – Gets or sets the scaling factor to rescale the window during search.
- ScalingMode – Gets or sets the desired scaling method.
- SearchMode – Gets or sets the desired searching method.
- Steady – Gets how many frames the object has been detected in a steady position.
- Suppression – Gets or sets the minimum threshold used to suppress rectangles which have not been detected sufficient number of times. This property only has effect when SearchMode is set to Average
- UseParallelProcessing – Gets or sets a value indicating whether this HaarObjectDetector should scan the image using multiple threads.
In Computer Vision, features like pixel intensities (RGB values, etc) doesn’t reveal a valuable information on the image. As a result, we need to extract features from the input images rather then using their pixel intensities. Haar-like features for example.
The different types of Haar-like features let us extract useful information from an image such as edges, straight lines, and diagonal lines that we can use to identify an object. For example, a human face.
Since the process of extracting Haar-like features involves calculating the sum of dark/light rectangular regions, the introduction of these parameters is a must.
We have to set the minimum and maximum window size which the algorithm needs to search for face like features in the given image. Because, the algorithm passes the image multiple times we need a scaling factor. The scaling factor allows us to rescale the window in which we are searching for objects. As a result, these parameters can help us detect bigger or smaller desired objects inside the image.
Face Detection Function
After we have set the parameters all we need to do is call the ProcessFrame method with a Bitmap image as a parameter.
var faceRectangles = faceDetector.ProcessFrame(picture);
The faceRectangles is an Array of Rectangles. The Rectangle struct represents each face bounding box location and size on the image.
Image Face Detection with C# NET
Now let’s quickly examine the face detection code in the application:
As you can see there is one button that is used to open and select an image – btnOpenImage
btnDetect holds the logic for face detection.
First we make sure a valid image is selected using the ImageSelector class. Next we use the wrapper class FaceDetector that abstracts the Accord NET HaarObjectDetectorClass and provides a method called ExtractFaces with the image as parameter as well as the HaarObjectDetectorClass parameters.
The next step is optional, but I still used it to pre-process the image. In order to maximize the number of face detections I decided to Grayscale the image, as well as apply the Equalize Histogram procedure.
The Grayscale method will turn your image from 3 channel RGB to a single channel grayscale image.
The EqualizeHistogram method usually increases the global contrast of the image, especially when the usable data of the image is represented by close contrast values. Through this adjustment, pixels intensities can be better distributed on the histogram. This allows for areas of lower local contrast to gain a higher contrast without affecting the global contrast.
The idea behind this Image Pre-processing step is to better enhance features that may represent faces.
And the last step would be to clear up the PictureBox Control from any pre existing drawn rectangle bounding boxes, if the ExtractFaces method returns result.
Because the method ExtractFaces returns the bounding box around a detected face, we can now draw it on the PictureBox.
Final Thoughts
This is just Part 1 of the two part series of the Face Mask Detection Project. We just described how to detect and extract a face from a given image. Next we use that image to recognize if a person is wearing a face mask or now.
dramago.live
April 19, 2024 at 11:47 amYour dedication to community service is making a real impact. Your contributions to local initiatives inspire us all to be more involved in our communities.
dramago.live
April 19, 2024 at 11:49 amYour podcast recommendations are a treasure trove of great content! I’ve discovered some gems thanks to your suggestions. Keep being the curator of quality podcasts.
dramago.live
April 22, 2024 at 9:10 amYour authenticity shines through your content. Thanks for being genuine in a world that sometimes values superficiality. Keep being true to yourself.
dramago.live
April 28, 2024 at 11:57 amYour thoughtful comments in discussions add so much depth. Your perspectives challenge us to think beyond the surface. Thanks for contributing meaningfully.
Kassidy
April 29, 2024 at 8:16 pmI’m gone to inform my little brother, that
he should also go to see this blog on regular basis to
get updated from hottest information.
Hokicoy Daftar
April 30, 2024 at 9:22 amHey there! I know this is kinda off topic but I was wondering which blog
platform are you using for this site? I’m getting fed up of WordPress because I’ve had issues with hackers and
I’m looking at options for another platform. I would be awesome if you could point
me in the direction of a good platform.
Tanesia
April 30, 2024 at 12:22 pmI have to thank you for the efforts you have put in writing this site.
I am hoping to check out the same high-grade content by you in the future as
well. In truth, your creative writing abilities has inspired me to get my own, personal site now
😉
Aniesha
April 30, 2024 at 12:34 pmI’m gone to convey my little brother, that he should also go to see this weblog on regular basis
to take updated from hottest news update.
Seretha
April 30, 2024 at 12:37 pmExcellent pieces. Keep writing such kind of info on your
blog. Im really impressed by your blog.
Hello there, You have performed an incredible job. I’ll certainly digg it and for my
part recommend to my friends. I’m sure they will be benefited from this site.
Andrez
April 30, 2024 at 12:54 pmSimply wish to say your article is as amazing. The clearness to your submit is
simply excellent and that i can think you are a professional
in this subject. Fine with your permission allow me to seize your feed to stay updated with impending post.
Thanks one million and please carry on the rewarding work.
Jona
April 30, 2024 at 12:56 pmGood day I am so thrilled I found your website, I really found you by mistake, while I was
looking on Bing for something else, Anyways I am here now and would just like to say cheers for a marvelous post and
a all round entertaining blog (I also love the theme/design), I
don’t have time to read it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back
to read much more, Please do keep up the fantastic jo.
Nastacia
April 30, 2024 at 1:03 pmHello there! This post couldn’t be written any better!
Reading through this post reminds me of my old room mate!
He always kept chatting about this. I will forward this
article to him. Fairly certain he will have a good read. Thanks for sharing!
Francico
April 30, 2024 at 1:07 pmWoah! I’m really enjoying the template/theme of this site.
It’s simple, yet effective. A lot of times it’s challenging to get that “perfect balance” between usability and appearance.
I must say you’ve done a awesome job with this.
Also, the blog loads extremely quick for me on Safari.
Outstanding Blog!
Lacresia
April 30, 2024 at 1:15 pmThat is very fascinating, You are a very professional blogger.
I have joined your feed and look ahead to seeking more of your wonderful post.
Also, I have shared your site in my social
networks
sabung ayam sv388
April 30, 2024 at 3:21 pmHello, Neat post. There’s an issue with your site in web explorer,
might test this? IE nonetheless is the marketplace chief and
a big section of people will leave out your excellent writing because of this problem.
cara deposit pulsa joker123
April 30, 2024 at 4:09 pmMy partner and I absolutely love your blog
and find many of your post’s to be just what I’m looking for.
can you offer guest writers to write content to suit your needs?
I wouldn’t mind writing a post or elaborating on a number of
the subjects you write about here. Again, awesome site!
joker123 pulsa
April 30, 2024 at 4:20 pmHi there everyone, it’s my first go to see at this site, and piece of writing
is really fruitful in favor of me, keep up posting such posts.
Sophia
April 30, 2024 at 5:49 pmHelpful info. Lucky me I found your website accidentally, and I am surprised why this coincidence didn’t came about earlier!
I bookmarked it.
Jimmie
April 30, 2024 at 5:56 pmI’m impressed, I have to admit. Rarely do I come across a blog that’s both equally educative and engaging,
and without a doubt, you have hit the nail on the head. The
problem is something which not enough people are speaking intelligently
about. Now i’m very happy I stumbled across this in my search for something relating to this.
Shemika
April 30, 2024 at 5:58 pmI know this if off topic but I’m looking into starting my own blog and was curious what all is needed to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% certain. Any suggestions or
advice would be greatly appreciated. Cheers
Chanie
April 30, 2024 at 6:00 pmI’m not that much of a internet reader to be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your website to come back
in the future. Many thanks
Devita
April 30, 2024 at 6:03 pmHmm is anyone else encountering problems with the pictures on this blog loading?
I’m trying to find out if its a problem on my end or if
it’s the blog. Any feedback would be greatly appreciated.
Marshawn
April 30, 2024 at 6:38 pmHi there! I know this is kind of off-topic but I
had to ask. Does operating a well-established website like
yours require a massive amount work? I’m completely new to blogging but
I do write in my journal daily. I’d like
to start a blog so I will be able to share my personal experience and thoughts online.
Please let me know if you have any suggestions or tips for new aspiring blog owners.
Appreciate it!
naga169
April 30, 2024 at 6:46 pmFirst of all I want to say excellent blog! I had a quick question that I’d like to ask if you don’t mind.
I was curious to find out how you center yourself and clear your head
before writing. I have had a hard time clearing my mind in getting my ideas out there.
I truly do take pleasure in writing however it just seems like the first 10 to 15 minutes are generally wasted simply
just trying to figure out how to begin. Any recommendations
or hints? Thank you!
Devarus
April 30, 2024 at 6:48 pmThat is really interesting, You are an excessively skilled blogger.
I have joined your rss feed and look forward
to looking for more of your wonderful post. Additionally, I’ve shared your website in my social networks
naga169
April 30, 2024 at 10:29 pmI take pleasure in, lead to I found just what I used to be taking a look for.
You have ended my four day lengthy hunt! God Bless you man. Have a great day.
Bye
naga169
May 1, 2024 at 12:14 amUndeniably believe that which you stated.
Your favorite reason appeared to be on the internet the simplest thing to be aware of.
I say to you, I certainly get annoyed while people
consider worries that they plainly don’t know about.
You managed to hit the nail upon the top as well as defined out the whole thing without having side-effects , people can take a signal.
Will probably be back to get more. Thanks
jus buah
May 1, 2024 at 5:16 amThere’s certainly a great deal to learn about this subject.
I really like all of the points you made.
https://7apk.ru/user/MichellCuni/
May 1, 2024 at 11:04 amMagnificent goods from you, man. I’ve understand your stuff previous to and you’re just too
excellent. I actually like what you have acquired here,
certainly like what you are saying and the way in which you say it.
You make it enjoyable and you still care for to keep
it smart. I can’t wait to read much more from you.
This is really a great web site.
https://rateproxy.com/user/KelvinJacobs/
May 1, 2024 at 11:18 amFor newest news you have to go to see world-wide-web and
on web I found this site as a finest website for hottest updates.
http://vesti46.ru/user/CelinaHaddon/
May 1, 2024 at 11:25 amHi! I know this is kind of off topic but I was wondering if
you knew where I could get a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having problems finding
one? Thanks a lot!
adu ayam sv388
May 1, 2024 at 12:00 pmI visited multiple web sites except the audio quality
for audio songs present at this web site is genuinely fabulous.
sv388
May 1, 2024 at 1:59 pmAmazing blog! Do you have any tips and hints for aspiring writers?
I’m hoping to start my own site soon but I’m a little lost on everything.
Would you suggest starting with a free platform like
Wordpress or go for a paid option? There are so many choices out there that I’m completely
overwhelmed .. Any ideas? Thank you!
slot88
May 1, 2024 at 2:07 pmWe’re a bunch of volunteers and opening a brand new scheme in our community.
Your website provided us with valuable information to work on. You’ve performed a formidable process and our
entire community will likely be thankful to you.
joker123 deposit pulsa 10rb
May 1, 2024 at 3:09 pmHi every one, here every one is sharing such familiarity, therefore it’s nice to read this website,
and I used to go to see this weblog daily.
naga169
May 1, 2024 at 7:36 pmYou are so awesome! I do not think I have read something like
that before. So great to discover another person with original thoughts on this issue.
Really.. thank you for starting this up. This site is one thing that’s needed on the web,
someone with a bit of originality!
Hokicoy login
May 1, 2024 at 8:32 pmWhats up very nice website!! Guy .. Beautiful .. Amazing ..
I will bookmark your blog and take the feeds additionally?
I’m happy to find a lot of useful info right here within the publish, we need develop extra strategies on this regard, thanks for sharing.
. . . . .
agen sv388
May 2, 2024 at 12:14 amHi! I know this is kind of off topic but I was wondering which blog platform are you using for this website?
I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking
at alternatives for another platform. I would be fantastic if you could point me in the direction of a good
platform.
sabung ayam sv388
May 2, 2024 at 1:01 amRight here is the right site for anybody who
hopes to understand this topic. You realize a whole lot
its almost tough to argue with you (not that I actually would want to…HaHa).
You certainly put a fresh spin on a subject that has been written about for many years.
Great stuff, just excellent!
joker123 deposit via pulsa
May 2, 2024 at 2:57 amof course like your web-site but you need to check the spelling on quite a few of your posts.
Several of them are rife with spelling problems and I find it very bothersome to inform the reality then again I will certainly come back again.
joker123 deposit pulsa 10rb
May 2, 2024 at 3:02 amUndeniably imagine that that you stated. Your favourite reason appeared to be on the net the easiest factor to keep in mind of.
I say to you, I definitely get annoyed while other people think
about issues that they just do not realize about. You controlled to
hit the nail upon the top and also defined out the entire thing with
no need side-effects , folks can take a signal.
Will likely be again to get more. Thank you
naga169
May 2, 2024 at 4:10 amIt’s very easy to find out any matter on net as compared
to textbooks, as I found this post at this site.
laga ayam sv388
May 2, 2024 at 10:00 amWhen I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment.
Is there any way you can remove people from that service?
Thanks!
followerswift.com
May 2, 2024 at 12:11 pmYour podcast recommendations never disappoint! I’ve discovered some incredible content thanks to your curated suggestions. Keep being a podcast connoisseur.
到會推介
May 2, 2024 at 12:49 pmThank you for another fantastic post. Where else could
anybody get that type of information in such a perfect means of writing?
I have a presentation subsequent week, and I’m at the search for such information.
Tosha
May 2, 2024 at 2:48 pmWe stumbled over here from a different web page and thought I may as
well check things out. I like what I see so now i’m following you.
Look forward to finding out about your web page again.
Jannette
May 3, 2024 at 2:23 amYou may have your very own target listing of URLs somewhere besides GSA Internet Search Engine Ranker.
Feel free to visit my blog; http://www.jjrun.kr/bbs/board.php?bo_table=free&wr_id=128504
naga169
May 3, 2024 at 4:45 amWhat’s up everyone, it’s my first go to see at this web site, and paragraph is actually
fruitful for me, keep up posting these articles or reviews.
sv388
May 3, 2024 at 9:05 amHey there would you mind sharing which blog platform you’re working with?
I’m looking to start my own blog soon but I’m having a difficult
time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different
then most blogs and I’m looking for something unique.
P.S Apologies for being off-topic but I had to ask!