public DetectedFace DetectFirstFace(Image <Bgr, Byte> frame, string newLabel = null)
        {
            var result        = new DetectedFace();
            var gray          = frame.Convert <Gray, Byte>();
            var facesDetected = gray.DetectHaarCascade(
                _face,
                1.2,
                10,
                HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                new Size(20, 20));

            gray.Dispose();

            if (!facesDetected[0].Any())
            {
                return(null);
            }

            MCvAvgComp faceInfo = facesDetected[0].First();

            result.FaceInfo = faceInfo;

            var faceImage = frame.Copy(faceInfo.rect)
                            .Convert <Gray, byte>()
                            .Resize(100, 100,
                                    INTER.CV_INTER_CUBIC);

            if (!String.IsNullOrEmpty(newLabel))
            {
                _recognizerProvider.AddNewLabel(newLabel, faceImage);
                _rep.Save(newLabel, faceImage);
                result.Label = newLabel;
                return(result);
            }

            if (_recognizerProvider.HasConfiguredFaces())
            {
                result.Label = _recognizerProvider.GetRecognizer()
                               .Recognize(faceImage);
            }
            else
            {
                result.Label = UnknownLabel;
            }
            faceImage.Dispose();
            return(result);
        }
예제 #2
0
        private FaceRecognitionResult ReconizeFace(Image <Bgr, byte> frame, Rectangle rectangle)
        {
            var cropped = frame.Copy(rectangle)
                          .Convert <Gray, byte>()
                          .Resize(100, 100, Inter.Cubic);

            cropped._EqualizeHist();

            lock (_sync) {
                if (_nextFaceLabel != null)
                {
                    _faceRecognizer.AddNewLabel(_nextFaceLabel, cropped);
                    _faceLabelsRepository.Save(_nextFaceLabel, cropped);
                    _nextFaceLabel = null;
                }
            }

            return(_faceRecognizer.Recognize(cropped));
        }