Example #1
0
        public void MoveFrameBackwards(GestureFrame f)
        {
            if (f == null)
            {
                return;
            }

            Gesture g = (Gesture)TheWorkspace.DataContext;

            if (g == null)
            {
                return;
            }

            int fIndex = g.Frames.IndexOf(f);

            if (fIndex == 0)
            {
                g.Frames.Move(fIndex, g.Frames.Count - 1);
            }
            else
            {
                g.Frames.Move(fIndex, fIndex - 1);
            }

            SyncEditorGrids();
        }
    /// <summary>
    /// Gets the normalized gesture data.
    /// </summary>
    /// <returns>
    /// The normalized gesture data.
    /// </returns>
    /// <param name='resolution'>
    /// Resolution to normalize to.
    /// </param>
    public GestureData getNormalizedGestureData(float resolution)
    {
        var dx = (this.maximumX - this.minimumX) * 1.0f;
        var dy = (this.maximumY - this.minimumY) * 1.0f;

        if (dx > dy)
        {
            dy = dx;
        }
        else
        {
            dx = dy;
        }

        GestureData normalizedData = new GestureData();

        for (var i = 0; i < this.frames.Count; i++)
        {
            GestureFrame frame = (GestureFrame)this.frames[i];

            float x = (frame.position.x * 1.0f - this.minimumX) / dx * resolution;
            float y = resolution - (frame.position.y * 1.0f - this.minimumY) / dy * resolution;

            GestureFrame normalizedFrame = new GestureFrame(new Vector(x, y, 0.0f), frame.velocity, frame.timestamp);
            normalizedData.appendFrame(normalizedFrame);
        }

        return(normalizedData);
    }
Example #3
0
        public void DeleteFrame(GestureFrame f)
        {
            if (f == null)
            {
                return;
            }

            Gesture g = (Gesture)TheWorkspace.DataContext;

            if (g == null)
            {
                return;
            }

            g.Frames.Remove(f);

            if (g.Frames.Count == 0)
            {
                g.Frames.Add(MakeNewGestureFrame()); // Do not let a gesture have 0 frames //TODO: having 0-framed gestures could be useful for creating named libraries of keyboard shortcuts as templates (say one library for PowerPoint, one for Media Player etc.) to which a user can then fill-in the gesture frames (would need the Visualizer to be aware of that and also maybe show the respective gestures colored as red - to signify they're not yet complete)
            }
            if (FramesListBox.SelectedItem == null)
            {
                FramesListBox.SelectedIndex = FramesListBox.Items.Count - 1;
            }

            SyncEditorGrids();
        }
    public void appendFrame(GestureFrame frame)
    {
        if (frame.position.x < this.minimumX)
        {
            this.minimumX = frame.position.x;
        }

        if (frame.position.y < this.minimumY)
        {
            this.minimumY = frame.position.y;
        }

        if (frame.position.z < this.minimumZ)
        {
            this.minimumZ = frame.position.z;
        }

        if (frame.position.x > this.maximumX)
        {
            this.maximumX = frame.position.x;
        }

        if (frame.position.y > this.maximumY)
        {
            this.maximumY = frame.position.y;
        }

        if (frame.position.z > this.maximumZ)
        {
            this.maximumZ = frame.position.z;
        }

        this.frames.Add(frame);
    }
Example #5
0
        private Gesture DeepCopyGesture(Gesture source)
        {
            Gesture target = new Gesture();

            target         = new Gesture();
            target.Name    = source.Name;
            target.Command = new ObservableCollection <Key>(source.Command);
            target.Hold    = source.Hold;  // Motherfucking System.Boolean is cocksucking value type
            target.Joint   = source.Joint; // Enums are also value type
            target.Frames  = new ObservableCollection <GestureFrame>();
            foreach (GestureFrame sourceFrame in source.Frames)
            {
                GestureFrame targetFrame = new GestureFrame();
                for (int i = 0; i < 400; i++)
                {
                    targetFrame.FrontCells[i] = new GestureFrameCell()
                    {
                        IndexInFrame = sourceFrame.FrontCells[i].IndexInFrame,
                        IsHotspot    = sourceFrame.FrontCells[i].IsHotspot
                    };
                    targetFrame.SideCells[i] = new GestureFrameCell()
                    {
                        IndexInFrame = sourceFrame.SideCells[i].IndexInFrame,
                        IsHotspot    = sourceFrame.SideCells[i].IsHotspot
                    };
                }
                target.Frames.Add(targetFrame);
            }
            return(target);
        }
    // Get the position of a gesture in the first previous frame that has it
    private void GetPrevalentPositionInPreviousFrame(GestureFrame prevalent)
    {
        // Loop through previous frames
        int prev = GetPreviousFrame();

        while (prev != currentFrame)
        {
            // Check if this previous frame has a true positive
            if (frames[prev].isPrevalentCalculated)
            {
                // Check if this true positive is the same than the one we are looking for
                if (frames[prev].prevalent.type == prevalent.type)
                {
                    // The gesture is updated. The position in the only value overwritten, the rest is the same
                    prevalent = frames[prev].prevalent;
                    return;
                }
            }
            prev--;
            if (prev < 0)
            {
                prev = nFrames - 1;
            }
        }
    }
Example #7
0
        /// <summary>
        /// Enable/disable rows on SideViewGrid according to selection on FrontViewGrid
        /// </summary>
        private void SyncEditorGrids_FrontToSide()
        {
            GestureFrame sf = (GestureFrame)FramesListBox.SelectedItem;

            GestureFrameCell[] fcs = (GestureFrameCell[])FVGrid.ItemsSource;
            GestureFrameCell[] scs = (GestureFrameCell[])SVGrid.ItemsSource;

            // 'If' overcomes FVGrid_SelectionChanged firing before everything else and syncing SVGrid to a different Frame's FVGrid
            if (Object.ReferenceEquals(sf.FrontCells, fcs) && Object.ReferenceEquals(sf.SideCells, scs))
            {
                IList      frontViewGrid_selectedCells = (IList)FVGrid.SelectedItems;
                List <int> frontViewGrid_selectedRows  = new List <int>();

                foreach (GestureFrameCell c in frontViewGrid_selectedCells)
                {
                    frontViewGrid_selectedRows.Add((int)(c.IndexInFrame / 20));
                }

                for (int i = 0; i < 400; i++)
                {
                    ListBoxItem sideViewGridItemContainer = (ListBoxItem)SVGrid.ItemContainerGenerator.ContainerFromIndex(i);
                    if (frontViewGrid_selectedRows.Contains((int)(i / 20)))
                    {
                        sideViewGridItemContainer.IsEnabled = true;
                    }
                    else
                    {
                        sideViewGridItemContainer.IsEnabled = false;
                        GestureFrame f = (GestureFrame)SVGrid.DataContext;
                        f.SideCells[i].IsHotspot = false;
                    }
                }
            }
        }
Example #8
0
        private void DeleteFrameButton_Click(object sender, RoutedEventArgs e) //reused at each frame
        {
            Button       b = (Button)sender;                                   //find the button clicked
            GestureFrame s = (GestureFrame)b.DataContext;                      //find the corresponding frame

            DeleteFrame(s);
        }
Example #9
0
    public void appendFrame(GestureFrame frame)
    {
        if (frame.position.x < this.minimumX) {
            this.minimumX = frame.position.x;
        }

        if (frame.position.y < this.minimumY) {
            this.minimumY = frame.position.y;
        }

        if (frame.position.z < this.minimumZ) {
            this.minimumZ = frame.position.z;
        }

        if (frame.position.x > this.maximumX) {
            this.maximumX = frame.position.x;
        }

        if (frame.position.y > this.maximumY) {
            this.maximumY = frame.position.y;
        }

        if (frame.position.z > this.maximumZ) {
            this.maximumZ = frame.position.z;
        }

        this.frames.Add(frame);
    }
Example #10
0
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            base.TouchesBegan(touches, evt);
            try
            {
                UITouch[] arr = evt.AllTouches.ToArray <UITouch>();
                if (arr.Length == 2)
                {
                    _resize = Math.Sqrt(Math.Abs(arr[0].LocationInView(this).X - arr[1].LocationInView(this).X) * Math.Abs(arr[0].LocationInView(this).X - arr[1].LocationInView(this).X) + Math.Abs(arr[0].LocationInView(this).Y - arr[1].LocationInView(this).Y) * Math.Abs(arr[0].LocationInView(this).Y - arr[1].LocationInView(this).Y));
                    //					_resizeReady = true;
                    ignore_action = false;
                }

                UITouch touch = touches.AnyObject as UITouch;
                if (touch != null)
                {
                    _touchesX = touch.LocationInView(this).X;
                    _touchesY = touch.LocationInView(this).Y;
                    GestureFrame _gi = (GestureFrame)this.Element;
                    EventArgs    e   = new EventArgs();
                    _gi.OnDown(this, e, (float)_touchesX, (float)_touchesY, (int)touch.TapCount);
                }
            }
            catch (Exception ex)
            {
            }
        }
Example #11
0
 // Remove the information of the frame. Remember that it is a circular buffer. It recycles frames
 public void Clear()
 {
     gestures.Clear();
     gesturesCounters.Clear();
     prevalent             = new GestureFrame();
     isPrevalentCalculated = false;
 }
Example #12
0
    // Add a gesture to the list of detectred
    public void AddGesture(GestureFrame gesture)
    {
        gestures.Add(gesture);

        // Update the counter of gestures. Increases the gesture counter by 1
        UpdateCounters(gesture.type, 1);
    }
Example #13
0
        public override void TouchesMoved(NSSet touches, UIEvent evt)
        {
            base.TouchesMoved(touches, evt);
            try
            {
                GestureFrame _gi = (GestureFrame)this.Element;
                double       rs  = DetectResizeMotion(evt.AllTouches.ToArray <UITouch>());
                if (-1 != rs)
                {
                    EventArgs e = new EventArgs();
                    _gi.OnResize(this, e, _resize, rs);
                }
                else if (!ignore_action)
                {
                    UITouch touch = touches.AnyObject as UITouch;
                    if (touch != null)
                    {
                        nfloat offsetX = _touchesX - touch.LocationInView(touch.View).X;
                        nfloat offsetY = _touchesY - touch.LocationInView(touch.View).Y;
                        float  scale   = (float)UIScreen.MainScreen.Scale;

                        EventArgs e = new EventArgs();
                        _gi.OnMove(this, e, (float)(offsetX) / scale, (float)(offsetY) / scale);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Example #14
0
        private void MoveFrameForwardButton_Click(object sender, RoutedEventArgs e) //reused at each frame
        {
            Button       b = (Button)sender;                                        //find the button clicked
            GestureFrame f = (GestureFrame)b.DataContext;                           //find the corresponding frame

            MoveFrameForward(f);
        }
    // For debugging
    void OnGUI()
    {
        GestureFrame currentGesture  = phoneCamera.GetCurrentGesture();
        Vector4      xywhScreenCoord = phoneCamera.CamCoordinatesToScreenCoordinates(currentGesture.x, currentGesture.y, currentGesture.width, currentGesture.height);

        GUILayout.BeginArea(new Rect(500, 20, 250, 120));
        GUILayout.Label(message);
        GUILayout.EndArea();
    }
    // Get a list of detected gestures from the list retrieved from OpenCV
    // This function just converts the list of detected gestures retrieved from OpenCV to the format that we can use in Unity
    public static List <GestureFrame> GetList(GestureType type, CvDetectedGesture[] gestures, int n)
    {
        List <GestureFrame> list = new List <GestureFrame>();

        for (int i = 0; i < n; i++)
        {
            GestureFrame g = new GestureFrame(type, gestures[i]);
            list.Add(g);
        }

        return(list);
    }
Example #17
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FInvalidateConnect)
            {
                if (runtime != null)
                {
                    this.runtime.SkeletonFrameReady -= SkeletonReady;
                }

                if (this.FInRuntime.IsConnected)
                {
                    //Cache runtime node
                    this.runtime = this.FInRuntime[0];

                    if (runtime != null)
                    {
                        this.FInRuntime[0].SkeletonFrameReady += SkeletonReady;
                    }
                }

                this.FInvalidateConnect = false;
            }

            if (this.FInvalidate)
            {
                lock (m_lock)
                {
                    this.FOutId.SliceCount   = this.LastGestures.Count;
                    this.FOutType.SliceCount = this.LastGestures.Count;

                    int cnt = 0;
                    foreach (int k in this.LastGestures.Keys)
                    {
                        this.FOutId[cnt] = k;

                        GestureFrame gf = this.LastGestures[k];

                        this.FOutType[cnt] = gf.Gesture;


                        this.FOutNew[cnt] = gf.IsNew;

                        gf.IsNew = false;
                        cnt++;
                    }
                }

                this.FInvalidate = false;
            }
        }
        private void MoveFrameBackwardButton_Click(object sender, RoutedEventArgs e)
        {
            Button       b      = (Button)sender;
            GestureFrame f      = (GestureFrame)b.DataContext;
            Gesture      g      = (Gesture)TheWorkspace.DataContext;
            int          fIndex = g.Frames.IndexOf(f);

            if (fIndex == 0)
            {
                g.Frames.Move(fIndex, g.Frames.Count - 1);
            }
            else
            {
                g.Frames.Move(fIndex, fIndex - 1);
            }
            SyncEditorGrids();
        }
        private void DeleteFrameButton_Click(object sender, RoutedEventArgs e)
        {
            Button       b = (Button)sender;
            GestureFrame s = (GestureFrame)b.DataContext;
            Gesture      g = (Gesture)TheWorkspace.DataContext;

            g.Frames.Remove(s);
            if (g.Frames.Count == 0)
            {
                g.Frames.Add(MakeNewGestureFrame());                // Do not let a gesture have 0 frames
            }
            if (FramesListBox.SelectedItem == null)
            {
                FramesListBox.SelectedIndex = FramesListBox.Items.Count - 1;
            }
            SyncEditorGrids();
        }
Example #20
0
        private GestureFrame MakeNewGestureFrame()
        {
            GestureFrame newFrame = new GestureFrame();

            for (int i = 0; i < 400; i++)
            {
                newFrame.FrontCells[i] = new GestureFrameCell()
                {
                    IndexInFrame = i, IsHotspot = false
                };
                newFrame.SideCells[i] = new GestureFrameCell()
                {
                    IndexInFrame = i, IsHotspot = false
                };
            }
            return(newFrame);
        }
Example #21
0
        private GestureFrame DeepCopyGestureFrame(GestureFrame sourceFrame)
        {
            GestureFrame targetFrame = new GestureFrame();

            for (int i = 0; i < 400; i++)
            {
                targetFrame.FrontCells[i] = new GestureFrameCell()
                {
                    IndexInFrame = sourceFrame.FrontCells[i].IndexInFrame,
                    IsHotspot    = sourceFrame.FrontCells[i].IsHotspot
                };
                targetFrame.SideCells[i] = new GestureFrameCell()
                {
                    IndexInFrame = sourceFrame.SideCells[i].IndexInFrame,
                    IsHotspot    = sourceFrame.SideCells[i].IsHotspot
                };
            }
            return(targetFrame);
        }
Example #22
0
        public void LoadGestureCollection(object parameter)
        {
            // Conjure file explorer
            OpenFileDialog openDialog = new OpenFileDialog();

            // F*****g around with file formats
            openDialog.Filter = "Hotspotizer Gesture files (*.hsjson)|*.hsjson";
            // Read and load if dialog returns OK
            if (openDialog.ShowDialog() == true)
            {
                string json = File.ReadAllText(openDialog.FileName);
                // DeserializeObject() does not appear to correctly deserialize Gesture objects
                // Below is a kinda-dirty solution around that
                List <Gesture> sourceList = JsonConvert.DeserializeObject <List <Gesture> >(json);
                while (GestureCollection.Count > 0)
                {
                    GestureCollection.RemoveAt(0);
                }
                foreach (Gesture sourceGesture in sourceList)
                {
                    Gesture targetGesture = new Gesture();
                    targetGesture.Name    = sourceGesture.Name;
                    targetGesture.Command = new ObservableCollection <Key>(sourceGesture.Command);
                    targetGesture.Hold    = sourceGesture.Hold;
                    targetGesture.Joint   = sourceGesture.Joint;
                    while (targetGesture.Frames.Count > 0)
                    {
                        targetGesture.Frames.RemoveAt(0);
                    }
                    foreach (GestureFrame sourceFrame in sourceGesture.Frames)
                    {
                        GestureFrame targetFrame = new GestureFrame();
                        for (int i = 0; i < 400; i++)
                        {
                            targetFrame.FrontCells[i] = sourceFrame.FrontCells[i];
                            targetFrame.SideCells[i]  = sourceFrame.SideCells[i];
                        }
                        targetGesture.Frames.Add(targetFrame);
                    }
                    GestureCollection.Add(targetGesture);
                }
            }
        }
Example #23
0
        public void LoadGestureCollection(string filename)
        {
            string json = File.ReadAllText(filename);
            // DeserializeObject() does not appear to correctly deserialize Gesture objects
            // Below is a kinda-dirty solution around that
            List <Gesture> sourceList = JsonConvert.DeserializeObject <List <Gesture> >(json);

            GestureCollection.Clear();

            foreach (Gesture sourceGesture in sourceList)
            {
                RemoveNoneKeys(sourceGesture.Command); //Seems somewhere at the serialization or deserialization Key.None creeps in, so remove it

                //copy sourceGesture to targetGesture //TODO: check why this copying is needed
                Gesture targetGesture = new Gesture()
                {
                    Name    = sourceGesture.Name,
                    Command = new ObservableCollection <Key>(sourceGesture.Command),
                    Hold    = sourceGesture.Hold,
                    Joint   = sourceGesture.Joint
                };
                //copy the frames too (note: this is not the same as DeepCopyGestureFrame)
                foreach (GestureFrame sourceFrame in sourceGesture.Frames)
                {
                    GestureFrame targetFrame = new GestureFrame();
                    for (int i = 0; i < 400; i++)
                    {
                        targetFrame.FrontCells[i] = sourceFrame.FrontCells[i];
                        targetFrame.SideCells[i]  = sourceFrame.SideCells[i];
                    }
                    targetGesture.Frames.Add(targetFrame);
                }

                GestureCollection.Add(targetGesture);

                if (GestureCollectionLoaded != null)
                {
                    GestureCollectionLoaded(this, EventArgs.Empty);
                }
            }
        }
Example #24
0
        private void OnGestureRecognized(object sender, GestureEventArgs e)
        {
            lock (m_lock)
            {
                GestureFrame gf = new GestureFrame();
                gf.Gesture = e.GestureType;
                gf.IsNew   = true;
                this.LastGestures[e.TrackingId] = gf;
            }

            /*switch (e.GestureType)
             * {
             *  case GestureType.Menu:
             *      Gesture = "Menu";
             *      break;
             *  case GestureType.WaveRight:
             *      Gesture = "Wave Right";
             *      break;
             *  case GestureType.WaveLeft:
             *      Gesture = "Wave Left";
             *      break;
             *  case GestureType.JoinedHands:
             *      Gesture = "Joined Hands";
             *      break;
             *  case GestureType.SwipeLeft:
             *      Gesture = "Swipe Left";
             *      break;
             *  case GestureType.SwipeRight:
             *      Gesture = "Swipe Right";
             *      break;
             *  case GestureType.ZoomIn:
             *      Gesture = "Zoom In";
             *      break;
             *  case GestureType.ZoomOut:
             *      Gesture = "Zoom Out";
             *      break;
             *
             *  default:
             *      break;
             * }*/
        }
    // Get the most detected gesture in the previous frames. The most detected gesture is then nominated as the true positive of
    // the current frame because there is a high probability that it is still the same gesture
    public GestureFrame GetPrevalentGestureInPreviousFrames()
    {
        GestureFrame prevalent = new GestureFrame();

        // If the buffer has 1 frame then there are no previous frames
        if (nFrames == 1)
        {
            return(prevalent);
        }

        // Create a counter for all the gestures in the format (name, n)
        Dictionary <GestureType, int> counter = new Dictionary <GestureType, int>();

        // Loop through all the previous frames
        int prev = GetPreviousFrame();

        while (prev != currentFrame)
        {
            // Check if this previous frame has a true positive
            if (frames[prev].isPrevalentCalculated && frames[prev].prevalent.type != GestureType.NONE)
            {
                // Check if the counter does not have the true positive of this previous frame
                if (!counter.ContainsKey(frames[prev].prevalent.type))
                {
                    // If the counter does not exist then it is created and initialized with 1
                    counter.Add(frames[prev].prevalent.type, 1);
                }
                else
                {
                    // If the counter exists the it is increased
                    counter[frames[prev].prevalent.type] += 1;
                }
            }

            // Get the next previous frame
            prev--;
            if (prev < 0)
            {
                prev = nFrames - 1;
            }
        }

        // Check if the counter is empty. This means that the previous frames did not detect anything. Returns an empty object
        if (counter.Count == 0)
        {
            return(prevalent);
        }

        // Get the most detected gesture in all the previous frames (the counter that has more number)
        var max = counter.OrderByDescending(entry => entry.Value).FirstOrDefault();

        if (!max.Equals(new KeyValuePair <GestureType, int>())) // Check if it found anything
        {
            // At this point max.Key has the most detected gesture among all the previous frames
            prevalent.type = max.Key;

            // Get the position of the most detected gesture. This is necessary because the previous steps only found the NAME of the gesture,
            // not the position
            GetPrevalentPositionInPreviousFrame(prevalent);
        }

        return(prevalent);
    }
Example #26
0
    /// <summary>
    /// Detects the movement and builds the frame list when movement is detected.
    /// </summary>
    /// <param name='frame'>
    /// Leap frame.
    /// </param>
    public void DetectMovement(Frame frame)
    {
        GestureFrame gestureFrame = new GestureFrame(frame.Pointables[0].TipPosition, frame.Pointables[0].TipVelocity, frame.Timestamp);
        this.detectionHeap.Add(gestureFrame);

        Vector3 acceleration = Vector3.zero;
        Vector3 delta = Vector3.zero;
        Vector3 movementWork = Vector3.zero;
        Vector3 kinecticEnergy = Vector3.zero;

        float movementTotalWork;
        float kinecticEnergyTotal;

        if (this.detectionHeap.Count > this.maximumHeapSize) {
            this.detectionHeap.RemoveAt(0);

            GestureFrame firstFrame = (GestureFrame)this.detectionHeap[0];
            GestureFrame lastFrame = (GestureFrame)this.detectionHeap[this.maximumHeapSize - 1];

            float deltaTime = lastFrame.timestamp - firstFrame.timestamp;

            acceleration.x = (lastFrame.velocity.x - firstFrame.velocity.x) / deltaTime;
            acceleration.y = (lastFrame.velocity.y - firstFrame.velocity.y) / deltaTime;
            acceleration.z = (lastFrame.velocity.z - firstFrame.velocity.z) / deltaTime;

            this.movementDirection.x = acceleration.x > 0 ? 1 : -1;
            this.movementDirection.y = acceleration.y > 0 ? 1 : -1;
            this.movementDirection.z = acceleration.z > 0 ? 1 : -1;

            delta.x = (lastFrame.position.x - firstFrame.position.x);
            delta.y = (lastFrame.position.y - firstFrame.position.y);
            delta.z = (lastFrame.position.z - firstFrame.position.z);

            /*
             * http://en.wikipedia.org/wiki/Work_(physics)
             * Work calculation, using the acceleration previously calculated as the force to move the finger from one point to another
             */

            movementWork.x = delta.x * acceleration.x;
            movementWork.y = delta.y * acceleration.y;
            movementWork.z = delta.z * acceleration.z;

            /*
             * http://en.wikipedia.org/wiki/Kinetic_energy
             * Measures the pointable kinectic energy to detect when gesture have ended.
             */

            kinecticEnergy.x = Mathf.Pow(lastFrame.velocity.x, 2) * 0.5f * 1.0e-6f;
            kinecticEnergy.y = Mathf.Pow(lastFrame.velocity.y, 2) * 0.5f * 1.0e-6f;
            kinecticEnergy.z = Mathf.Pow(lastFrame.velocity.z, 2) * 0.5f * 1.0e-6f;

            kinecticEnergyTotal = kinecticEnergy.x + kinecticEnergy.y;

            if (Mathf.Abs(movementWork.x) > this.minimumRelevantWorkThreshold) {
                this.movementTotalWork.x += movementWork.x;
            }

            if (Mathf.Abs(movementWork.y) > this.minimumRelevantWorkThreshold) {
                this.movementTotalWork.y += movementWork.y;
            }

            if (Mathf.Abs(movementWork.z) > this.minimumRelevantWorkThreshold) {
                this.movementTotalWork.z += movementWork.z;
            }

            if (!this.performingMovement && lastFrame.timestamp - this.lastStoppedTime > 50000.0f) {
                this.movementTotalWork.x = 0.0f;
                this.movementTotalWork.y = 0.0f;
                this.movementTotalWork.z = 0.0f;
                this.lastStoppedTime = lastFrame.timestamp;
            }

            movementTotalWork = this.movementTotalWork.x + this.movementTotalWork.y;

            if (movementTotalWork > this.minimumStartMovementTotalWork && !this.performingMovement) {
                this.movementTotalWork.x = 0;
                this.movementTotalWork.y = 0;
                this.movementTotalWork.z = 0;

                this.performingMovement = true;

                if (!this.performingGesture) {
                    this.performingGesture = true;
                    this.gestureData = new GestureData();

                    if (this.callbackInstance != null)
                    {
                        this.callbackInstance.beginOfGestureCallback();
                    }
                }

            } else if (this.performingMovement && kinecticEnergyTotal < this.minimumEndMovementKinectEnergy) {

                this.movementTotalWork.x = 0.0f;
                this.movementTotalWork.y = 0.0f;
                this.movementTotalWork.z = 0.0f;

                this.performingMovement = false;
                this.lastStoppedTime = this.lastEndMovementTime = lastFrame.timestamp;
            }

            if (this.performingGesture) {
                this.gestureData.appendFrame(gestureFrame);
            }

            float timeSinceEndMovement = lastFrame.timestamp - this.lastEndMovementTime;

            if (!this.performingMovement && this.performingGesture && timeSinceEndMovement > 500000) {
                if (this.callbackInstance != null) {
                    this.callbackInstance.endOfGestureCallback(this.gestureData);
                }

                this.performingGesture = false;
            }
        }
    }
 // Set the true positive of the current frame
 public void SetPrevalentInCurrentFrame(GestureFrame prevalent)
 {
     frames[currentFrame].SetPrevalentGesture(prevalent);
 }
        private void HandleOnTap(object sender, EventArgs e)
        {
            GestureFrame _gi = (GestureFrame)this.Element;

            _gi.OnTap();
        }
        void HandleOnSwipeDown(object sender, EventArgs e)
        {
            GestureFrame _gi = (GestureFrame)this.Element;

            _gi.OnSwipeDown();
        }
Example #30
0
    /// <summary>
    /// Gets the normalized gesture data.
    /// </summary>
    /// <returns>
    /// The normalized gesture data.
    /// </returns>
    /// <param name='resolution'>
    /// Resolution to normalize to.
    /// </param>
    public GestureData getNormalizedGestureData(float resolution)
    {
        var dx = (this.maximumX - this.minimumX) * 1.0f;
        var dy = (this.maximumY - this.minimumY) * 1.0f;

        if (dx > dy)
            dy = dx;
        else
            dx = dy;

        GestureData normalizedData = new GestureData();

        for (var i = 0; i < this.frames.Count; i++) {
            GestureFrame frame = (GestureFrame)this.frames[i];

            float x = (frame.position.x * 1.0f - this.minimumX) / dx * resolution;
            float y = resolution - (frame.position.y * 1.0f - this.minimumY) / dy * resolution;

            GestureFrame normalizedFrame = new GestureFrame(new Vector(x, y, 0.0f), frame.velocity, frame.timestamp);
            normalizedData.appendFrame(normalizedFrame);
        }

        return normalizedData;
    }
        protected override void OnElementChanged(ElementChangedEventArgs <Frame> e)
        {
            base.OnElementChanged(e);

            swipeDown = new UISwipeGestureRecognizer(
                () =>
            {
                GestureFrame _gi = (GestureFrame)this.Element;
                _gi.OnSwipeDown();
            }
                )
            {
                Direction = UISwipeGestureRecognizerDirection.Down,
            };

            swipeUp = new UISwipeGestureRecognizer(
                () =>
            {
                GestureFrame _gi = (GestureFrame)this.Element;
                _gi.OnSwipeTop();
            }
                )
            {
                Direction = UISwipeGestureRecognizerDirection.Up,
            };

            swipeLeft = new UISwipeGestureRecognizer(
                () =>
            {
                GestureFrame _gi = (GestureFrame)this.Element;
                _gi.OnSwipeLeft();
            }
                )
            {
                Direction = UISwipeGestureRecognizerDirection.Left,
            };

            swipeRight = new UISwipeGestureRecognizer(
                () =>
            {
                GestureFrame _gi = (GestureFrame)this.Element;
                _gi.OnSwipeRight();
            }
                )
            {
                Direction = UISwipeGestureRecognizerDirection.Right,
            };

            if (e.NewElement == null)
            {
                if (swipeDown != null)
                {
                    this.RemoveGestureRecognizer(swipeDown);
                }
                if (swipeUp != null)
                {
                    this.RemoveGestureRecognizer(swipeUp);
                }
                if (swipeLeft != null)
                {
                    this.RemoveGestureRecognizer(swipeLeft);
                }
                if (swipeRight != null)
                {
                    this.RemoveGestureRecognizer(swipeRight);
                }
            }

            if (e.OldElement == null)
            {
                this.AddGestureRecognizer(swipeDown);
                this.AddGestureRecognizer(swipeUp);
                this.AddGestureRecognizer(swipeLeft);
                this.AddGestureRecognizer(swipeRight);
            }
        }
Example #32
0
    /// <summary>
    /// End of gesture movement callback.
    /// </summary>
    /// <param name='data'>
    /// Data of the gesture gathered.
    /// </param>
    public void endOfGestureCallback(GestureData data)
    {
        GestureUtils.EnumGestures gResult;

        Debug.Log("Gesture ended!");
        List <Vector2> pointList      = new List <Vector2>();
        GestureData    normalizedData = data.getNormalizedGestureData(CANVAS_SIZE);

        GestureFrame previousFrame = (GestureFrame)normalizedData.frames[0];

        pointList.Add(new Vector2(previousFrame.position.x, previousFrame.position.y));
        for (int i = 1; i < normalizedData.frames.Count; i++)
        {
            GestureFrame currentFrame = (GestureFrame)normalizedData.frames[i];

            if ((Math.Abs(currentFrame.position.x - previousFrame.position.x) < NORMAL_FRAME_VARIATION) &&
                (Math.Abs(currentFrame.position.y - previousFrame.position.y) < NORMAL_FRAME_VARIATION))
            {
                pointList.Add(new Vector2(currentFrame.position.x, currentFrame.position.y));
                previousFrame = currentFrame;
            }
        }

        //At gesture end, draw the normalized data.
        Vector2 previousPoint = pointList[0];

        for (int i = 1; i < pointList.Count; i++)
        {
            Vector2 currentPoint = pointList[i];
            DrawLine(canvas, previousPoint.x, previousPoint.y, currentPoint.x, currentPoint.y, Color.black);
            previousPoint = currentPoint;
        }

        canvas.Apply();

        //The checker method only receives canvas if you want the points used drawn on the screen,
        //can just receive a points list otherwise.
        gResult = gu.GestureChecker(pointList, canvas, CANVAS_SIZE);

        switch (gResult)
        {
        case GestureUtils.EnumGestures.DownZigZag:
            Debug.Log("Downwards Zig Zag!");
            status = "Downwards Zig Zag!";
            break;

        case GestureUtils.EnumGestures.RightZigZag:
            Debug.Log("Rightwards Zig Zag!");
            status = "Rightwards Zig Zag!";
            break;

        case GestureUtils.EnumGestures.Square:
            Debug.Log("Square!");
            status = "Square!";
            break;

        default:
            Debug.Log("No Gesture found.");
            status = "Nothing.";
            break;
        }
    }
        private void OnGestureRecognized(object sender, GestureEventArgs e)
        {
            lock (m_lock)
            {
                GestureFrame gf = new GestureFrame();
                gf.Gesture = e.GestureType;
                gf.IsNew = true;
                this.LastGestures[e.TrackingId] = gf;
            }

            /*switch (e.GestureType)
            {
                case GestureType.Menu:
                    Gesture = "Menu";
                    break;
                case GestureType.WaveRight:
                    Gesture = "Wave Right";
                    break;
                case GestureType.WaveLeft:
                    Gesture = "Wave Left";
                    break;
                case GestureType.JoinedHands:
                    Gesture = "Joined Hands";
                    break;
                case GestureType.SwipeLeft:
                    Gesture = "Swipe Left";
                    break;
                case GestureType.SwipeRight:
                    Gesture = "Swipe Right";
                    break;
                case GestureType.ZoomIn:
                    Gesture = "Zoom In";
                    break;
                case GestureType.ZoomOut:
                    Gesture = "Zoom Out";
                    break;

                default:
                    break;
            }*/
        }
    /// <summary>
    /// Detects the movement and builds the frame list when movement is detected.
    /// </summary>
    /// <param name='frame'>
    /// Leap frame.
    /// </param>
    public void DetectMovement(Frame frame)
    {
        GestureFrame gestureFrame = new GestureFrame(frame.Pointables[0].TipPosition, frame.Pointables[0].TipVelocity, frame.Timestamp);

        this.detectionHeap.Add(gestureFrame);

        Vector3 acceleration   = Vector3.zero;
        Vector3 delta          = Vector3.zero;
        Vector3 movementWork   = Vector3.zero;
        Vector3 kinecticEnergy = Vector3.zero;

        float movementTotalWork;
        float kinecticEnergyTotal;

        if (this.detectionHeap.Count > this.maximumHeapSize)
        {
            this.detectionHeap.RemoveAt(0);

            GestureFrame firstFrame = (GestureFrame)this.detectionHeap[0];
            GestureFrame lastFrame  = (GestureFrame)this.detectionHeap[this.maximumHeapSize - 1];

            float deltaTime = lastFrame.timestamp - firstFrame.timestamp;

            acceleration.x = (lastFrame.velocity.x - firstFrame.velocity.x) / deltaTime;
            acceleration.y = (lastFrame.velocity.y - firstFrame.velocity.y) / deltaTime;
            acceleration.z = (lastFrame.velocity.z - firstFrame.velocity.z) / deltaTime;

            this.movementDirection.x = acceleration.x > 0 ? 1 : -1;
            this.movementDirection.y = acceleration.y > 0 ? 1 : -1;
            this.movementDirection.z = acceleration.z > 0 ? 1 : -1;

            delta.x = (lastFrame.position.x - firstFrame.position.x);
            delta.y = (lastFrame.position.y - firstFrame.position.y);
            delta.z = (lastFrame.position.z - firstFrame.position.z);

            /*
             * http://en.wikipedia.org/wiki/Work_(physics)
             * Work calculation, using the acceleration previously calculated as the force to move the finger from one point to another
             */

            movementWork.x = delta.x * acceleration.x;
            movementWork.y = delta.y * acceleration.y;
            movementWork.z = delta.z * acceleration.z;

            /*
             * http://en.wikipedia.org/wiki/Kinetic_energy
             * Measures the pointable kinectic energy to detect when gesture have ended.
             */

            kinecticEnergy.x = Mathf.Pow(lastFrame.velocity.x, 2) * 0.5f * 1.0e-6f;
            kinecticEnergy.y = Mathf.Pow(lastFrame.velocity.y, 2) * 0.5f * 1.0e-6f;
            kinecticEnergy.z = Mathf.Pow(lastFrame.velocity.z, 2) * 0.5f * 1.0e-6f;

            kinecticEnergyTotal = kinecticEnergy.x + kinecticEnergy.y;

            if (Mathf.Abs(movementWork.x) > this.minimumRelevantWorkThreshold)
            {
                this.movementTotalWork.x += movementWork.x;
            }

            if (Mathf.Abs(movementWork.y) > this.minimumRelevantWorkThreshold)
            {
                this.movementTotalWork.y += movementWork.y;
            }

            if (Mathf.Abs(movementWork.z) > this.minimumRelevantWorkThreshold)
            {
                this.movementTotalWork.z += movementWork.z;
            }

            if (!this.performingMovement && lastFrame.timestamp - this.lastStoppedTime > 50000.0f)
            {
                this.movementTotalWork.x = 0.0f;
                this.movementTotalWork.y = 0.0f;
                this.movementTotalWork.z = 0.0f;
                this.lastStoppedTime     = lastFrame.timestamp;
            }

            movementTotalWork = this.movementTotalWork.x + this.movementTotalWork.y;

            if (movementTotalWork > this.minimumStartMovementTotalWork && !this.performingMovement)
            {
                this.movementTotalWork.x = 0;
                this.movementTotalWork.y = 0;
                this.movementTotalWork.z = 0;

                this.performingMovement = true;

                if (!this.performingGesture)
                {
                    this.performingGesture = true;
                    this.gestureData       = new GestureData();

                    if (this.callbackInstance != null)
                    {
                        this.callbackInstance.beginOfGestureCallback();
                    }
                }
            }
            else if (this.performingMovement && kinecticEnergyTotal < this.minimumEndMovementKinectEnergy)
            {
                this.movementTotalWork.x = 0.0f;
                this.movementTotalWork.y = 0.0f;
                this.movementTotalWork.z = 0.0f;

                this.performingMovement = false;
                this.lastStoppedTime    = this.lastEndMovementTime = lastFrame.timestamp;
            }

            if (this.performingGesture)
            {
                this.gestureData.appendFrame(gestureFrame);
            }

            float timeSinceEndMovement = lastFrame.timestamp - this.lastEndMovementTime;

            if (!this.performingMovement && this.performingGesture && timeSinceEndMovement > 500000)
            {
                if (this.callbackInstance != null)
                {
                    this.callbackInstance.endOfGestureCallback(this.gestureData);
                }

                this.performingGesture = false;
            }
        }
    }