效果排名:Lbp < Haar < CNN
1、Lbp
/// <summary>
/// Lbp人脸识别
/// </summary>
public static Mat FaceDetection_Lbp(Mat mat)
{
var lbpCascade = new CascadeClassifier("model/lbpcascade_frontalface.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 人脸识别
Rect[] faces = lbpCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
2、Haar
/// <summary>
/// Haar人脸识别1
/// haarcascade_frontalface_default.xml
/// </summary>
public static Mat FaceDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_frontalface_default.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 人脸识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar人脸识别2
/// haarcascade_profileface.xml
/// </summary>
public static Mat FaceDetection_HaarProfileFace(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_profileface.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 人脸识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar眼睛识别
/// </summary>
public static Mat EyeDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_eye.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar嘴巴识别
/// </summary>
public static Mat MouthDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_mcs_mouth.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar微笑识别
/// </summary>
public static Mat SmileDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_smile.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar全身识别
/// </summary>
public static Mat FullbodyDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_fullbody.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar上身识别
/// </summary>
public static Mat UpperbodyDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_upperbody.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
/// <summary>
/// Haar下身识别
/// </summary>
public static Mat LowerbodyDetection_Haar(Mat mat)
{
var haarCascade = new CascadeClassifier("model/haarcascade_lowerbody.xml");
Mat outMat = new Mat();
mat.CopyTo(outMat);
using (var gray = new Mat())
{
Cv2.CvtColor(outMat, gray, ColorConversionCodes.BGR2GRAY);
// 眼睛识别
Rect[] faces = haarCascade.DetectMultiScale(
gray, 1.1, 3, HaarDetectionTypes.ScaleImage, new OpenCvSharp.Size(30, 30));
// Render all detected faces
foreach (Rect face in faces)
{
// 画矩形
outMat.Rectangle(face, Scalar.Red, 2);
}
}
return outMat;
}
3、CNN
/// <summary>
/// CNN人脸识别
/// </summary>
/// <returns></returns>
public static Mat FaceDetection_CNN(Mat mat)
{
const string configFile = "model/deploy.prototxt"; // Dnn参数
const string faceModel = "model/res10_300x300_ssd_iter_140000_fp16.caffemodel"; // Dnn人脸模型
Mat outMat = new Mat();
mat.CopyTo(outMat);
// 读图片
int matHeight = outMat.Rows;
int matWidth = outMat.Cols;
using (var faceNet = CvDnn.ReadNetFromCaffe(configFile, faceModel)) // Dnn初始化
{
using (var blob = CvDnn.BlobFromImage(outMat, 1.0,
new Size(300, 300), new Scalar(104, 117, 123), false, false)) // Dnn网络
{
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) // 识别概率>0.7时
{
// 框选人脸
int x1 = (int)(detectionMat.At<float>(i, 3) * matWidth);
int y1 = (int)(detectionMat.At<float>(i, 4) * matHeight);
int x2 = (int)(detectionMat.At<float>(i, 5) * matWidth);
int y2 = (int)(detectionMat.At<float>(i, 6) * matHeight);
Cv2.Rectangle(outMat,
new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x2, y2),
new Scalar(0, 255, 0), 2, LineTypes.Link4);
}
}
return outMat;
}
}
}
}
}
标签:gray,进阶,C#,outMat,OpenCV,faces,var,new,Mat From: https://www.cnblogs.com/qq2806933146xiaobai/p/18295770