private void FaceTrackingPipeline() { IsDispose = false; OnStart?.Invoke(this, null); #region Manager Init realSenseManager = RealSenseObjects.Session.CreateSenseManager(); if (realSenseManager == null) { MessageBox.Show( "PXCMSenseManager初始化失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } PXCMCaptureManager captureManager = realSenseManager.captureManager; if (captureManager == null) { MessageBox.Show( "PXCMCaptureManager初始化失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } #endregion #region 基本設定 //設定裝置 captureManager.FilterByDeviceInfo(Form.SelectedDevice); //設定串流類型 captureManager.FilterByStreamProfiles(Form.SelectedDeviceStreamProfile); //啟用臉部追蹤模組 realSenseManager.EnableFace(); PXCMFaceModule faceModule = realSenseManager.QueryFace(); if (faceModule == null) { MessageBox.Show( "取得PXCMFaceModule失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } //建立臉部追蹤模組設定 moduleConfiguration = faceModule.CreateActiveConfiguration(); if (moduleConfiguration == null) { MessageBox.Show( "建立PXCMFaceConfiguration失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } //追蹤模式設定 moduleConfiguration.SetTrackingMode(Form.ModeType); moduleConfiguration.strategy = PXCMFaceConfiguration.TrackingStrategyType.STRATEGY_RIGHT_TO_LEFT; moduleConfiguration.detection.isEnabled = true; moduleConfiguration.detection.maxTrackedFaces = 4;//最大追蹤4個臉 moduleConfiguration.landmarks.isEnabled = false; moduleConfiguration.pose.isEnabled = false; recognitionConfig = moduleConfiguration.QueryRecognition(); if (recognitionConfig == null) { MessageBox.Show( "建立RecognitionConfiguration失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } recognitionConfig.Enable(); #endregion #region 讀取資料庫數據 if (Form.FaceData != null) { recognitionConfig.SetDatabase(Form.FaceData); moduleConfiguration.ApplyChanges(); } #endregion #region 預備啟動 moduleConfiguration.EnableAllAlerts(); //moduleConfiguration.SubscribeAlert(FaceAlertHandler); pxcmStatus applyChangesStatus = moduleConfiguration.ApplyChanges(); Form.SetStatus("RealSenseManager初始化中"); if (applyChangesStatus.IsError() || realSenseManager.Init().IsError()) { MessageBox.Show( "RealSenseManager初始化失敗,請檢查設定正確。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } #endregion using (moduleOutput = faceModule.CreateOutput()) { PXCMCapture.Device.StreamProfileSet profiles; PXCMCapture.Device device = captureManager.QueryDevice(); if (device == null) { MessageBox.Show( "取得設備失敗。", "初始化失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); OnStop?.Invoke(this, null); return; } device.QueryStreamProfileSet(PXCMCapture.StreamType.STREAM_TYPE_DEPTH, 0, out profiles); #region Loop while (!_Stop) { while (_Paush) { Application.DoEvents(); } if (realSenseManager.AcquireFrame(true).IsError()) { break; } var isConnected = realSenseManager.IsConnected(); if (isConnected) { var sample = realSenseManager.QueryFaceSample(); if (sample == null) { realSenseManager.ReleaseFrame(); continue; } #region 畫面取出 PXCMImage image = null; if (Form.ModeType == PXCMFaceConfiguration.TrackingModeType.FACE_MODE_IR) { image = sample.ir; } else { image = sample.color; } #endregion moduleOutput.Update();//更新辨識 PXCMFaceConfiguration.RecognitionConfiguration recognition = moduleConfiguration.QueryRecognition(); if (recognition == null) { realSenseManager.ReleaseFrame(); continue; } #region 繪圖與事件 OnFrame?.Invoke(this, new FaceRecognitionEventArgs() { Image = ToBitmap(image) }); FindFace(moduleOutput); #endregion } //發布框 realSenseManager.ReleaseFrame(); } #endregion //更新資料庫緩衝區 //Buffer = moduleOutput.QueryRecognitionModule().GetDatabaseBuffer(); } #region 釋放資源 moduleConfiguration.Dispose(); realSenseManager.Close(); realSenseManager.Dispose(); #endregion IsDispose = true; OnStop?.Invoke(this, null); }
/* Using PXCMSenseManager to handle data */ public void SimplePipeline() { form.UpdateInfo(String.Empty, Color.Black); bool liveCamera = false; bool flag = true; PXCMSenseManager instance = null; _disconnected = false; instance = form.g_session.CreateSenseManager(); if (instance == null) { form.UpdateStatus("Failed creating SenseManager"); return; } PXCMCaptureManager captureManager = instance.captureManager; if (captureManager != null) { if (form.GetRecordState()) { captureManager.SetFileName(form.GetFileName(), true); PXCMCapture.DeviceInfo info; if (form.Devices.TryGetValue(form.GetCheckedDevice(), out info)) { captureManager.FilterByDeviceInfo(info); } } else if (form.GetPlaybackState()) { captureManager.SetFileName(form.GetFileName(), false); } else { PXCMCapture.DeviceInfo info; if (String.IsNullOrEmpty(form.GetCheckedDevice())) { form.UpdateStatus("Device Failure"); return; } if (form.Devices.TryGetValue(form.GetCheckedDevice(), out info)) { captureManager.FilterByDeviceInfo(info); } liveCamera = true; } } /* Set Module */ pxcmStatus status = instance.EnableHand(form.GetCheckedModule()); PXCMHandModule handAnalysis = instance.QueryHand(); if (status != pxcmStatus.PXCM_STATUS_NO_ERROR || handAnalysis == null) { form.UpdateStatus("Failed Loading Module"); return; } PXCMSenseManager.Handler handler = new PXCMSenseManager.Handler(); handler.onModuleProcessedFrame = new PXCMSenseManager.Handler.OnModuleProcessedFrameDelegate(OnNewFrame); PXCMHandConfiguration handConfiguration = handAnalysis.CreateActiveConfiguration(); PXCMHandData handData = handAnalysis.CreateOutput(); if (handConfiguration == null) { form.UpdateStatus("Failed Create Configuration"); return; } if (handData == null) { form.UpdateStatus("Failed Create Output"); return; } if (form.getInitGesturesFirstTime() == false) { int totalNumOfGestures = handConfiguration.QueryGesturesTotalNumber(); if (totalNumOfGestures > 0) { this.form.UpdateGesturesToList("", 0); for (int i = 0; i < totalNumOfGestures; i++) { string gestureName = string.Empty; if (handConfiguration.QueryGestureNameByIndex(i, out gestureName) == pxcmStatus.PXCM_STATUS_NO_ERROR) { this.form.UpdateGesturesToList(gestureName, i + 1); } } form.setInitGesturesFirstTime(true); form.UpdateGesturesListSize(); } } FPSTimer timer = new FPSTimer(form); form.UpdateStatus("Init Started"); if (handAnalysis != null && instance.Init(handler) == pxcmStatus.PXCM_STATUS_NO_ERROR) { PXCMCapture.DeviceInfo dinfo; PXCMCapture.Device device = instance.captureManager.device; if (device != null) { device.QueryDeviceInfo(out dinfo); _maxRange = device.QueryDepthSensorRange().max; } if (handConfiguration != null) { handConfiguration.EnableAllAlerts(); handConfiguration.EnableSegmentationImage(true); handConfiguration.ApplyChanges(); handConfiguration.Update(); } form.UpdateStatus("Streaming"); int frameCounter = 0; int frameNumber = 0; while (!form.stop) { string gestureName = form.GetGestureName(); if (string.IsNullOrEmpty(gestureName) == false) { if (handConfiguration.IsGestureEnabled(gestureName) == false) { handConfiguration.DisableAllGestures(); handConfiguration.EnableGesture(gestureName, true); handConfiguration.ApplyChanges(); } } else { handConfiguration.DisableAllGestures(); handConfiguration.ApplyChanges(); } if (instance.AcquireFrame(true) < pxcmStatus.PXCM_STATUS_NO_ERROR) { break; } frameCounter++; if (!DisplayDeviceConnection(!instance.IsConnected())) { if (handData != null) { handData.Update(); } PXCMCapture.Sample sample = instance.QueryHandSample(); if (sample != null && sample.depth != null) { DisplayPicture(sample.depth, handData); if (handData != null) { frameNumber = liveCamera ? frameCounter : instance.captureManager.QueryFrameIndex(); DisplayJoints(handData); DisplayGesture(handData, frameNumber); DisplayAlerts(handData, frameNumber); } form.UpdatePanel(); } timer.Tick(); } instance.ReleaseFrame(); } } else { form.UpdateStatus("Init Failed"); flag = false; } foreach (PXCMImage pxcmImage in m_images) { pxcmImage.Dispose(); } // Clean Up if (handData != null) { handData.Dispose(); } if (handConfiguration != null) { handConfiguration.Dispose(); } instance.Close(); instance.Dispose(); if (flag) { form.UpdateStatus("Stopped"); } }
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(); }
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) { // 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 } } }; } // 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; PXCMFaceConfiguration.ExpressionsConfiguration econfiguration = moduleConfiguration.QueryExpressions(); if (econfiguration == null) { throw new Exception("ExpressionsConfiguration null"); } econfiguration.properties.maxTrackedFaces = 4; //Expressionの最大認識量 econfiguration.EnableAllExpressions(); //if (m_form.IsExpressionsEnabled()) //{ econfiguration.Enable(); //Expressionsの強制有効化 //} moduleConfiguration.EnableAllAlerts(); moduleConfiguration.SubscribeAlert(FaceAlertHandler); pxcmStatus applyChangesStatus = moduleConfiguration.ApplyChanges(); m_form.UpdateStatus("Init Started", MainForm.Label.StatusLabel); m_form.UpdateStatus("カメラ起動中", MainForm.Label.ReportLabel); if (applyChangesStatus < pxcmStatus.PXCM_STATUS_NO_ERROR || pp.Init() < pxcmStatus.PXCM_STATUS_NO_ERROR) { m_form.UpdateStatus("Init Failed", MainForm.Label.StatusLabel); m_form.UpdateStatus("設定エラー:メニューから適切な設定をしてください。", MainForm.Label.ReportLabel); } else { using (PXCMFaceData moduleOutput = faceModule.CreateOutput()) { Debug.Assert(moduleOutput != null); PXCMCapture.Device.StreamProfileSet profiles; PXCMCapture.Device device = captureMgr.QueryDevice(); if (device == null) { m_form.UpdateStatus("カメラを接続してください。", MainForm.Label.ReportLabel); 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); 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; } m_form.DrawGraphics(moduleOutput); m_form.UpdatePanel(); } pp.ReleaseFrame(); } } // moduleConfiguration.UnsubscribeAlert(FaceAlertHandler); // moduleConfiguration.ApplyChanges(); m_form.UpdateStatus("Stopped", MainForm.Label.StatusLabel); } moduleConfiguration.Dispose(); pp.Close(); pp.Dispose(); }