public RecognizerResult RecognizeSingleGesture(InputGesture g)
        {
            var features = mValidFeatures.Take(mValidFeatures.Count - 1).Select(x => Features.AllFeatures.GestureFeatures[x]).ToList();
            //Console.WriteLine("{0} features to be checked", features.Count);

            var results = new List <GestureWeight>();

            foreach (var kvp in mWeights)
            {
                GestureWeight gw = new GestureWeight()
                {
                    name = kvp.Key
                };
                for (int i = 0; i < features.Count; i++)
                {
                    float fres = features[i].QueryGesture(g);
                    gw.weight += kvp.Value[i] * fres;
                }
                gw.weight += kvp.Value[mValidFeatures.Count - 1];
                gw.weight  = Utility.Sigmoid(gw.weight);
                results.Add(gw);
            }

            results.Sort();
            return(new RecognizerResult()
            {
                Gesture1 = results[0].name, Confidence1 = (float)results[0].weight,
                Gesture2 = results[1].name, Confidence2 = (float)results[1].weight,
                Gesture3 = results[2].name, Confidence3 = (float)results[2].weight
            });
        }
Ejemplo n.º 2
0
 void _CheckIfSegmented()
 {
     if (mCurrSegmentSize >= _minSegmentSize)
     {
         Console.WriteLine("Segmented gesture");
         LastGesture = InputGesture.FromJointStates(mCurrGesture);
         GestureSegmented(this, null);
     }
 }
Ejemplo n.º 3
0
        static public InputGesture FromJointStates(IEnumerable <JointState> states)
        {
            InputGesture ig = new InputGesture(null);

            ig.States = new List <JointState>(states);
            if (ig.States.Count > 0)
            {
                ig.StartTime = ig.States[0].Timestamp;
            }
            return(ig);
        }
Ejemplo n.º 4
0
 public RecognizerResult RecognizeSingleGesture(InputGesture g)
 {
     foreach (var js in g.States)
     {
         if (mFeatures[0].QueryFrame(js) > 0.0f)
         {
             var res = RecognizerResult.Empty();
             res.Gesture1    = "high_kick";
             res.Confidence1 = 100.0f;
             return(res);
         }
     }
     return(RecognizerResult.Empty());
 }
Ejemplo n.º 5
0
        static public void BenchmarkRecognize()
        {
            var sw       = new Stopwatch();
            var gest     = new InputGesture(new LogFileLoader("gestures/track_high_kick_01.log"));
            var numiters = 1000;

            sw.Start();
            for (int i = 0; i < numiters; i++)
            {
                foreach (var f in Features.AllFeatures.GestureFeatures)
                {
                    f.QueryGesture(gest);
                }
            }
            sw.Stop();

            Console.WriteLine("Elapsed time ({0} iters): {1}", numiters, sw.Elapsed);
            Console.WriteLine("Gestures per second: {0}", 1000.0f / (sw.ElapsedMilliseconds / (float)numiters));
        }
        public RecognizerResult RecognizeSingleGesture(InputGesture g)
        {
            List <Features.IGestureFeature> features = Features.AllFeatures.GestureFeatures;

            var results = new List <GestureWeight>();

            foreach (var kvp in mWeights)
            {
                GestureWeight gw = new GestureWeight()
                {
                    name = kvp.Key
                };
                gw.weight = kvp.Value[features.Count];
                for (int i = 0; i < features.Count; i++)
                {
                    float fres = features[i].QueryGesture(g);
                    gw.weight += kvp.Value[i] * fres;
                }
                gw.weight = Math.Exp(gw.weight);
                results.Add(gw);
            }
            double total_weight = 0;

            for (int i = 0; i < results.Count; i++)
            {
                total_weight += results[i].weight;
            }
            for (int i = 0; i < results.Count; i++)
            {
                results[i].weight = 1.0 * results[i].weight / total_weight;
            }

            results.Sort();
            return(new RecognizerResult()
            {
                Gesture1 = results[0].name, Confidence1 = (float)results[0].weight,
                Gesture2 = results[1].name, Confidence2 = (float)results[1].weight,
                Gesture3 = results[2].name, Confidence3 = (float)results[2].weight
            });
        }
Ejemplo n.º 7
0
        public RecognizerResult RecognizeSingleGesture(InputGesture g)
        {
            var results = new List <GestureWeight>();

            foreach (var kvp in mWeights)
            {
                GestureWeight gw = new GestureWeight()
                {
                    name = kvp.Key
                };
                for (int i = 0; i < mFeatures.Count; i++)
                {
                    bool fp = mFeatures[i].QueryGesture(g);
                    if (fp)
                    {
                        gw.weight += kvp.Value[i * 2];
                    }
                    else
                    {
                        gw.weight += kvp.Value[i * 2 + 1];
                    }
                }
                results.Add(gw);
            }

            results.Sort();
            results.Reverse();
            float norm = (float)mFeatures.Count;

            return(new RecognizerResult()
            {
                Gesture1 = results[0].name, Confidence1 = results[0].weight / norm,
                Gesture2 = results[1].name, Confidence2 = results[1].weight / norm,
                Gesture3 = results[2].name, Confidence3 = results[2].weight / norm
            });
        }
Ejemplo n.º 8
0
        public bool QueryGesture(InputGesture ig)
        {
            float result = mFeature.QueryGesture(ig);

            return(result >= mMin && result < mMax);
        }
Ejemplo n.º 9
0
        public void Run(string[] args)
        {
            Command c = Recognizer.Command.Help;

            if (args.Length > 0)
            {
                c = (Command)Enum.Parse(typeof(Recognizer.Command), args[0]);
            }
            if (c == Recognizer.Command.Help)
            {
                Console.WriteLine("Usage:\n\tFinalProject.exe command [filename]\n\nCommands:");
                foreach (var cm in Enum.GetNames(typeof(Recognizer.Command)))
                {
                    Console.WriteLine(cm);
                }
                return;
            }

            // For CV
            mCV = new CrossValidation("cvindex.txt");

            ////////////////////
            // CHANGE THESE LINES TO SWAP MODELS
            ////////////////////
            mRec = new LogisticRegressionRecognizer();
            mSeg = new DumbSegmenter();

            Console.WriteLine("Using recognizer {0}", mRec.GetType().ToString());
            Console.WriteLine("Using segmenter {0}", mSeg.GetType().ToString());
            mRecFilename = "models/" + mRec.GetType().ToString() + ".rec.model";
            mSegFilename = "models/" + mSeg.GetType().ToString() + ".seg.model";


            mSeg.GestureSegmented += delegate(object sender, EventArgs e) {
                var segm   = ((ISegmenter)sender).LastGesture;
                var recres = mRec.RecognizeSingleGesture(segm);
                if (recres.Confidence1 > 0.5f)
                {
                    Console.WriteLine("Recognized gesture: {0}", recres.Gesture1);
                }
                else
                {
                    Console.WriteLine("Inconclusive");
                }
            };


            string filename = "gestures/track_high_kick_01.log";

            if (args.Length > 1)
            {
                filename = args[1];
            }

            InputGesture gest;
            Thread       visthread;

            switch (c)
            {
            case Command.Train:
                Train();
                break;

            case Command.TestSingle:
                LoadModels();
                var result = mRec.RecognizeSingleGesture(new InputGesture(new LogFileLoader(filename)));
                Console.WriteLine(result.ToString());
                break;

            case Command.TestRecognize:
                LoadModels();
                var   test_gestures = LoadData(mGestureNames, false);
                int[] total         = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray(),
                correct = Enumerable.Range(0, test_gestures.Count).Select(x => 0).ToArray();
                int i = 0;
                Console.WriteLine();
                foreach (var gn in test_gestures)
                {
                    foreach (var tg in gn.Value)
                    {
                        total[i]++;
                        var result2 = mRec.RecognizeSingleGesture(tg);
                        if (result2.Gesture1 == gn.Key)
                        {
                            correct[i]++;
                        }
                    }
                    Console.WriteLine("{0}: {1} correct / {2} total = {3}% correct",
                                      gn.Key,
                                      correct[i], total[i], (float)correct[i] / (float)total[i] * 100.0f);
                    i++;
                }

                Console.WriteLine("TEST RESULTS:\n\t{0} correct / {1} total = {2}% correct",
                                  correct.Sum(), total.Sum(), (float)correct.Sum() / (float)total.Sum() * 100.0f);
                Utility.PrintMemoryUsage();
                break;

            case Command.TestRealtime:
                LoadModels();
                gest      = new InputGesture(new LogFileLoader(filename));
                visthread = new Thread(new ThreadStart(this.StartVisualize));
                visthread.Start();
                while (true)                     // Wait for window to get created
                {
                    lock (this) {
                        if (mVisWindow != null)
                        {
                            break;
                        }
                    }
                }

                float lastTime = gest.States[0].Timestamp;
                foreach (var frame in gest.States)
                {
                    mSeg.AddState(frame);
                    mVisWindow.CurrState = frame;
                    Thread.Sleep((int)((frame.Timestamp - lastTime) * 750.0f));
                    lastTime = frame.Timestamp;
                }
                mSeg.Finish();

                visthread.Abort();
                break;

            case Command.RunRealtime:
                LoadModels();
                visthread = new Thread(new ThreadStart(this.StartVisualize));
                visthread.Start();
                while (true)                     // Wait for window to get created
                {
                    lock (this) {
                        if (mVisWindow != null)
                        {
                            break;
                        }
                    }
                }

                var nfl = new NetworkFrameListener(4711);
                nfl.Start();
                Console.WriteLine("Press the 'x' key to quit");

                while (true)
                {
                    var frame = nfl.GetState();
                    if (frame != null)
                    {
                        var js = JointState.FromRawJointState(frame);
                        mSeg.AddState(js);
                        mVisWindow.CurrState = js;
                    }
                    if (Console.KeyAvailable)
                    {
                        var cki = Console.ReadKey(true);
                        if (cki.Key == ConsoleKey.X)
                        {
                            break;
                        }
                    }
                    if (!nfl.Running)
                    {
                        break;
                    }
                    Thread.Sleep(0);
                }

                mSeg.Finish();
                nfl.Stop();
                visthread.Abort();
                break;

            case Command.PrintFeatures:
                gest = new InputGesture(new LogFileLoader(filename));

                /*foreach ( var f in Features.AllFeatures.SingleGestureFeatures ) {
                 *      Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest));
                 * }*/
                foreach (var f in Features.AllFeatures.GestureFeatures)
                {
                    Console.WriteLine("{0}: {1}", f.ToString(), f.QueryGesture(gest));
                }
                break;

            case Command.BenchmarkRecognize:
                Benchmarks.BenchmarkRecognize();
                break;

            case Command.CycleCV:
                mCV.Incr();
                mCV.Save();
                break;
            }
        }
Ejemplo n.º 10
0
 public LateralPositionEnumerable(InputGesture g, int j)
 {
     parent = g;
     joint  = j;
     System.Diagnostics.Debug.Assert(j >= 0 && j < parent.States[0].RelativeJoints.Length);
 }