Exemplo n.º 1
0
        /// <summary>
        /// 人脸识别
        /// </summary>
        /// <param name="emguImage"></param>
        /// <returns></returns>
        public FaceDetectedObj FaceRecognize(Mat imageFrame)
        {
            FaceDetectedObj fdo = GetFaceRectangle(imageFrame);
            //Image<Gray, byte> tempImg = fdo.originalImg.ToImage<Gray, byte>();
            Mat tempImg = fdo.originalImg;

            //Add
            byte[] array = tempImg.ToBytes();
            Image  image = byteToImage(array);

            #region 给识别出的所有人脸画矩形框
            using (Graphics g = Graphics.FromImage(image))
            {
                foreach (Rect face in fdo.facesRectangle)
                {
                    imageFrame.Rectangle(face, new Scalar(0, 0, 255)); //给识别出的人脸画矩形框

                    Mat grayFace = new Mat();
                    tempImg.CopyTo(grayFace);
                    OpenCvSharp.Size size = new OpenCvSharp.Size(100, 100);
                    Cv2.Resize(grayFace, grayFace, size);

                    Cv2.CvtColor(grayFace, grayFace, ColorConversionCodes.BGR2GRAY);
                    grayFace.EqualizeHist();//得到均衡化人脸的灰度图像

                    #region 得到匹配姓名,并画出
                    if (tfr.trainedFileList != null && tfr.trainedFileList.trainedImages.Count > 0)
                    {
                        int    pr            = tfr.faceRecognizer.Predict(grayFace);
                        string recogniseName = tfr.trainedFileList.trainedFileName[pr].ToString();
                        fdo.Name = recogniseName;
                        fdo.names.Add(recogniseName);
                        imageFrame.PutText(recogniseName, new OpenCvSharp.Point(200, 100), HersheyFonts.HersheyComplex, 1, Scalar.Red);
                    }
                    #endregion

                    _faceSample = face;
                }
            }
            #endregion
            return(fdo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取制定图片,识别出的人脸矩形框
        /// </summary>
        /// <param name="emguImage"></param>
        /// <returns></returns>
        public FaceDetectedObj GetFaceRectangle(Mat emguImage)
        {
            FaceDetectedObj fdo = new FaceDetectedObj();

            fdo.originalImg = emguImage;
            List <Rect> faces = new List <Rect>();

            try
            {
                Rect[] facesDetected = _faceClassifier.DetectMultiScale(emguImage, 1.1, 1, HaarDetectionType.ScaleImage, new OpenCvSharp.Size(100, 120));
                faces.AddRange(facesDetected);
            }
            catch (Exception ex)
            {
                //MessageBox.Show("GetFaceRectangle" + ex.Message);
            }
            fdo.facesRectangle = faces;

            return(fdo);
        }