Esempio n. 1
0
        /// <summary>
        /// Construct the training data for the combo recognizer.
        /// </summary>
        /// <param name="testData">the data to test on</param>
        /// <param name="rubine">the rubine recognizer</param>
        /// <param name="dollar">the dollar recognizer</param>
        /// <param name="zernike">the zernike recognizer</param>
        /// <param name="image">the image recognizer</param>
        /// <returns>a list of tuples (s,f) where s is a shape type and f is a set of features</returns>
        private static List <KeyValuePair <ShapeType, Dictionary <string, object> > > TrainingDataCombo(
            List <Shape> testData,
            RubineRecognizerUpdateable rubine,
            DollarRecognizer dollar,
            ZernikeMomentRecognizerUpdateable zernike,
            ImageRecognizer image)
        {
            List <KeyValuePair <ShapeType, Dictionary <string, object> > > data = new List <KeyValuePair <ShapeType, Dictionary <string, object> > >();

            foreach (Shape shape in testData)
            {
                string z = zernike.Recognize(shape.SubstrokesL);

                List <ShapeType> img = image.Recognize(shape.SubstrokesL);

                List <string> r    = new List <string>();
                List <string> dAvg = new List <string>();
                List <string> d    = new List <string>();

                foreach (Substroke s in shape.SubstrokesL)
                {
                    r.Add(rubine.Recognize(s));
                    dAvg.Add(dollar.RecognizeAverage(s));
                    d.Add(dollar.Recognize(s));
                }

                Dictionary <string, object> features = ComboRecognizer.GetFeatures(shape.SubstrokesL.Count, z, img, r, dAvg, d);
                data.Add(new KeyValuePair <ShapeType, Dictionary <string, object> >(shape.Type, features));
            }

            return(data);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                return;
            }

            List <string> allLabels = new List <string>(leafLabels);

            allLabels.Add("Wire");

            Dictionary <string, List <string>[]> user2sketches = GetSketchesPerUser(args[0], args[1]);

            Dictionary <string, List <Substroke> > user2gateStrokes  = new Dictionary <string, List <Substroke> >();
            Dictionary <string, List <Substroke> > user2otherStrokes = new Dictionary <string, List <Substroke> >();

            foreach (KeyValuePair <string, List <string>[]> pair in user2sketches)
            {
                string user = pair.Key;
                user2gateStrokes.Add(user, new List <Substroke>());
                user2otherStrokes.Add(user, new List <Substroke>());
                int  userNum;
                bool good = int.TryParse(user.Substring(0, 2), out userNum);
                if (good && userNum <= 0)
                {
                    continue;
                }

                List <string> sketchFiles = pair.Value[0];
                List <string> testFiles   = pair.Value[1];

                foreach (string sketchFile in sketchFiles)
                {
                    Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch;
                    sketch = General.ReOrderParentShapes(sketch);

                    foreach (Substroke s in sketch.Substrokes)
                    {
                        if (s.Labels.Length > 0)
                        {
                            string label = s.Labels[s.Labels.Length - 1];
                            if (leafLabels.Contains(label))
                            {
                                user2gateStrokes[user].Add(s);
                            }
                        }
                    }
                }

                foreach (string sketchFile in testFiles)
                {
                    Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch;
                    sketch = General.ReOrderParentShapes(sketch);

                    foreach (Substroke s in sketch.Substrokes)
                    {
                        if (s.Labels.Length > 0)
                        {
                            string label = s.Labels[s.Labels.Length - 1];
                            string root  = s.Labels[0];
                            if (label == "Wire" || label == "LabelBoxOther")// || root == "LabelBox")
                            {
                                user2otherStrokes[user].Add(s);
                            }
                        }
                    }
                }
            }

            Dictionary <string, ConfusionMatrix> user2Confusion = new Dictionary <string, ConfusionMatrix>();
            ConfusionMatrix allConfusion = new ConfusionMatrix(allLabels);

            // Use this to train a rubine recognizer on all substrokes...
            //RubineRecognizerUpdateable rubine = new RubineRecognizerUpdateable();

            foreach (string user in user2gateStrokes.Keys)
            {
                RubineRecognizerUpdateable rubine = new RubineRecognizerUpdateable();
                foreach (Substroke s in user2gateStrokes[user])
                {
                    if (s.Labels.Length > 0)
                    {
                        rubine.Add(s.Labels[s.Labels.Length - 1], s.PointsL);
                    }
                }

                foreach (KeyValuePair <string, List <Substroke> > kv in user2otherStrokes)
                {
                    if (kv.Key.Substring(0, 2) != user.Substring(0, 2))
                    {
                        foreach (Substroke s in kv.Value)
                        {
                            if (s.Labels.Length > 0)
                            {
                            }  //rubine.Add(s.Labels[s.Labels.Length - 1], s.PointsL);
                        }
                    }
                }

                bool updated = rubine.updateMatrices();
                if (!updated)
                {
                    continue;
                }

                ConfusionMatrix confusion = new ConfusionMatrix(allLabels);

                foreach (string sketchFile in user2sketches[user][1])
                {
                    Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch;
                    sketch = General.ReOrderParentShapes(sketch);

                    foreach (Substroke s in sketch.Substrokes)
                    {
                        if (s.Labels.Length > 0)
                        {
                            string label = s.Labels[s.Labels.Length - 1];
                            if (leafLabels.Contains(label))
                            {
                                string result = rubine.Recognize(s);
                                confusion.Add(label, result);
                                allConfusion.Add(label, result);
                            }
                        }
                    }
                }
                confusion.IncreaseAllByOne();
                user2Confusion.Add(user, confusion);
            }
            allConfusion.IncreaseAllByOne();

            System.IO.StreamWriter allWriter = new System.IO.StreamWriter("allConfusion.txt");
            allConfusion.Print(allWriter);
            allWriter.Close();

            foreach (KeyValuePair <string, ConfusionMatrix> kv in user2Confusion)
            {
                string user = kv.Key;
                System.IO.StreamWriter writer = new System.IO.StreamWriter("Confusion" + user + ".txt");

                kv.Value.Print(writer);

                writer.Close();
            }
        }