C#+OpenCV進階(一)_人體識別

꧁执笔小白꧂發表於2024-07-11

  效果排名: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;
                }
            }
        }
    }
}

相關文章