Ejemplo n.º 1
0
        // 设置面部的许多参数,打开Landmark、Expression,未打开pulse、面部识别模块
        private void InitFaceState()
        {
            PXCMFaceConfiguration faceCfg = faceModule.CreateActiveConfiguration();

            if (faceCfg == null)
            {
#if DEBUG
                System.Windows.Forms.MessageBox.Show("faceCfg failed");
#endif
                return;
            }

            faceCfg.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);
            faceCfg.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_CLOSEST_TO_FARTHEST;
            // 单个人追踪
            faceCfg.detection.maxTrackedFaces = NUM_PERSONS;
            faceCfg.landmarks.maxTrackedFaces = NUM_PERSONS;
            faceCfg.pose.maxTrackedFaces      = NUM_PERSONS;

            // 表情初始化
            PXCMFaceConfiguration.ExpressionsConfiguration expressionCfg = faceCfg.QueryExpressions();
            if (expressionCfg == null)
            {
                throw new Exception("ExpressionsConfiguration null");
            }
            expressionCfg.properties.maxTrackedFaces = NUM_PERSONS;

            expressionCfg.EnableAllExpressions();
            faceCfg.detection.isEnabled = true;
            faceCfg.landmarks.isEnabled = true;
            faceCfg.pose.isEnabled      = true;
            if (expressionCfg != null)
            {
                expressionCfg.Enable();
            }

            //脉搏初始化
            if (false)
            {
                PXCMFaceConfiguration.PulseConfiguration pulseConfiguration = faceCfg.QueryPulse();
                if (pulseConfiguration == null)
                {
                    throw new Exception("pulseConfiguration null");
                }

                pulseConfiguration.properties.maxTrackedFaces = NUM_PERSONS;
                if (pulseConfiguration != null)
                {
                    pulseConfiguration.Enable();
                }
            }

            // 面部识别功能初始化
            if (false)
            {
                PXCMFaceConfiguration.RecognitionConfiguration qrecognition = faceCfg.QueryRecognition();
                if (qrecognition == null)
                {
                    throw new Exception("PXCMFaceConfiguration.RecognitionConfiguration null");
                }
                else
                {
                    qrecognition.Enable();
                }
            }

            faceCfg.ApplyChanges();
        }
Ejemplo n.º 2
0
        public void SimplePipeline()
        {
            PXCMSenseManager pp = m_form.Session.CreateSenseManager();

            if (pp == null)
            {
                throw new Exception("PXCMSenseManager null");
            }

            PXCMCaptureManager captureMgr = pp.captureManager;

            if (captureMgr == null)
            {
                throw new Exception("PXCMCaptureManager null");
            }

            var selectedRes = m_form.GetCheckedColorResolution();

            if (selectedRes != null && !m_form.IsInPlaybackState())
            {
                // Set active camera
                PXCMCapture.DeviceInfo deviceInfo;
                m_form.Devices.TryGetValue(m_form.GetCheckedDevice(), out deviceInfo);
                captureMgr.FilterByDeviceInfo(m_form.GetCheckedDeviceInfo());

                // activate filter only live/record mode , no need in playback mode
                var set = new PXCMCapture.Device.StreamProfileSet
                {
                    color =
                    {
                        frameRate = selectedRes.Item2,
                        imageInfo =
                        {
                            format = selectedRes.Item1.format,
                            height = selectedRes.Item1.height,
                            width  = selectedRes.Item1.width
                        }
                    }
                };

                if (m_form.IsPulseEnabled() && (set.color.imageInfo.width < 1280 || set.color.imageInfo.height < 720))
                {
                    captureMgr.FilterByStreamProfiles(PXCMCapture.StreamType.STREAM_TYPE_COLOR, 1280, 720, 0);
                }
                else
                {
                    captureMgr.FilterByStreamProfiles(set);
                }
            }

            // Set Source & Landmark Profile Index
            if (m_form.IsInPlaybackState())
            {
                //pp.captureManager.FilterByStreamProfiles(null);
                captureMgr.SetFileName(m_form.GetFileName(), false);
                captureMgr.SetRealtime(false);
            }
            else if (m_form.GetRecordState())
            {
                captureMgr.SetFileName(m_form.GetFileName(), true);
            }

            // Set Module
            pp.EnableFace();
            PXCMFaceModule faceModule = pp.QueryFace();

            if (faceModule == null)
            {
                Debug.Assert(faceModule != null);
                return;
            }

            PXCMFaceConfiguration moduleConfiguration = faceModule.CreateActiveConfiguration();

            if (moduleConfiguration == null)
            {
                Debug.Assert(moduleConfiguration != null);
                return;
            }

            var checkedProfile = m_form.GetCheckedProfile();
            var mode           = m_form.FaceModesMap.First(x => x.Value == checkedProfile).Key;

            moduleConfiguration.SetTrackingMode(mode);

            moduleConfiguration.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_RIGHT_TO_LEFT;

            moduleConfiguration.detection.maxTrackedFaces = m_form.NumDetection;
            moduleConfiguration.landmarks.maxTrackedFaces = m_form.NumLandmarks;
            moduleConfiguration.pose.maxTrackedFaces      = m_form.NumPose;

            PXCMFaceConfiguration.ExpressionsConfiguration econfiguration = moduleConfiguration.QueryExpressions();
            if (econfiguration == null)
            {
                throw new Exception("ExpressionsConfiguration null");
            }
            econfiguration.properties.maxTrackedFaces = m_form.NumExpressions;

            econfiguration.EnableAllExpressions();
            moduleConfiguration.detection.isEnabled = m_form.IsDetectionEnabled();
            moduleConfiguration.landmarks.isEnabled = m_form.IsLandmarksEnabled();
            moduleConfiguration.pose.isEnabled      = m_form.IsPoseEnabled();
            if (m_form.IsExpressionsEnabled())
            {
                econfiguration.Enable();
            }

            PXCMFaceConfiguration.PulseConfiguration pulseConfiguration = moduleConfiguration.QueryPulse();
            if (pulseConfiguration == null)
            {
                throw new Exception("pulseConfiguration null");
            }

            pulseConfiguration.properties.maxTrackedFaces = m_form.NumPulse;
            if (m_form.IsPulseEnabled())
            {
                pulseConfiguration.Enable();
            }

            qrecognition = moduleConfiguration.QueryRecognition();
            if (qrecognition == null)
            {
                throw new Exception("PXCMFaceConfiguration.RecognitionConfiguration null");
            }
            if (m_form.IsRecognitionChecked())
            {
                qrecognition.Enable();


                #region 臉部辨識資料庫讀取
                if (File.Exists(DatabasePath))
                {
                    m_form.UpdateStatus("正在讀取資料庫", MainForm.Label.StatusLabel);
                    List <RecognitionFaceData> faceData = null;
                    FaceDatabaseFile.Load(DatabasePath, ref faceData, ref NameMapping);
                    FaceData = faceData.ToArray();
                    qrecognition.SetDatabase(FaceData);
                }
                #endregion
            }



            moduleConfiguration.EnableAllAlerts();
            moduleConfiguration.SubscribeAlert(FaceAlertHandler);

            pxcmStatus applyChangesStatus = moduleConfiguration.ApplyChanges();

            m_form.UpdateStatus("Init Started", MainForm.Label.StatusLabel);

            if (applyChangesStatus < pxcmStatus.PXCM_STATUS_NO_ERROR || pp.Init() < pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                m_form.UpdateStatus("Init Failed", MainForm.Label.StatusLabel);
            }
            else
            {
                using (PXCMFaceData moduleOutput = faceModule.CreateOutput())
                {
                    Debug.Assert(moduleOutput != null);
                    PXCMCapture.Device.StreamProfileSet profiles;
                    PXCMCapture.Device device = captureMgr.QueryDevice();

                    if (device == null)
                    {
                        throw new Exception("device null");
                    }

                    device.QueryStreamProfileSet(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, 0, out profiles);
                    CheckForDepthStream(profiles, faceModule);

                    m_form.UpdateStatus("Streaming", MainForm.Label.StatusLabel);
                    m_timer = new FPSTimer(m_form);

                    #region loop
                    while (!m_form.Stopped)
                    {
                        if (pp.AcquireFrame(true) < pxcmStatus.PXCM_STATUS_NO_ERROR)
                        {
                            break;
                        }
                        var isConnected = pp.IsConnected();
                        DisplayDeviceConnection(isConnected);
                        if (isConnected)
                        {
                            var sample = pp.QueryFaceSample();
                            if (sample == null)
                            {
                                pp.ReleaseFrame();
                                continue;
                            }
                            switch (mode)
                            {
                            case PXCMFaceConfiguration.TrackingModeType.FACE_MODE_IR:
                                if (sample.ir != null)
                                {
                                    DisplayPicture(sample.ir);
                                }
                                break;

                            default:
                                DisplayPicture(sample.color);
                                break;
                            }

                            moduleOutput.Update();
                            PXCMFaceConfiguration.RecognitionConfiguration recognition = moduleConfiguration.QueryRecognition();
                            if (recognition == null)
                            {
                                pp.ReleaseFrame();
                                continue;
                            }

                            if (recognition.properties.isEnabled)
                            {
                                UpdateRecognition(moduleOutput);
                            }

                            m_form.DrawGraphics(moduleOutput);
                            m_form.UpdatePanel();
                        }
                        pp.ReleaseFrame();
                    }
                    #endregion
                }

                //             moduleConfiguration.UnsubscribeAlert(FaceAlertHandler);
                //             moduleConfiguration.ApplyChanges();
                m_form.UpdateStatus("Stopped", MainForm.Label.StatusLabel);
            }

            #region 儲存臉部辨識資訊檔案
            if (DatabaseChanged)
            {
                FaceDatabaseFile.Save(DatabasePath, FaceData.ToList(), NameMapping);
            }
            #endregion

            var dbm = new FaceDatabaseManager(pp);


            moduleConfiguration.Dispose();
            pp.Close();
            pp.Dispose();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 面部
        /// </summary>
        public void FacePipeLine()
        {
            PXCMSenseManager pp = m_form.Session.CreateSenseManager();

            if (pp == null)
            {
                throw new Exception("PXCMSenseManager null");
            }

            pp.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, 640, 360);

            // 面部初始化
            pp.EnableFace();


            PXCMFaceModule faceModule = pp.QueryFace();

            if (faceModule == null)
            {
                Debug.Assert(true);
                return;
            }
            PXCMFaceConfiguration faceCfg = faceModule.CreateActiveConfiguration();

            if (faceCfg == null)
            {
                Debug.Assert(true);
                return;
            }
            faceCfg.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);
            faceCfg.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_CLOSEST_TO_FARTHEST;
            // 单个人追踪
            faceCfg.detection.maxTrackedFaces = NUM_PERSONS;
            faceCfg.landmarks.maxTrackedFaces = NUM_PERSONS;
            faceCfg.pose.maxTrackedFaces      = NUM_PERSONS;

            // 表情初始化
            PXCMFaceConfiguration.ExpressionsConfiguration expressionCfg = faceCfg.QueryExpressions();
            if (expressionCfg == null)
            {
                throw new Exception("ExpressionsConfiguration null");
            }
            expressionCfg.properties.maxTrackedFaces = NUM_PERSONS;

            expressionCfg.EnableAllExpressions();
            faceCfg.detection.isEnabled = true;
            faceCfg.landmarks.isEnabled = true;
            faceCfg.pose.isEnabled      = true;
            if (expressionCfg != null)
            {
                expressionCfg.Enable();
            }

            //脉搏初始化
            PXCMFaceConfiguration.PulseConfiguration pulseConfiguration = faceCfg.QueryPulse();
            if (pulseConfiguration == null)
            {
                throw new Exception("pulseConfiguration null");
            }

            pulseConfiguration.properties.maxTrackedFaces = NUM_PERSONS;
            if (pulseConfiguration != null)
            {
                pulseConfiguration.Enable();
            }

            // 面部识别功能初始化
            //PXCMFaceConfiguration.RecognitionConfiguration qrecognition = faceCfg.QueryRecognition();
            //if (qrecognition == null)
            //{
            //    throw new Exception("PXCMFaceConfiguration.RecognitionConfiguration null");
            //}
            //else
            //{
            //    qrecognition.Enable();
            //}

            faceCfg.ApplyChanges();

            if (pp.Init() < pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                throw new Exception("Init failed");
            }
            else
            {
                using (PXCMFaceData faceData = faceModule.CreateOutput())
                {
                    if (faceData == null)
                    {
                        throw new Exception("face data failure");
                    }
                    while (!m_form.Stopped)
                    {
                        if (pp.AcquireFrame(true).IsError())
                        {
                            break;
                        }

                        var isConnected = pp.IsConnected();
                        if (isConnected)
                        {
                            var sample = pp.QueryFaceSample();
                            if (sample == null)
                            {
                                pp.ReleaseFrame();
                                continue;
                            }

                            // default is COLOR
                            DisplayPicture(sample.color);

                            // 如果检测脸数==0,则continue
                            faceData.Update();
                            int nFace = faceData.QueryNumberOfDetectedFaces();
                            if (nFace == 0)
                            {
                                continue;
                            }

                            // 存
                            //PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);
                            //SaveFaceLandmarkData(face);
                            //SaveFacialExpressionData(face);

                            // 获取脸部特征点数据
                            PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);
                            Landmarks.updateData(face);

                            // 获取表情数据
                            PXCMFaceData.ExpressionsData edata = face.QueryExpressions();

                            // 多线程加锁,数据同步,与VideoModule会发生竞争
                            lock (EmotionModel.svmFeature)
                            {
                                if (edata != null)
                                {
                                    // 提取表情数据
                                    int startIdx = EmotionModel.FaceExpressionStartIdx;
                                    for (int i = 0; i < 22; i++)
                                    {
                                        PXCMFaceData.ExpressionsData.FaceExpressionResult score;
                                        edata.QueryExpression((PXCMFaceData.ExpressionsData.FaceExpression)i, out score);
                                        Expression.facialExpressionIndensity[i] = score.intensity;
                                        // 设置SVM Feature
                                        EmotionModel.svmFeature[startIdx + i].Index = startIdx + i;
                                        EmotionModel.svmFeature[startIdx + i].Value = score.intensity;
                                    }
                                }

                                // 提取特征点位置数据
                                int startIdx2 = EmotionModel.FaceLandmarkStartIdx;
                                for (int i = 0; i < EmotionModel.FaceLandmarkCnt; i++)
                                {
                                    // 设置SVM Feature
                                    EmotionModel.svmFeature[startIdx2 + i].Index = startIdx2 + i;
                                    EmotionModel.svmFeature[startIdx2 + i].Value = Landmarks.Landmarks[i];
                                }
                            }

                            m_form.UpdatePic();
                        }
                        pp.ReleaseFrame();
                    }
                }
            }



            pp.Close();
            pp.Dispose();
        }
Ejemplo n.º 4
0
        public void FaceAndHandPipeLine()
        {
            PXCMSenseManager pp = m_form.Session.CreateSenseManager();

            if (pp == null)
            {
                throw new Exception("PXCMSenseManager null");
            }

            pp.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR, 640, 360);

            // 面部初始化
            pp.EnableFace();
            PXCMFaceModule faceModule = pp.QueryFace();

            if (faceModule == null)
            {
                Debug.Assert(true);
                return;
            }
            PXCMFaceConfiguration faceCfg = faceModule.CreateActiveConfiguration();

            if (faceCfg == null)
            {
                Debug.Assert(true);
                return;
            }
            faceCfg.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR_PLUS_DEPTH);
            faceCfg.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_CLOSEST_TO_FARTHEST;
            // 单个人追踪
            faceCfg.detection.maxTrackedFaces = NUM_PERSONS;
            faceCfg.landmarks.maxTrackedFaces = NUM_PERSONS;
            faceCfg.pose.maxTrackedFaces      = NUM_PERSONS;

            // 表情初始化
            PXCMFaceConfiguration.ExpressionsConfiguration expressionCfg = faceCfg.QueryExpressions();
            if (expressionCfg == null)
            {
                throw new Exception("ExpressionsConfiguration null");
            }
            expressionCfg.properties.maxTrackedFaces = NUM_PERSONS;

            expressionCfg.EnableAllExpressions();
            faceCfg.detection.isEnabled = true;
            faceCfg.landmarks.isEnabled = true;
            faceCfg.pose.isEnabled      = true;
            if (expressionCfg != null)
            {
                expressionCfg.Enable();
            }

            //脉搏初始化
            PXCMFaceConfiguration.PulseConfiguration pulseConfiguration = faceCfg.QueryPulse();
            if (pulseConfiguration == null)
            {
                throw new Exception("pulseConfiguration null");
            }

            pulseConfiguration.properties.maxTrackedFaces = NUM_PERSONS;
            if (pulseConfiguration != null)
            {
                pulseConfiguration.Enable();
            }

            faceCfg.ApplyChanges();

            if (pp.Init() < pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                throw new Exception("Init failed");
            }
            else
            {
                using (PXCMFaceData faceData = faceModule.CreateOutput())
                {
                    if (faceData == null)
                    {
                        throw new Exception("face data failure");
                    }
                    while (!m_form.Stopped)
                    {
                        if (pp.AcquireFrame(true).IsError())
                        {
                            break;
                        }

                        var isConnected = pp.IsConnected();
                        if (isConnected)
                        {
                            var sample = pp.QueryFaceSample();
                            if (sample == null)
                            {
                                pp.ReleaseFrame();
                                continue;
                            }

                            // default is COLOR
                            DisplayPicture(sample.color);

                            // 如果检测脸数==0,则continue
                            faceData.Update();
                            int nFace = faceData.QueryNumberOfDetectedFaces();
                            if (nFace == 0)
                            {
                                continue;
                            }

                            // 存
                            PXCMFaceData.Face face = faceData.QueryFaceByIndex(0);
                            //SaveFaceLandmarkData(face);
                            SaveFacialExpressionData(face);

                            m_form.UpdatePic();
                        }
                        pp.ReleaseFrame();
                    }
                }
            }



            pp.Close();
            pp.Dispose();
        }
Ejemplo n.º 5
0
        private void InitializeFace()
        {
            pxcmStatus sts = this.senseManager.EnableFace();

            if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                throw new Exception("Could not enable face.");
            }

            this.faceModule = this.senseManager.QueryFace();
            if (this.faceModule == null)
            {
                throw new Exception("Could not get face module.");
            }

            // get properties for face module
            this.config = this.faceModule.CreateActiveConfiguration();
            this.config.SetTrackingMode(PXCMFaceConfiguration.TrackingModeType.FACE_MODE_COLOR);
            this.config.ApplyChanges();
            this.config.Update();

            // init pipeline
            sts = this.senseManager.Init();
            if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR &&
                sts != pxcmStatus.PXCM_STATUS_CAPTURE_CONFIG_ALREADY_SET)
            {
                throw new Exception("Initialization failed. " + sts.ToString());
            }

            this.device = this.senseManager.QueryCaptureManager().QueryDevice();
            if (this.device == null)
            {
                throw new Exception("Could not get device.");
            }

            // set mirror mode
            sts = this.device.SetMirrorMode(PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL);
            if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                throw new Exception("Could not set mirror mode.");
            }

            PXCMCapture.DeviceInfo deviceInfo;
            this.device.QueryDeviceInfo(out deviceInfo);
            if (deviceInfo.model == PXCMCapture.DeviceModel.DEVICE_MODEL_IVCAM)
            {
                this.device.SetDepthConfidenceThreshold(1);
                this.device.SetIVCAMFilterOption(6);
            }

            // detection
            this.config.detection.isEnabled       = true;
            this.config.detection.maxTrackedFaces = MAX_FACES;
            // pose
            this.config.pose.isEnabled       = true;
            this.config.pose.maxTrackedFaces = MAX_FACES;
            // landmarks
            this.config.landmarks.isEnabled       = true;
            this.config.landmarks.maxTrackedFaces = MAX_FACES;
            // expressions
            PXCMFaceConfiguration.ExpressionsConfiguration expressionConfig = this.config.QueryExpressions();
            if (expressionConfig == null)
            {
                throw new Exception("Could not configure expressions module.");
            }
            expressionConfig.Enable();
            expressionConfig.EnableAllExpressions();
            expressionConfig.properties.maxTrackedFaces = MAX_FACES;
            // pulse
            PXCMFaceConfiguration.PulseConfiguration pulseConfig = this.config.QueryPulse();
            if (pulseConfig == null)
            {
                throw new Exception("Could not configure pulse module.");
            }
            pulseConfig.Enable();
            pulseConfig.properties.maxTrackedFaces = MAX_FACES;
            this.config.ApplyChanges();
            this.config.Update();

            this.faceData = faceModule.CreateOutput();
        }