private void loadTemplatesToolStripMenuItem_Click(object sender, EventArgs e) { bool openSuccess; List <string> sketches = General.SelectOpenFiles(out openSuccess, "Sketches containing template shapes", "Labeled (including gate parts) XML Sketches (*.labeled.xml)|*.labeled.xml"); if (!openSuccess) { MessageBox.Show("Unable to load sketches"); return; } foreach (string sketchFile in sketches) { Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch; sketch = General.ReOrderParentShapes(sketch); foreach (Shape shape in sketch.Shapes) { if (General.IsGate(shape)) { if (shape.Substrokes[0].Labels[0] == shape.Label) { m_Recognizer.Add(shape); } } } } m_Recognizer.Save("C:\\Reco.iar"); }
static void Main(string[] args) { //if (args.Length == 0) //return; if (NUMBEST != 0) { string filePath = Directory.GetCurrentDirectory(); if (filePath.Contains("\\Code\\")) { filePath = filePath.Substring(0, filePath.IndexOf("\\Code\\") + 1); } TrainByNBest(filePath + "Data\\Gate Study Data\\LabeledPartsSketches", "*.xml"); return; } List <KeyValuePair <string, Dictionary <string, object> > > data = new List <KeyValuePair <string, Dictionary <string, object> > >(); Dictionary <string, List <string>[]> user2sketches = GetSketchesPerUser(args[0], args[1]); // Foreach user: train each of the recognizers foreach (KeyValuePair <string, List <string>[]> pair in user2sketches) { string user = pair.Key; int userNum; bool good = int.TryParse(user.Substring(0, 2), out userNum); if (good && userNum <= 0) { continue; } User u = new User(userNum.ToString()); PlatformUsed platform = PlatformUsed.TabletPC; if (user.Contains("P")) { platform = PlatformUsed.Wacom; } //Console.WriteLine("User: "******"NOT") { List <Substroke> strokes = new List <Substroke>(); foreach (Substroke s in shape.Substrokes) { if (s.Labels[s.Labels.Length - 1] == "Bubble") { strokes.Add(s); break; } } XmlStructs.XmlShapeAttrs attr = new XmlStructs.XmlShapeAttrs(); attr.Type = "NOTBUBBLE"; Shape nb = new Shape(strokes, attr); recognizer.Add(nb); } } } } recognizer.Save("ImageAlignerRecognizer" + user + ".iar"); int numRight = 0; int numWrong = 0; Dictionary <Shape, ImageTemplateResult> results = new Dictionary <Shape, ImageTemplateResult>(); foreach (string sketchFile in testFiles) { Sketch.Sketch sketch = new ReadXML(sketchFile).Sketch; sketch = General.ReOrderParentShapes(sketch); foreach (Shape shape in sketch.Shapes) { if (General.IsGate(shape) && shape.Substrokes[0].Labels[0] == shape.Label && shape.Label != "LabelBox") { ImageTemplateResult result = recognizer.Recognize(shape); if (result != null) { results.Add(shape, result); if (result.Name == shape.Label) { numRight++; } else { numWrong++; } } } } } int total = numRight + numWrong; Console.WriteLine("User: "******" Correct: " + numRight.ToString() + "/" + total.ToString()); if (args.Length > 2 && args[2] == "-v") { foreach (KeyValuePair <Shape, ImageTemplateResult> kv in results) { WriteResult(kv.Key, kv.Value); } } } }
private static void TrainByNBest(string dir, string search) { string cMatrixFile = "Code\\Recognition\\ImageAligner\\allConfusion.txt"; matrix = GetConfusionMatrix(cMatrixFile); ImageAligner.ImageAlignerRecognizer recognizer = new ImageAlignerRecognizer(); string[] files = System.IO.Directory.GetFiles(dir, search); Dictionary <string, Dictionary <int, List <Sketch.Shape> > > shapes = new Dictionary <string, Dictionary <int, List <Shape> > >(); Console.WriteLine("Reading Files: starting @ " + DateTime.Now.ToString()); foreach (string file in files) { bool copyEqn = false; if (file.Contains("COPY") || file.Contains("EQ")) { copyEqn = true; } Sketch.Sketch sketch = new ReadXML(file).Sketch; sketch = General.ReOrderParentShapes(sketch); sketch = General.LinkShapes(sketch); foreach (Shape shape in sketch.Shapes) { if (!General.IsParent(shape)) { continue; } if (!General.IsGate(shape)) { continue; } if (copyEqn && shape.Label != "LabelBox") { continue; } bool junkFound = false; foreach (Substroke stroke in shape.SubstrokesL) { if (stroke.Labels.Length > 0 && (stroke.Labels[stroke.Labels.Length - 1] == "Junk" || stroke.Labels[stroke.Labels.Length - 1] == "TouchUp")) { junkFound = true; } } if (!junkFound) { if (!shapes.ContainsKey(shape.Label)) { shapes.Add(shape.Label, new Dictionary <int, List <Shape> >()); } if (!shapes[shape.Label].ContainsKey(shape.SubstrokesL.Count)) { shapes[shape.Label].Add(shape.SubstrokesL.Count, new List <Shape>()); } if (shapes[shape.Label][shape.SubstrokesL.Count].Count < 10000) { shapes[shape.Label][shape.SubstrokesL.Count].Add(shape); } } } } foreach (string label in shapes.Keys) { foreach (KeyValuePair <int, List <Shape> > kvp in shapes[label]) { List <Shape> shapeClass = kvp.Value; Console.WriteLine(label + " " + kvp.Key + "-Stroke: starting @ " + DateTime.Now.ToString()); int num = 0; if (shapeClass.Count < 5) { continue; } else if (shapeClass.Count < 20) { num = 1; } else if (shapeClass.Count < 50) { num = 2; } else { num = 3; } Dictionary <Shape, double> scores = new Dictionary <Shape, double>(); int n = 0; foreach (Shape currentShape in shapeClass) { shape1 = currentShape; if (n % 50 == 1) { Console.WriteLine("\t" + n + " @ " + DateTime.Now.ToString()); } n++; m_TotalScore = 0.0; GetTotalScore(shape1, shapeClass); scores.Add(shape1, m_TotalScore); } for (int i = 0; i < num; i++) { double bestscore = double.NegativeInfinity; Shape bestShape = null; foreach (KeyValuePair <Shape, double> score in scores) { if (score.Value > bestscore) { bestscore = score.Value; bestShape = score.Key; } } if (bestShape != null) { recognizer.Add(bestShape); scores.Remove(bestShape); } } } } recognizer.Save("ImageAlignerRecognizer" + "NBEST_eric.iar"); }