예제 #1
0
 void ImageGrabberThread()
 {
     while (!_isDone)
     {
         if (!_captured)
         {
             _capDev.ToImage(_image, 0, 0, Width, Height);
             _captured = true;
             _captureFPS.AddFrame();
             //_capDev.Capture ();
             List <Rect> faces = _faceDetector.DetectFaces();
             //foreach (var f in faces) {
             //	Debug.Log ("Found face: " + f.position.ToString ());
             //}
             if (faces.Count > 0)
             {
                 _face = faces [0];
                 int x = (int)(Mathf.Max(0, _face.x - Margin));
                 int y = (int)(Mathf.Max(0, _face.y - Margin));
                 int w = (int)(Mathf.Min(Width - x, _face.width + Margin * 2));
                 int h = (int)(Mathf.Min(Height - y, _face.height + Margin * 2));
                 FaceRect.Set(x, y, w, h);
                 _facePos.AddSample(new Vector3(x, y, w));
                 _capDev.ToImage(_faceImage, x, y, w, h);
                 _faceCaptured = true;
                 _faceDetected = true;
             }
             else
             {
                 _faceDetected = false;
             }
         }
     }
 }
예제 #2
0
    void _threadProcess()
    {
        if (!_inited)
        {
            _inited = true;
            if (_config.EnableRecognizer)
            {
                _recognizer = new UnityOpenCVPersonRecognizerAPI(_config.trainingPath);
            }
            _detector = new UnityOpenCVFaceDetectorAPI(_config.cascadePath, _config.scaler, _config.minNeighbors, _config.minSize, _config.maxSize);
        }
        while (!_isDone)
        {
            _signalEvent.WaitOne();
            _signalEvent.Reset();
            if (_newImage)
            {
                _detecting = true;
                try{
                    _detector.BindImage(_img);
                    _faces = _detector.DetectFaces();
                    TriggerFaceDetected(_detector.DetectedFaces);
                    Debug.Log("Face Detected : " + _faces.Count.ToString());

                    if (_recognizer != null)
                    {
                        _recognizedFaces.Clear();
                        for (int i = 0; i < _detector.DetectedFaces.Count; ++i)
                        {
                            _recognizer.BindImage(_img);
                            RecognizedFace f = new RecognizedFace();
                            f.ID   = _recognizer.RecognizeFace(_detector.DetectedFaces[i], ref f.distance);
                            f.rect = _detector.DetectedFaces[i];
                            _recognizedFaces.Add(f);
                            //if(conf<60)
                            Debug.Log("Face Detected with label: " + f.ID.ToString() + " with distance:" + f.distance.ToString());
                        }
                        TriggerFaceRecognized(_recognizedFaces);
                    }
                }catch (Exception) {
                }
                _detecting = false;
                _newImage  = false;
            }
        }
    }