Esempio n. 1
0
        public void Start()
        {
            //create session
            PXCMSession session;
            pxcmStatus  status = PXCMSession.CreateInstance(out session);

            if (IsError(status))
            {
                OnError(CamEvent.FAILED_TO_CREATE_SDK_SESSION);
                return;
            }

            //create gesture-module
            PXCMBase gestureBase;

            status = session.CreateImpl(PXCMGesture.CUID, out gestureBase);
            if (IsError(status))
            {
                OnError(CamEvent.FAILED_TO_LOAD_GESTURE_RECOGNITION);
                session.Dispose();
                return;
            }

            //create gesture-profile
            PXCMGesture gesture = (PXCMGesture)gestureBase;

            PXCMGesture.ProfileInfo profileInfo;
            status = gesture.QueryProfile(0, out profileInfo);
            profileInfo.activationDistance = 70;

            //setup gesture-capture
            UtilMCapture capture = new UtilMCapture(session);

            status = capture.LocateStreams(ref profileInfo.inputs);
            if (IsError(status))
            {
                OnError(CamEvent.FAILED_TO_LOCATE_CAPTURE_MODULE);
                gesture.Dispose();
                capture.Dispose();
                session.Dispose();
                return;
            }

            status = gesture.SetProfile(ref profileInfo);
            status = gesture.SubscribeAlert(this.OnAlertHandler);
            status = gesture.SubscribeGesture(100, this.OnGesureHandler);

            //start capture of frames
            bool device_lost = false;

            PXCMImage[] images = new PXCMImage[PXCMCapture.VideoStream.STREAM_LIMIT];
            PXCMScheduler.SyncPoint[] syncPoints = new PXCMScheduler.SyncPoint[2];

            while (_tracking)
            {
                status = capture.ReadStreamAsync(images, out syncPoints[0]);
                if (IsError(status))
                {
                    if (status == pxcmStatus.PXCM_STATUS_DEVICE_LOST)
                    {
                        if (!device_lost)
                        {
                            OnError(CamEvent.DEVICE_DISCONNECTED);
                        }
                        device_lost = true;
                        continue;
                    }
                    OnError(CamEvent.DEVICE_FAILED);
                    break;
                }
                if (device_lost)
                {
                    OnNotify(CamEvent.DEVICE_RECONNECTED);
                    device_lost = false;
                }

                status = gesture.ProcessImageAsync(images, out syncPoints[1]);
                if (IsError(status))
                {
                    break;
                }

                PXCMScheduler.SyncPoint.SynchronizeEx(syncPoints);
                if (syncPoints[0].Synchronize(0) >= pxcmStatus.PXCM_STATUS_NO_ERROR)
                {
                    PXCMGesture.GeoNode data;
                    status = gesture.QueryNodeData(0, PXCMGesture.GeoNode.Label.LABEL_BODY_HAND_PRIMARY, out data);
                    if (!IsError(status))
                    {
                        if (ShapeHelper.IsPointInsideRect(data.positionImage.x, data.positionImage.y, Constants.FoVWindow))
                        {
                            //adjust the point to field-of-view window
                            Point cameraPoint = new Point(data.positionImage.x - Constants.FoVWindow.X, data.positionImage.y - Constants.FoVWindow.Y);
                            //cameraPoint = ShapeHelper.RotatePoint(cameraPoint, Constants.FoVCenter, Constants.RotationAngle);
                            OnMovement(cameraPoint);

                            if (data.opennessState != _previousOpenness)
                            {
                                OnOpenClose(data.opennessState, data.openness);
                                _previousOpenness = data.opennessState;
                            }
                        }
                        else
                        {
                            OnNotify(CamEvent.HOVERING_OUTSIDE);
                        }
                    }
                }

                foreach (PXCMScheduler.SyncPoint p in syncPoints)
                {
                    if (p != null)
                    {
                        p.Dispose();
                    }
                }
                foreach (PXCMImage img in images)
                {
                    if (img != null)
                    {
                        img.Dispose();
                    }
                }
            }

            if (gesture != null)
            {
                gesture.Dispose();
            }
            if (capture != null)
            {
                capture.Dispose();
            }
            if (session != null)
            {
                session.Dispose();
            }
        }
        public GesturePipeline(Form1 form)
            : base()
        {
            //------------------------------
            // Creates datastructures for
            // the PCSDK
            //------------------------------
            UtilMPipeline pp;         //The pipeline

            PXCMGesture.Blob bdata;   //The intrepred blobs
            PXCMImage        bimage;  //The frame from the camera

            PXCMImage.ImageData data; //The data contained in the frame.
            PXCMImage.ImageInfo info; //Metadata about the frame.
            int color;                //The "color" for the hand in question.

            //------------------------------


            //------------------------------
            // Initilize the datastructures
            // defined above.
            //------------------------------
            Coord[] locations;
            pp = new UtilMPipeline();
            pp.EnableGesture();
            pp.EnableImage(PXCMImage.ColorFormat.COLOR_FORMAT_DEPTH);
            //-----------------------------

            //initilize the pointer for the WMC window.
            IntPtr handle = GetDesktopWindow();


            //----------------------------------
            // Create and initilize the bitmaps
            // that the frames will be comapred
            // against.
            //---------------------------------
            Bitmap pause       = convertBitmap(KwisStandalone.Properties.Resources.STOP);
            Bitmap fastForward = convertBitmap(KwisStandalone.Properties.Resources.FAST_FORWARD);
            Bitmap rewind      = convertBitmap(KwisStandalone.Properties.Resources.REWIND);
            Bitmap play        = convertBitmap(KwisStandalone.Properties.Resources.PLAY);
            Bitmap menu        = convertBitmap(KwisStandalone.Properties.Resources.MENU);
            Bitmap volUp       = convertBitmap(KwisStandalone.Properties.Resources.VOL_UP);
            Bitmap volDown     = convertBitmap(KwisStandalone.Properties.Resources.VOL_DOWN);

            //--------------------------------

            // initial the template matching algorithm.
            templateMatching = new ExhaustiveTemplateMatching(0.75f);


            /* If we were able to aquire a frame from the camera,
             * start the main application logic.
             * */
            if (pp.Init())
            {
                //Loop forever as long as the form is still open.
                while (!form.closing)
                {
                    if (!pp.AcquireFrame(true))
                    {
                        MessageBox.Show("Failed to aquire a frame.", "Kwi-S", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
                        break;
                    }

                    //Aquire the gesture data for the frame.
                    using (PXCMGesture gesture = pp.QueryGesture())
                    {
                        //Load gesture data into respective data structures.
                        gesture.QueryBlobData(PXCMGesture.Blob.Label.LABEL_SCENE, 0, out bdata);
                        gesture.QueryBlobImage(PXCMGesture.Blob.Label.LABEL_SCENE, 0, out bimage);
                        bimage.AcquireAccess(PXCMImage.Access.ACCESS_READ, out data);
                        info = bimage.imageInfo;



                        /* Although this says left hand,
                         * this actually aquires the right hand.
                         * This is from trial and error and contradicts
                         * both the documentation and common sense.  A bug
                         * in the SDK?
                         * */
                        color = (int)bdata.labelLeftHand;

                        //Store the pixels that include the hand specified above.
                        locations = new Coord[info.width * info.height];
                        handLocation(ref locations, data, (int)info.width);

                        //create a new bitmap from this data.
                        Bitmap bitmap = createBitmap(ref locations, (int)info.width, (int)info.height);


                        /* Zero is the left hand (actuall right).  So, if we detected this,
                         * then lets see if it matches any of the stored reference images.
                         * */
                        if (color == 0)
                        {
                            try
                            {
                                if (compare(ref bitmap, ref pause))
                                {
                                    clickAndSendKey(RemoteKey.PLAY_PAUSE);
                                }
                                else if (compare(ref bitmap, ref play))
                                {
                                    clickAndSendKey(RemoteKey.PLAY);
                                }
                                else if (compare(ref bitmap, ref fastForward))
                                {
                                    clickAndSendKey(RemoteKey.FAST_FORWARD);
                                }

                                else if (compare(ref bitmap, ref rewind))
                                {
                                    clickAndSendKey(APPCOMMAND_MEDIA_REWIND, handle);
                                }
                                else if (compare(ref bitmap, ref menu))
                                {
                                    clickAndSendKey(RemoteKey.STOP);
                                }
                                else if (compare(ref bitmap, ref volUp))
                                {
                                    clickAndSendKey(RemoteKey.VOL_UP);
                                }
                                else if (compare(ref bitmap, ref volDown))
                                {
                                    clickAndSendKey(RemoteKey.VOL_DOWN);
                                }
                            }
                            catch (InvalidImagePropertiesException iipe)
                            {
                            }
                        }

                        //Clean up the datastructures in prep for a new frame.
                        bitmap.Dispose();
                        bimage.ReleaseAccess(ref data);
                        bimage.Dispose();
                        gesture.Dispose();
                    }

                    pp.ReleaseFrame();
                }
            }
        }