public void TestArrays() { string imgfn = "test-c3.png"; // load Bytearray Bytearray ba = new Bytearray(1, 1); ImgIo.read_image_gray(ba, imgfn); OcrRoutine.Invert(ba); //NarrayUtil.Sub((byte)255, image); byte[] bytes1 = ba.To1DArray(); NarrayShow.ShowConsole(ba); StdInput linput1 = new StdInput(ba); Console.WriteLine(); // load StdInput Bitmap bitmap = ImgIo.LoadBitmapFromFile(imgfn); StdInput linput2 = StdInput.FromBitmap(bitmap); //NarrayShow.ShowConsole(linput2); // test convert Floatarray fa = linput2.ToFloatarray(); StdInput linput3 = new StdInput(fa); Console.WriteLine("Arrays is identical? {0}", Equals(linput1.GetDataBuffer(), linput2.GetDataBuffer())); Console.WriteLine("Arrays is identical? {0}", Equals(linput2.GetDataBuffer(), linput3.GetDataBuffer())); }
public void TestSimple() { Global.SetEnv("debug", Global.GetEnv("debug") + ""); // image file name to recognize string imgFileName = "line.png"; string imgCsegFileName = "line.cseg.png"; string imgTranscriptFileName = "line.txt"; // line recognizer Linerec lrec = (Linerec)Linerec.LoadLinerec("default.model"); //Linerec lrec = (Linerec)Linerec.LoadLinerec("2m2-reject.cmodel"); //Linerec lrec = (Linerec)Linerec.LoadLinerec("multi3.cmodel"); //Linerec lrec = (Linerec)Linerec.LoadLinerec("latin-ascii.model"); lrec.Info(); // language model OcroFST default_lmodel = OcroFST.MakeOcroFst(); default_lmodel.Load("default.fst"); OcroFST lmodel = default_lmodel; // read image Bytearray image = new Bytearray(1, 1); ImgIo.read_image_gray(image, imgFileName); // recognize! OcroFST fst = OcroFST.MakeOcroFst(); Intarray rseg = new Intarray(); lrec.RecognizeLine(rseg, fst, image); // show result 1 string resStr; fst.BestPath(out resStr); Console.WriteLine("Fst BestPath: {0}", resStr); // find result 2 Intarray v1 = new Intarray(); Intarray v2 = new Intarray(); Intarray inp = new Intarray(); Intarray outp = new Intarray(); Floatarray c = new Floatarray(); BeamSearch.beam_search(v1, v2, inp, outp, c, fst, lmodel, 100); FstUtil.remove_epsilons(out resStr, outp); Console.WriteLine("Fst BeamSearch: {0}", resStr); Intarray cseg = new Intarray(); SegmRoutine.rseg_to_cseg(cseg, rseg, inp); SegmRoutine.make_line_segmentation_white(cseg); ImgLabels.simple_recolor(cseg); // for human readable ImgIo.write_image_packed(imgCsegFileName, cseg); File.WriteAllText(imgTranscriptFileName, resStr.Replace(" ", "")); }
public override void PutLine(Intarray image, int page, int line, string variant = null) { MaybeMakeDirectory(page); FileStream fs = Open(FileMode.Create, page, line, variant, "png"); ImgIo.write_image_packed(fs, image, System.Drawing.Imaging.ImageFormat.Png); fs.Flush(); fs.Close(); }
public override bool GetLine(Intarray image, int page, int line, string variant = null) { string s = PathFile(page, line, variant, "png"); if (!File.Exists(s)) { return(false); } ImgIo.read_image_packed(image, s); return(true); }
public override bool GetPage(Bytearray image, int page, string variant = null) { string s = PathFile(page, -1, variant, "png"); if (!File.Exists(s)) { return(false); } ImgIo.read_image_gray(image, s); return(true); }
public override bool GetLine(out Bitmap bitmap, int page, int line, string variant = null) { bitmap = null; string s = PathFile(page, line, variant, "png"); if (!File.Exists(s)) { return(false); } bitmap = ImgIo.LoadBitmapFromFile(s); return(true); }
public void RunTest() { // create int array of ascii codes int[] classes = new int[strClasses.Length]; for (int i = 0; i < strClasses.Length; i++) { classes[i] = (int)strClasses[i]; } // init lenet IntPtr lenetptr; LenetWrapper.CreateLenet(out lenetptr, classes.Length, classes, false, false, true); // load network from file LenetWrapper.LoadNetwork(lenetptr, networkFileName); // read image variant 2 Bitmap bitmap = ImgIo.LoadBitmapFromFile(testPngFileName); int w = bitmap.Width; int h = bitmap.Height; byte[] buffer = LenetRoutines.ToMatrix <byte>(bitmap, false); // run recognize from buffer int answer; double rate; LenetWrapper.RecognizeRawData(lenetptr, buffer, buffer.Length, h, w, out answer, out rate); Console.WriteLine("BUFFER: Result index '{0}', rate '{1}'", answer, rate); // run recognize from file LenetWrapper.RecognizeImageFile(lenetptr, testPpmFileName, out answer, out rate); Console.WriteLine("FILE: Result index '{0}', rate '{1}'", answer, rate); // compute outputs int[] outclasses = new int[strClasses.Length]; double[] outenergies = new double[strClasses.Length]; int getsize; LenetWrapper.ComputeOutputs(lenetptr, buffer, buffer.Length, h, w, outclasses, outenergies, out getsize); PrintOutputs(classes, outenergies, getsize); // test CoTaskMemAlloc IntPtr ptrArray; int ptrSize; LenetWrapper.TestDoubleCoTaskMemAlloc(out ptrSize, out ptrArray); IntPtr curr = ptrArray; double[] valArray = new double[ptrSize]; Marshal.Copy(ptrArray, valArray, 0, ptrSize); Marshal.FreeCoTaskMem(ptrArray); }
private void DoTestRecognize(LenetClassifier classifier) { OutputVector ov = new OutputVector(); Floatarray v = new Floatarray(); Bytearray ba = new Bytearray(1, 1); ImgIo.read_image_gray(ba, testPngFileName); NarrayUtil.Sub(255, ba); v.Copy(ba); v /= 255.0; classifier.XOutputs(ov, v); Console.WriteLine("Featured output class '{0}', score '{1}'", (char)ov.Key(ov.BestIndex), ov.Value(ov.BestIndex)); }
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); }
private void AddItemsToBase_Click(object sender, RoutedEventArgs e) { if (tbBookPath.Text.Length == 0) { return; } // Configure open file dialog box Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); dlg.FileName = "Image"; // Default file name dlg.DefaultExt = ".png"; // Default file extension dlg.Filter = "PNG images (.png)|*.png|JPEG images (.jpg)|*.jpg"; // Filter files by extension dlg.Multiselect = true; dlg.Title = "Выберите один или несколько файлов"; // Show open file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process open file dialog box results if (result == true) { // Open document var filenames = dlg.FileNames.OrderBy(name => name); // check method (0 - default, 1 - with text) int method = 0; Button btn = sender as Button; if (btn.Tag != null) { method = int.Parse(btn.Tag.ToString()); } foreach (string fname in filenames) { Bitmap bitmap = ImgIo.LoadBitmapFromFile(fname); int lineid = lineSource.PutNewImage(bitmap); if (method == 1) { string str = System.IO.Path.GetFileNameWithoutExtension(fname); lineSource.PutTranscript(str, lineSource.CurrentBook, lineSource.CurrentPage, lineid); } } InitBookstoreSource(tbBookPath.Text); } }
/// <summary> /// Create char segmentation (cseg) files if missing /// </summary> /// <param name="bookPath">path to bookstore</param> /// <param name="modelFilename">Linerec model file</param> /// <param name="langModel">language model file</param> /// <param name="suffix">e.g., 'gt'</param> public void ComputeMissingCsegForBookStore(string bookPath, string model = "default.model", string suffix = "", bool saveRseg = false, string langModel = "default.fst") { // create line recognizer Linerec linerec = Linerec.LoadLinerec(model); // create IBookStore IBookStore bookstore = new SmartBookStore(); bookstore.SetPrefix(bookPath); bookstore.Info(); // language model OcroFST lmodel = OcroFST.MakeOcroFst(); lmodel.Load(langModel); // iterate lines of pages for (int page = 0; page < bookstore.NumberOfPages(); page++) { int nlines = bookstore.LinesOnPage(page); Console.WriteLine("Page {0} has {1} lines", page, nlines); for (int j = 0; j < nlines; j++) { int line = bookstore.GetLineId(page, j); Bytearray image = new Bytearray(); bookstore.GetLine(image, page, line); Intarray cseg = new Intarray(); bookstore.GetCharSegmentation(cseg, page, line, suffix); // check missing cseg file if (cseg.Length() <= 0 && image.Length() > 0) { // recognize line OcroFST fst = OcroFST.MakeOcroFst(); Intarray rseg = new Intarray(); linerec.RecognizeLine(rseg, fst, image); // find best results string resText; Intarray inp = new Intarray(); Floatarray costs = new Floatarray(); double totalCost = BeamSearch.beam_search(out resText, inp, costs, fst, lmodel, 100); Console.WriteLine(bookstore.PathFile(page, line, suffix)); Console.Write(" beam_search score: {0}", totalCost); /*string resText2; * fst.BestPath(out resText2);*/ // write cseg to bookstore string trans; bookstore.GetLine(out trans, page, line, suffix); resText = resText.Replace(" ", ""); if (String.IsNullOrEmpty(trans)) { bookstore.PutLine(resText, page, line, suffix); Console.Write("; transcript saved"); } else if (trans == resText) { // convert inputs and rseg to cseg SegmRoutine.rseg_to_cseg(cseg, rseg, inp); bookstore.PutCharSegmentation(cseg, page, line, suffix); Console.Write("; cseg saved"); } else if (saveRseg) { // convert inputs and rseg to cseg SegmRoutine.rseg_to_cseg(cseg, rseg, inp); //SegmRoutine.remove_small_components(cseg, 4); /*bookstore.PutCharSegmentation(cseg, page, line, suffix); * Console.Write("; cseg saved");*/ SegmRoutine.make_line_segmentation_white(cseg); ImgLabels.simple_recolor(cseg); string v = "rseg"; if (!String.IsNullOrEmpty(suffix)) { v += "."; v += suffix; } string rsegpath = bookstore.PathFile(page, line, v, "png"); ImgIo.write_image_packed(rsegpath, cseg); Console.Write("; rseg saved"); } Console.WriteLine(); } } } }
public override void Binarize(Bytearray bin_image, Bytearray gray_image) { w = PGeti("w"); k = (float)PGetf("k"); whalf = w >> 1; // fprintf(stderr,"[sauvola %g %d]\n",k,w); if (k < 0.001 || k > 0.999) { throw new Exception("Binarize: CHECK_ARG(k>=0.001 && k<=0.999)"); } if (w == 0 || k >= 1000) { throw new Exception("Binarize: CHECK_ARG(w>0 && k<1000)"); } if (bin_image.Length1d() != gray_image.Length1d()) { bin_image.MakeLike(gray_image); } if (NarrayUtil.contains_only(gray_image, (byte)0, (byte)255)) { bin_image.Copy(gray_image); return; } int image_width = gray_image.Dim(0); int image_height = gray_image.Dim(1); whalf = w >> 1; // Calculate the integral image, and integral of the squared image Narray <long> integral_image = new Narray <long>(), rowsum_image = new Narray <long>(); Narray <long> integral_sqimg = new Narray <long>(), rowsum_sqimg = new Narray <long>(); integral_image.MakeLike(gray_image); rowsum_image.MakeLike(gray_image); integral_sqimg.MakeLike(gray_image); rowsum_sqimg.MakeLike(gray_image); int xmin, ymin, xmax, ymax; double diagsum, idiagsum, diff, sqdiagsum, sqidiagsum, sqdiff, area; double mean, std, threshold; for (int j = 0; j < image_height; j++) { rowsum_image[0, j] = gray_image[0, j]; rowsum_sqimg[0, j] = gray_image[0, j] * gray_image[0, j]; } for (int i = 1; i < image_width; i++) { for (int j = 0; j < image_height; j++) { rowsum_image[i, j] = rowsum_image[i - 1, j] + gray_image[i, j]; rowsum_sqimg[i, j] = rowsum_sqimg[i - 1, j] + gray_image[i, j] * gray_image[i, j]; } } for (int i = 0; i < image_width; i++) { integral_image[i, 0] = rowsum_image[i, 0]; integral_sqimg[i, 0] = rowsum_sqimg[i, 0]; } for (int i = 0; i < image_width; i++) { for (int j = 1; j < image_height; j++) { integral_image[i, j] = integral_image[i, j - 1] + rowsum_image[i, j]; integral_sqimg[i, j] = integral_sqimg[i, j - 1] + rowsum_sqimg[i, j]; } } //Calculate the mean and standard deviation using the integral image for (int i = 0; i < image_width; i++) { for (int j = 0; j < image_height; j++) { xmin = Math.Max(0, i - whalf); ymin = Math.Max(0, j - whalf); xmax = Math.Min(image_width - 1, i + whalf); ymax = Math.Min(image_height - 1, j + whalf); area = (xmax - xmin + 1) * (ymax - ymin + 1); // area can't be 0 here // proof (assuming whalf >= 0): // we'll prove that (xmax-xmin+1) > 0, // (ymax-ymin+1) is analogous // It's the same as to prove: xmax >= xmin // image_width - 1 >= 0 since image_width > i >= 0 // i + whalf >= 0 since i >= 0, whalf >= 0 // i + whalf >= i - whalf since whalf >= 0 // image_width - 1 >= i - whalf since image_width > i // --IM if (area <= 0) { throw new Exception("Binarize: area can't be 0 here"); } if (xmin == 0 && ymin == 0) { // Point at origin diff = integral_image[xmax, ymax]; sqdiff = integral_sqimg[xmax, ymax]; } else if (xmin == 0 && ymin > 0) { // first column diff = integral_image[xmax, ymax] - integral_image[xmax, ymin - 1]; sqdiff = integral_sqimg[xmax, ymax] - integral_sqimg[xmax, ymin - 1]; } else if (xmin > 0 && ymin == 0) { // first row diff = integral_image[xmax, ymax] - integral_image[xmin - 1, ymax]; sqdiff = integral_sqimg[xmax, ymax] - integral_sqimg[xmin - 1, ymax]; } else { // rest of the image diagsum = integral_image[xmax, ymax] + integral_image[xmin - 1, ymin - 1]; idiagsum = integral_image[xmax, ymin - 1] + integral_image[xmin - 1, ymax]; diff = diagsum - idiagsum; sqdiagsum = integral_sqimg[xmax, ymax] + integral_sqimg[xmin - 1, ymin - 1]; sqidiagsum = integral_sqimg[xmax, ymin - 1] + integral_sqimg[xmin - 1, ymax]; sqdiff = sqdiagsum - sqidiagsum; } mean = diff / area; std = Math.Sqrt((sqdiff - diff * diff / area) / (area - 1)); threshold = mean * (1 + k * ((std / 128) - 1)); if (gray_image[i, j] < threshold) { bin_image[i, j] = 0; } else { bin_image[i, j] = (byte)(MAXVAL - 1); } } } if (PGeti("debug_binarize") > 0) { ImgIo.write_image_gray("debug_binarize.png", bin_image); } }
public override void PutPage(Intarray image, int page, string variant = null) { ImgIo.write_image_packed(PathFile(page, -1, variant, "png"), image); }
static void Main(string[] args) { //MlpClassifier mlp = new MlpClassifier(); /*OcroFST fst = OcroFST.MakeFst("OcroFST"); * fst.Load("ocr-dict-case.fst"); * * Console.WriteLine("{0} ({1})", fst.Name, fst.Interface); * Console.WriteLine("{0}..{1}", fst.GetStart(), fst.nStates()); * fst.Save("test.fst");*/ //TestDataSet ds = new TestDataSet(); /*Console.WriteLine("Interface: {0}", ds.Interface); * Console.WriteLine("Name: {0}", ds.Name); * Console.WriteLine("Description: {0}", ds.Description);*/ /*Console.WriteLine(String.Format("{0:X6}", 15)); * DirPattern dpatt1 = new DirPattern("data2", @"[0-9][0-9][0-9][0-9]"); * Console.WriteLine("Count: {0}", dpatt1.Length); * DirPattern dpatt2 = new DirPattern("data2", @"([0-9][0-9][0-9][0-9])\.png"); * Console.WriteLine("Count: {0}", dpatt2.Length); * * Console.WriteLine("Exist: {0}", * DirPattern.Exist("data2", @"[0-9][0-9][0-9][0-9]", @"([0-9][0-9][0-9][0-9][0-9][0-9])\.png"));*/ //new TestBookStore().RunTest(); Ocronet.Dynamic.Utils.Logger.Default.verbose = true; //new TestLenetWrapper().RunTest(); //TestLenetClassifier testLenet = new TestLenetClassifier(); //testLenet.TestArrays(); //testLenet.TestSaveNetwork(); //testLenet.TestRecognize(); //new TestDataset().TestRowDataset(); TestLinerec testLinerec = new TestLinerec(); //testLinerec.TestSimple(); //testLinerec.TestTrainLatinCseg(); testLinerec.TestTrainLenetCseg(); //testLinerec.TestRecognizeCseg(); //testLinerec.TestComputeMissingCseg(); //testLinerec.TestSimple(); if (args.Length == 0) { Console.WriteLine("Usage: DynamicConsole.exe imagefile"); return; } //args[0] = "scan1.png"; //args[0] = "lenna.jpg"; string fileNameWoExt = Path.GetFileNameWithoutExtension(args[0]); Bytearray image = new Bytearray(1, 1); //Intarray image = new Intarray(1, 1); ImgIo.read_image_gray(image, args[0]); Bytearray binimage = new Bytearray(1, 1); Intarray segmimage = new Intarray(1, 1); IBinarize binarizer = new BinarizeByOtsu(); //binarizer.Set("k", 0.05); //binarizer.Set("w", 5); ISegmentLine segmenter = new DpSegmenter(); binarizer.Binarize(binimage, image); ImgIo.write_image_gray(fileNameWoExt + ".bin.png", binimage); //new BinarizeByOtsu().Binarize(binimage, image); segmenter.Charseg(ref segmimage, binimage); ImgLabels.simple_recolor(segmimage); ImgIo.write_image_packed(fileNameWoExt + ".rseg.png", segmimage); }