Search code examples
c#eventskinect-sdknui

Increase variable once on gesture detect


I'm using some third party libraries to gesture detection in my Kinect based application. Everything works but I have only one small problem and it would be great if someone of you could help me.

To use this library I had to write kinect_SkeletonFrameReady like this:

    void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        Skeleton[] skeletons = new Skeleton[0];

        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if (skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
            }
        }

        if (skeletons.Length > 0 && inter != null)
        {
            Skeleton first = null;
            int b = 0;
            while (b < skeletons.Length && first == null)
            {
                if (skeletons[b].TrackingState == SkeletonTrackingState.Tracked)
                    first = skeletons[b];
                b++;
            }
            if (first != null)
            {
                GDLWatch.Stop();
                double TimeHelp = GDLWatch.Elapsed.TotalSeconds;
                GDLWatch.Reset();
                GDLWatch.Start();
                Point3D[] bodyParts = GenerateBodyPartArray(first, 0);
                System.String[] con = inter.ReturnConclusions(bodyParts, 0, TimeHelp);
                System.String conclusionsString = "";
                for (int a = 0; a < con.Length; a++)
                {
                    conclusionsString += con[a] + "\r\n";
                    System.Diagnostics.Debug.WriteLine(conclusionsString);
                    if (conclusionsString.Contains("!"))
                    {
                        this.alreadyDone++;
                        this.RaisePropertyChanged(() => this.AlreadyDone);
                    }
                }
            }
        }
    }

The only problem is that variable allreadyDone is not increased once, but many times. I think that the data from sensor, match for gesture description for some period of time. How to increase this variable only once, when the gesture appears for the first time? Adding something lik break or return does not work here.


Solution

  • I found a solution. Had to add a property:

        private bool _isGestureCatched;
        private bool IsGestureCatched
        {
            get { return _isGestureCatched; }
            set
            {
                if (_isGestureCatched != value)
                {
                    if (!IsGestureCatched)
                    {
                        this._alreadyDone++;
                        this.RaisePropertyChanged(() => this.AlreadyDone);
                    }            
                }
    
                _isGestureCatched = value;
            }
        } 
    

    And then in SkeletonFrameReady have to set it like this:

    if (conclusionsString.Contains("!"))
                    {
                        IsGestureCatched = true;
                    }
                    else
                    {
                        IsGestureCatched = false;
                    }
    

    It is working, only sometimes this code catched one gesture twice so if someone have better idea, is welcome, reply from @dav_i is helpfull