
Hi !
Face detected, so next step is to use some prebuild models to perform additional actions: like estimate the Age of a face, and also the Gender. In order to do this, I downloaded a couple of models from here.
Disclaimer: these models are just sample models, do not use them in production. These model does not covers all the necessary scenarios for a real implementation.
And the final winform app is kind of cute!

Below you can find the complete Form1 source code, before let’s take a look at the sample analyzing a magazine photo.

So let’s analyze the code. For this sample, we load 3 models to work with age, faces and gender.
// # detect faces, age and gender using models from https://github.com/spmallick/learnopencv/tree/08e61fe80b8c0244cc4029ac11e44cd0fbb008c3/AgeGender
const string faceProto = "models/deploy.prototxt";
const string faceModel = "models/res10_300x300_ssd_iter_140000_fp16.caffemodel";
const string ageProto = @"models/age_deploy.prototxt";
const string ageModel = @"models/age_net.caffemodel";
const string genderProto = @"models/gender_deploy.prototxt";
const string genderModel = @"models/gender_net.caffemodel";
_ageNet = CvDnn.ReadNetFromCaffe(ageProto, ageModel);
_genderNet = CvDnn.ReadNetFromCaffe(genderProto, genderModel);
_faceNet = CvDnn.ReadNetFromCaffe(faceProto, faceModel);
Once the models are loaded, in the loop to analyze camera frames, we perform face detection, and then age and gender estimation.
while (true)
{
if (!_run) continue;
var startTime = DateTime.Now;
_capture.Read(_image);
if (_image.Empty()) return;
var imageRes = new Mat();
Cv2.Resize(_image, imageRes, new Size(320, 240));
var newImage = imageRes.Clone();
if (_doFaceDetection) DetectFaces(newImage, imageRes);
if (_fps) CalculateFps(startTime, newImage);
var bmpWebCam = BitmapConverter.ToBitmap(imageRes);
var bmpEffect = BitmapConverter.ToBitmap(newImage);
pictureBoxWebCam.Image = bmpWebCam;
pictureBoxEffect.Image = bmpEffect;
}
For each detected face, we perform the age and gender estimation. In order to do this, we crop the detected face (plus a padding), and perform the estimation on the cropped image.
private void AnalyzeAgeAndGender(int x1, int y1, int x2, int y2, Mat imageRes, Mat newImage)
{
// get face frame
var x = x1 - Padding;
var y = y1 - Padding;
var w = (x2 - x1) + Padding * 3;
var h = (y2 - y1) + Padding * 3;
Rect roiNew = new Rect(x, y, w, h);
var face = imageRes[roi: roiNew];
var meanValues = new Scalar(78.4263377603, 87.7689143744, 114.895847746);
var blobGender = CvDnn.BlobFromImage(face, 1.0, new Size(227, 227), mean: meanValues,
swapRB: false);
_genderNet.SetInput(blobGender);
var genderPreds = _genderNet.Forward();
GetMaxClass(genderPreds, out int classId, out double classProbGender);
var gender = _genderList[classId];
_ageNet.SetInput(blobGender);
var agePreds = _ageNet.Forward();
GetMaxClass(agePreds, out int classIdAge, out double classProbAge);
var age = _ageList[classIdAge];
var label = $"{gender},{age}";
Cv2.PutText(newImage, label, new Point(x1 - 10, y2 + 20), HersheyFonts.HersheyComplexSmall, 1, Scalar.Yellow, 1);
}
private void GetMaxClass(Mat probBlob, out int classId, out double classProb)
{
// reshape the blob to 1x1000 matrix
using var probMat = probBlob.Reshape(1, 1);
Cv2.MinMaxLoc(probMat, out _, out classProb, out _, out var classNumber);
classId = classNumber.X;
Debug.WriteLine($"X: {classNumber.X} - Y: {classNumber.Y} ");
}
It’s also important to mention to the GetMaxClass() function, to retrieve the best detected element in the prob result.
And the complete source code:
Happy coding!
Greetings
El Bruno
More posts in my blog ElBruno.com.
More info in https://beacons.ai/elbruno
References
- OpenCVSharp, https://github.com/shimat/opencvsharp
- Wikipedia, Canny Edge Detector
- OpenCV, Canny tutorial
- OpenCV, FAST tutorial
- Wikipedia, FAST
- OpenCV, Perform face detection using Haar Cascades
Net 5 and OpenCV
- 13 lines to display a π¦ camera feed with OpenCV and Net5
- Detecting edges on the π¦ camera feed with Canny algorithm, OpenCV and Net5
- Detecting corners on the π¦ camera feed with FAST algorithm, OpenCV and Net 5
- Display the π¦ camera feed in a WinForm using OpenCV and Net5
- Face Detection using Face Cascades on the π¦ camera feed using OpenCV and Net 5
- Face Detection using DNN on the π¦ camera feed using OpenCV and Net 5
- Face Detection, DNN vs Haar Cascades on the π¦ camera feed using OpenCV and Net 5
- Age and Gender estimation on the π¦ camera feed using OpenCV and Net 5
- Caffe Model Zoo (GoogleNet) detection from the π¦ camera feed using OpenCV and Net5
- Pose detection from the π¦ camera feed using OpenCV and Net5
- Packaging a WinForm OpenCV and Net5 App in a one-self contained file
- Display a video file π₯ in Winform using OpenCV and Net5
- Open a video file π₯ and save each frame as a PNG πΌ file to a folderΒ π
- (Coming Soon) QR Codes detection on the π¦ camera feed using OpenCV and Net 5