Beispiel #1
0
        void WAY_cv()
        {
            String            path, img_path;
            CascadeClassifier faceCascade = new CascadeClassifier();

            faceCascade.Load("haar_face.xml");
            Mat img_array, gray, face_roi, pred_gray, pred_face_roi;

            int[] labels   = new int[20];
            Mat[] features = new Mat[20];
            for (int i = 0; i < people.Length; i++)
            {
                path = @"img\" + people[i];
                for (int j = 0; j < 5; j++)
                {
                    img_path  = path + @"\" + i.ToString() + ".jpg";
                    img_array = Cv2.ImRead(img_path);
                    gray      = new Mat();
                    Cv2.CvtColor(img_array, gray, ColorConversionCodes.BGR2GRAY);
                    Rect[] ret = faceCascade.DetectMultiScale(img_array, 1.1, 4);
                    face_roi            = gray.AdjustROI(ret[0].Top, ret[0].Bottom, ret[0].Left, ret[0].Right);
                    labels[i * 5 + j]   = i;
                    features[i * 5 + j] = face_roi;
                }
            }
            OpenCvSharp.Face.LBPHFaceRecognizer recognizer;
            recognizer = OpenCvSharp.Face.LBPHFaceRecognizer.Create();

            recognizer.Train(features, labels);
            pred_gray = new Mat();
            Cv2.CvtColor(inCvImage, pred_gray, ColorConversionCodes.BGR2GRAY);
            Rect[] ret2 = faceCascade.DetectMultiScale(pred_gray, 1.1, 4);
            pred_face_roi = pred_gray.AdjustROI(ret2[0].Top, ret2[0].Bottom, ret2[0].Left, ret2[0].Right);
            int ans = recognizer.Predict(pred_face_roi);

            MessageBox.Show(people[ans] + " 입니다!, 아님 말고...");
        }