public override void Load(string filename) { Linerec linerec = LoadLinerec(filename); if (linerec != null) { this.classifier.Object = linerec.GetClassifier(); } }
public static Linerec LoadLinerec(string filename) { FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream); try { IComponent comp = ComponentIO.load_component(reader); // loaded component is Linerec Linerec linerec = comp as Linerec; if (linerec != null) { return(linerec); } // else component is IModel IModel cmodel = comp as IModel; if (cmodel != null) { linerec = new Linerec(); linerec.SetClassifier(cmodel); return(linerec); } } finally { reader.Close(); stream.Close(); } // try load old format Linerec linerec_old = new Linerec(); linerec_old.LoadOldFormat(filename); return(linerec_old); }
public static Linerec LoadLinerec(string filename) { FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(stream); try { IComponent comp = ComponentIO.load_component(reader); // loaded component is Linerec Linerec linerec = comp as Linerec; if (linerec != null) return linerec; // else component is IModel IModel cmodel = comp as IModel; if (cmodel != null) { linerec = new Linerec(); linerec.SetClassifier(cmodel); return linerec; } } finally { reader.Close(); stream.Close(); } // try load old format Linerec linerec_old = new Linerec(); linerec_old.LoadOldFormat(filename); return linerec_old; }
private void DoTestLinerecRecognize(Linerec linerec, string bookPath, string filename) { Bytearray image = new Bytearray(); ImgIo.read_image_gray(image, bookPath + filename); // recognize! OcroFST fst = OcroFST.MakeOcroFst(); linerec.RecognizeLine(fst, image); // show result string resStr; fst.BestPath(out resStr); Console.WriteLine("Fst BestPath: {0}", resStr); }
public void TestTrainLenetCseg() { string bookPath = "data\\0000\\"; string netFileName = "latin-lenet.model"; Linerec.GDef("linerec", "use_reject", 1); Linerec.GDef("lenet", "junk", 1); Linerec.GDef("lenet", "epochs", 4); // create Linerec Linerec linerec; if (File.Exists(netFileName)) linerec = Linerec.LoadLinerec(netFileName); else { linerec = new Linerec("lenet"); LenetClassifier classifier = linerec.GetClassifier() as LenetClassifier; if (classifier != null) classifier.InitNumSymbLatinAlphabet(); } // temporary disable junk //linerec.DisableJunk = true; linerec.StartTraining(); int nepochs = 10; LineSource lines = new LineSource(); lines.Init(new string[] { "data2" }); //linerec.GetClassifier().Set("epochs", 1); for (int epoch = 1; epoch <= nepochs; epoch++) { linerec.Epoch(epoch); // load cseg samples while (!lines.Done()) { lines.MoveNext(); Intarray cseg = new Intarray(); //Bytearray image = new Bytearray(); string transcript = lines.GetTranscript(); //lines.GetImage(image); if (!lines.GetCharSegmentation(cseg) && cseg.Length() == 0) { Global.Debugf("warn", "skipping book {0} page {1} line {2} (no or bad cseg)", lines.CurrentBook, lines.CurrentPage, lines.Current); continue; } SegmRoutine.make_line_segmentation_black(cseg); linerec.AddTrainingLine(cseg, transcript); } lines.Reset(); lines.Shuffle(); // do Train and clear Dataset linerec.FinishTraining(); // do save if (epoch % 1 == 0) linerec.Save(netFileName); // recognize test line bool bakDisJunk = linerec.DisableJunk; linerec.DisableJunk = false; DoTestLinerecRecognize(linerec, "data2\\", "test1.png"); linerec.DisableJunk = bakDisJunk; } // finnaly save linerec.Save(netFileName); }