コード例 #1
0
    private bool InitializeRealSense()
    {
        senseManager = PXCMSenseManager.CreateInstance();

        if (senseManager == null)
        {
            Debug.LogError("Unable to create SenseManager.");
            return false;
        }

        if (senseManager.Enable3DSeg().IsError())
        {
            Debug.LogError("Couldn't enable the Face Module.");
            return false;
        }

        segmentationModule = senseManager.Query3DSeg();

        if (segmentationModule == null)
        {
            Debug.LogError("Couldn't query the Face Module.");
            return false;
        }

        if (senseManager.Init().IsError())
        {
            Debug.LogError("Unable to initialize SenseManager.");
            return false;
        }

        return true;
    }
コード例 #2
0
        protected override bool Initialize()
        {
            this.GetSessionAndSenseManager();

            this.EnableColorStream();

            pxcmStatus sts = this.senseManager.Enable3DSeg();

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

            this.InitSenseManager();

            this.segmentation = this.senseManager.Query3DSeg();
            if (this.segmentation == null)
            {
                throw new Exception("Could not get 3D Segmentation.");
            }

            this.GetDevice();

            this.SetMirrorMode();

            this.initialized = true;

            return(true);
        }
コード例 #3
0
        protected override void Uninitialize()
        {
            if (this.segmentation != null)
            {
                this.segmentation.Dispose();
                this.segmentation = null;
            }

            base.Uninitialize();
        }
コード例 #4
0
        private void Uninitialize()
        {
            if (senseManager != null)
            {
                senseManager.Dispose();
                senseManager = null;
            }

            if (segmentation != null)
            {
                segmentation.Dispose();
                segmentation = null;
            }
        }
コード例 #5
0
        private void Initialize()
        {
            try {
                // SenseManagerを生成する
                senseManager = PXCMSenseManager.CreateInstance();

                // カラーストリームを有効にする
                var sts = senseManager.EnableStream(PXCMCapture.StreamType.STREAM_TYPE_COLOR,
                                                    COLOR_WIDTH, COLOR_HEIGHT, COLOR_FPS);
                if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    throw new Exception("カラーストリームの有効化にしました");
                }

                // セグメンテーションを有効にする
                sts = senseManager.Enable3DSeg();
                if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    throw new Exception("セグメンテーションの有効化にしました");
                }

                // パイプラインを初期化する
                sts = senseManager.Init();
                if (sts < pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    throw new Exception("初期化に失敗しました");
                }

                // ミラー表示にする
                senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode(
                    PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL);

                // セグメンテーションオブジェクトを取得する
                segmentation = senseManager.Query3DSeg();
                if (segmentation == null)
                {
                    throw new Exception("セグメンテーションの取得に失敗しました");
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
                Close();
            }
        }
コード例 #6
0
        private void AcquireThread()
        {
            // Stream data
            while (senseManager.AcquireFrame(true) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
            {
                if (click == true)
                {
                    Thread.Sleep(500);
                    click = false;
                }
                // Retrieve the results
                PXCM3DSeg segmentation = senseManager.Query3DSeg();

                if (segmentation != null)
                {
                    // Get the segmented image
                    PXCMImage segmentedImage = segmentation.AcquireSegmentedImage();

                    if (segmentedImage != null)
                    {
                        // Access the segmented image data
                        PXCMImage.ImageData segmentedImageData;
                        segmentedImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_RGB32, out segmentedImageData);

                        // Lock the backdrop image bitmap bits into system memory and access its data
                        // (Reference: https://msdn.microsoft.com/en-us/library/5ey6h79d%28v=vs.110%29.aspx
                        // (Reference: http://csharpexamples.com/fast-image-processing-c/)
                        Rectangle  imageRect          = new Rectangle(0, 0, WIDTH, HEIGHT);
                        BitmapData backdropBitmapData = backdrop.LockBits(imageRect, ImageLockMode.ReadWrite, backdrop.PixelFormat);
                        int        bytesPerPixel      = Bitmap.GetPixelFormatSize(backdropBitmapData.PixelFormat) / 8;
                        int        widthInBytes       = WIDTH * bytesPerPixel;

                        for (int h = 0; h < HEIGHT; h++)
                        {
                            // Use unsafe keyword to work with pointers for faster image processing
                            // (Required setting: Project -> Properties -> Build -> Allow unsafe code)
                            unsafe
                            {
                                byte *segmentedImagePixel = (byte *)segmentedImageData.planes[0] + h * segmentedImageData.pitches[0];

                                for (int w = 0; w < widthInBytes; w = w + bytesPerPixel)
                                {
                                    byte *backdropPixel = (byte *)backdropBitmapData.Scan0 + (h * backdropBitmapData.Stride);

                                    // Substitute segmented background pixels (those containing an alpha channel of zero)
                                    // with pixels from the selected backdrop image, if the checkbox is selected
                                    if ((segmentedImagePixel[3] <= 0))
                                    {
                                        segmentedImagePixel[0] = backdropPixel[w];
                                        segmentedImagePixel[1] = backdropPixel[w + 1];
                                        segmentedImagePixel[2] = backdropPixel[w + 2];
                                    }

                                    segmentedImagePixel += 4;
                                }
                            }
                        }

                        // Unlock the backdrop image bitmap bits
                        backdrop.UnlockBits(backdropBitmapData);

                        // Export the image data to a bitmap
                        Bitmap bitmap = segmentedImageData.ToBitmap(0, segmentedImage.info.width, segmentedImage.info.height);

                        // Update the UI by delegating work to the Dispatcher associated with the UI thread
                        this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
                        {
                            imgBackdrop.Source = ImageUtils.ConvertBitmapToWpf(bitmap);
                        }));

                        // Optionally save a snapshot of the image (captureSnapshot is set in the Capture button's event handler)
                        if (captureSnapshot)
                        {
                            bitmap.Save(path + "MyPic.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                            captureSnapshot = false;
                        }

                        segmentedImage.ReleaseAccess(segmentedImageData);
                        segmentedImage.Dispose();
                        segmentation.Dispose();
                        bitmap.Dispose();
                    }
                }

                // Resume next frame processing
                senseManager.ReleaseFrame();
            }
        }
コード例 #7
0
        void Update()
        {
            //Dynamically Pause/Enable Modules
            int numberOfEnabledModules = 0;

            foreach (var option in _senseOptions)
            {
                if (option.RefCounter == 0 && option.Enabled)
                {
                    if (option.ModuleCUID > 0)
                    {
                        SenseManager.PauseModule(option.ModuleCUID, true);
                    }
                    option.Enabled = false;
                }
                else if (option.RefCounter > 0 && !option.Enabled)
                {
                    if (!option.Initialized)
                    {
                        OnDisable();
                        OnEnable();
                        Start();
                    }
                    if (option.ModuleCUID > 0)
                    {
                        SenseManager.PauseModule(option.ModuleCUID, false);
                    }
                    option.Enabled = true;
                }

                if (option.Enabled)
                {
                    numberOfEnabledModules++;
                }
            }

            //Update Speech commands if changed
            if (_speechCommandsChanged)
            {
                UpdateSpeechCommands();
                SpeechManager.Reset();
            }

            // Every frame update all the data
            if (Initialized && numberOfEnabledModules > 0)
            {
                _sts = SenseManager.AcquireFrame(true, 0);
                if (_sts == pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.VideoColorStream).Enabled)
                    {
                        if (ImageRgbOutput != null)
                        {
                            ImageRgbOutput.Dispose();
                        }

                        if (_captureSample == null)
                        {
                            _captureSample = SenseManager.QuerySample();
                        }

                        if (_captureSample.color != null)
                        {
                            ImageRgbOutput = _captureSample.color;
                            ImageRgbOutput.AddRef();
                        }
                    }
                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.VideoDepthStream).Enabled ||
                        _senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.PointCloud).Enabled)
                    {
                        if (ImageDepthOutput != null)
                        {
                            ImageDepthOutput.Dispose();
                        }

                        if (_captureSample == null)
                        {
                            _captureSample = SenseManager.QuerySample();
                        }

                        if (_captureSample.depth != null)
                        {
                            ImageDepthOutput = _captureSample.depth;
                            ImageDepthOutput.AddRef();

                            /* GZ
                             *                          if (!_isInitBlob)
                             *                          {
                             *                                  PXCMImage.ImageInfo info = ImageDepthOutput.QueryInfo();
                             *                                  BlobExtractor.Init(info);
                             *                                  BlobExtractor.SetMaxBlobs(MaxBlobsToDetect);
                             *                                  _isInitBlob = true;
                             *                          }
                             */

                            if (PointCloud == null)
                            {
                                PointCloud = new PXCMPoint3DF32[ImageDepthOutput.info.width * ImageDepthOutput.info.height];
                            }

                            if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.PointCloud).Enabled)
                            {
                                if (PointCloud == null)
                                {
                                    PointCloud = new PXCMPoint3DF32[ImageDepthOutput.info.width * ImageDepthOutput.info.height];
                                }

                                _sts = Projection.QueryVertices(ImageDepthOutput, PointCloud);
                            }

                            if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.UVMap).Enabled)
                            {
                                if (UvMap == null)
                                {
                                    UvMap = new PXCMPointF32[ImageDepthOutput.info.width * ImageDepthOutput.info.height];
                                }

                                Projection.QueryUVMap(ImageDepthOutput, UvMap);
                            }
                        }
                    }
                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.VideoIRStream).Enabled)
                    {
                        if (ImageIROutput != null)
                        {
                            ImageIROutput.Dispose();
                        }

                        if (_captureSample == null)
                        {
                            _captureSample = SenseManager.QuerySample();
                        }

                        if (_captureSample.ir != null)
                        {
                            ImageIROutput = _captureSample.ir;
                            ImageIROutput.AddRef();
                        }
                    }

                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.VideoSegmentation).Enabled)
                    {
                        if (Image3DSegmentationOutput != null)
                        {
                            Image3DSegmentationOutput.Dispose();
                        }

                        PXCM3DSeg seg = SenseManager.Query3DSeg();

                        if (seg != null)
                        {
                            Image3DSegmentationOutput = seg.AcquireSegmentedImage();
                        }
                    }

                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.Face).Enabled)
                    {
                        FaceModuleOutput.Update();
                    }

                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.Hand).Enabled)
                    {
                        HandDataOutput.Update();
                    }

                    if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.Blob).Enabled)
                    {
                        BlobDataOutput.Update();
                    }

                    _captureSample = null;

                    SenseManager.ReleaseFrame();
                }

                //Speech
                if (_senseOptions.Find(i => i.ID == SenseOption.SenseOptionID.Speech).Enabled)
                {
                    SpeechManager.QueryRecognizedCommands(out SpeechOutput);
                }
            }
        }
コード例 #8
0
        private void Initialize()
        {
            try {
                // SenseManagerを生成する
                senseManager = PXCMSenseManager.CreateInstance();

                // カラーストリームを有効にする
                var sts = senseManager.EnableStream( PXCMCapture.StreamType.STREAM_TYPE_COLOR,
                    COLOR_WIDTH, COLOR_HEIGHT, COLOR_FPS );
                if ( sts < pxcmStatus.PXCM_STATUS_NO_ERROR ) {
                    throw new Exception( "カラーストリームの有効化にしました" );
                }

                // セグメンテーションを有効にする
                sts = senseManager.Enable3DSeg();
                if ( sts < pxcmStatus.PXCM_STATUS_NO_ERROR ) {
                    throw new Exception( "セグメンテーションの有効化にしました" );
                }

                // パイプラインを初期化する
                sts =  senseManager.Init();
                if ( sts < pxcmStatus.PXCM_STATUS_NO_ERROR ) {
                    throw new Exception( "初期化に失敗しました" );
                }

                // ミラー表示にする
                senseManager.QueryCaptureManager().QueryDevice().SetMirrorMode(
                    PXCMCapture.Device.MirrorMode.MIRROR_MODE_HORIZONTAL );

                // セグメンテーションオブジェクトを取得する
                segmentation = senseManager.Query3DSeg();
                if ( segmentation == null ) {
                    throw new Exception( "セグメンテーションの取得に失敗しました" );
                }
            }
            catch ( Exception ex ) {
                MessageBox.Show( ex.Message );
                Close();
            }
        }
コード例 #9
0
        private void Uninitialize()
        {
            if ( senseManager != null ) {
                senseManager.Dispose();
                senseManager = null;
            }

            if ( segmentation != null ) {
                segmentation.Dispose();
                segmentation = null;
            }
        }