Beispiel #1
0
        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(" ", ""));
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        /// <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();
                    }
                }
            }
        }
Beispiel #4
0
 public override void PutPage(Intarray image, int page, string variant = null)
 {
     ImgIo.write_image_packed(PathFile(page, -1, variant, "png"), image);
 }
Beispiel #5
0
        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);
        }