
Hi !
Let’s do some face detection using a DNN model (See references). As yesterday, I won’t write about details, there are almost 20 years of online documentation available.

And, IMHO opinion code is much more useful that long writing, so let’s go there. 1st load the Caffe model and the config file.
// download model and prototxt from https://github.com/spmallick/learnopencv/tree/master/FaceDetectionComparison/models
const string configFile = "deploy.prototxt";
const string faceModel = "res10_300x300_ssd_iter_140000_fp16.caffemodel";
_faceNet = CvDnn.ReadNetFromCaffe(configFile, faceModel);
And, once we grab the camera frame, let’s perform face detection using the dnn model:
int frameHeight = newImage.Rows;
int frameWidth = newImage.Cols;
using var blob = CvDnn.BlobFromImage(newImage, 1.0, new Size(300, 300),
new Scalar(104, 117, 123), false, false);
_faceNet.SetInput(blob, "data");
using var detection = _faceNet.Forward("detection_out");
using var detectionMat = new Mat(detection.Size(2), detection.Size(3), MatType.CV_32F,
detection.Ptr(0));
for (int i = 0; i < detectionMat.Rows; i++)
{
float confidence = detectionMat.At<float>(i, 2);
if (confidence > 0.7)
{
int x1 = (int)(detectionMat.At<float>(i, 3) * frameWidth);
int y1 = (int)(detectionMat.At<float>(i, 4) * frameHeight);
int x2 = (int)(detectionMat.At<float>(i, 5) * frameWidth);
int y2 = (int)(detectionMat.At<float>(i, 6) * frameHeight);
Cv2.Rectangle(newImage, new Point(x1, y1), new Point(x2, y2), Scalar.Green);
Cv2.PutText(newImage, "Face Dnn", new Point(x1 + 2, y2 + 20),
HersheyFonts.HersheyComplexSmall, 1, Scalar.Green, 2);
}
}
And, the full code is here
That’s all for today!
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
Hola Elbruno! Recien encontré tu blog, super bueno y te felicito! Quisiera ver si me ayudas con una pequeña duda. Estoy en VB.NET. En la línea 94 detectionMat.At(i, 2) la tengo en VB.NET como Dim confidence As Single = detectionMat.At(Of Single)(i, 2) pero siempre me da el error ” is an unsupported type. He estado horas tratando de ver que puede ser pero no encuentro la solución. Tendrás alguna pista? Muchas gracias desde ya. Un abrazo desde Chile.
LikeLike
Hi,
Do you have any examples related to the Mouth open detection using a DNN model (C#)
LikeLike
Hi Amila, no I don’t, sorry.
LikeLike
Hello, It’s fine
Do you have any idea about face recognition using this face net?
LikeLike