Exemplo n.º 1
0
 public InputGesture(IEnumerable <RawJointState> states)
 {
     States = new List <JointState>();
     if (states != null)
     {
         foreach (var js in states)
         {
             States.Add(JointState.FromRawJointState(js));
         }
         if (States.Count > 0)
         {
             StartTime = States[0].Timestamp;
         }
     }
 }
Exemplo n.º 2
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;
            }
        }