コード例 #1
0
        public static void Recognize()
        {
            int n_images = Workspace.Images.Length;
            int i_image = n_images - 1;
            //i_image = 0;
            i_image = 2;
            string fileName = Workspace.Images[i_image];

            string imageFile = Path.Combine(Workspace.InputFolder, fileName);

            string language = "eng";
            int oem = (int)eOcrEngineMode.OEM_DEFAULT;

            string name = Path.GetFileNameWithoutExtension(imageFile);
            {
                using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
                {
                    using (GreyImage greyImage = GreyImage.FromImage(bmp))
                    {

                        ImageThresholder thresholder = new AdaptiveThresholder();
                        using (BinaryImage binImage = thresholder.Threshold(greyImage))
                        {
                            DateTime started = DateTime.Now;
                            DateTime ended = DateTime.Now;

                            Rectangle[] rois = new Rectangle[] { 
                                Rectangle.FromLTRB(807, 43, 1351, 613),
                                Rectangle.FromLTRB(4, 604, binImage.Width - 15, binImage.Height-35)
                            };

                            int nROIs = rois.Length;

                            string[] texts = new string[nROIs];
#if PARALLEL
                            Parallel.For(0, nROIs, delegate(int iROI) 
#else
                            using (TesseractProcessor processor = new TesseractProcessor())
                            for (int iROI = 0; iROI < nROIs; iROI++)
#endif
                            {
#if PARALLEL
                                using (TesseractProcessor processor = new TesseractProcessor())
#endif
                                {
                                    Rectangle roi = rois[iROI];
                                    {                                    
                                        //oem = (int)eOcrEngineMode.OEM_TESSERACT_CUBE_COMBINED;
                                        processor.Init(Workspace.TessdataFolder, language, oem);
                                        processor.UseROI = true;
                                        processor.ROI = roi;
                                        unsafe
                                        {                                            
                                            texts[iROI] = processor.RecognizeBinaryImage(
                                               binImage.BinaryData, binImage.Width, binImage.Height);
                                        }
                                    }
                                }
                            }
コード例 #2
0
ファイル: Program.cs プロジェクト: git-thinh/tesseract301
    static oTesseractRequest __ocrExecute2(oTesseractRequest req, Bitmap bitmap)
    {
        using (TesseractProcessor processor = new TesseractProcessor())
        {
            processor.InitForAnalysePage();
            using (GreyImage greyImage = GreyImage.FromImage(bitmap))
            {
                //greyImage.Save(ImageFormat.Bmp, outFile2);

                ImageThresholder thresholder = new AdaptiveThresholder();
                using (BinaryImage binImage = thresholder.Threshold(greyImage))
                {
                    DocumentLayout doc = null;
                    switch (req.command)
                    {
                    case TESSERACT_COMMAND.GET_TEXT:
                        //string s = tes.GetText().Trim();
                        //req.output_text = s;
                        //req.output_count = s.Length;
                        req.ok = 1;
                        break;

                    default:
                        unsafe
                        {
                            doc = processor.AnalyseLayoutBinaryImage(
                                binImage.BinaryData, greyImage.Width, greyImage.Height);
                        }
                        if (doc != null)
                        {
                            var bs = new List <string>();
                            if (doc.Blocks.Count > 0)
                            {
                                for (int i = 0; i < doc.Blocks.Count; i++)
                                {
                                    for (int j = 0; j < doc.Blocks[i].Paragraphs.Count; j++)
                                    {
                                        bs.AddRange(doc.Blocks[j].Paragraphs[j].Lines
                                                    .Select(x => string.Format(
                                                                "{0}_{1}_{2}_{3}", x.Left, x.Top, x.Right, x.Bottom)));
                                    }
                                }
                            }
                            req.output_format = "left_top_right_bottom";
                            req.output_text   = string.Join("|", bs.ToArray());
                            req.output_count  = bs.Count;
                            req.ok            = 1;
                        }
                        break;
                    }
                }
            }
        }

        return(req);
    }
コード例 #3
0
        static void Simple2_Recognize()
        {
            int n_images = Images.Length;
            int i_image = n_images - 1;
            //i_image = 0;
            string fileName = Images[i_image];

            string imageFile = Path.Combine(InputFolder, fileName);

            string language = "eng";
            int oem = (int)eOcrEngineMode.OEM_DEFAULT;

            using (TesseractProcessor processor = new TesseractProcessor())
            {
                using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
                {
                    using (GreyImage greyImage = GreyImage.FromImage(bmp))
                    {

                        ImageThresholder thresholder = new AdaptiveThresholder();
                        using (BinaryImage binImage = thresholder.Threshold(greyImage))
                        {
                            DateTime started = DateTime.Now;
                            DateTime ended = DateTime.Now;

                            int i = 3;
                            //for (i = 0; i < 4; i++)
                            //for (i = 3; i < 4; i++)
                            {
                                oem = i;
                                processor.Init(TessdataFolder, language, oem);

                                string text = "";
                                unsafe
                                {
                                    started = DateTime.Now;

                                    text = processor.RecognizeBinaryImage(
                                        binImage.BinaryData, greyImage.Width, greyImage.Height);

                                    ended = DateTime.Now;

                                    Console.WriteLine("Duration recognition: {0} ms\n\n", (ended - started).TotalMilliseconds);
                                }

                                Console.WriteLine(
                                    string.Format("RecognizeMode: {1}\nRecognized Text:\n{0}\n++++++++++++++++++++++++++++++++\n", text, ((eOcrEngineMode)oem).ToString()));
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
        public static void Recognize()
        {
            int n_images = Workspace.Images.Length;
            int i_image  = n_images - 1;

            i_image = 0;
            string fileName = Workspace.Images[i_image];

            string imageFile = Path.Combine(Workspace.InputFolder, fileName);

            string language = "eng";
            int    oem      = 3;

            TesseractProcessor processor = new TesseractProcessor();

            using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
            {
                GreyImage greyImage = GreyImage.FromImage(bmp);

                DateTime            started     = DateTime.Now;
                AdaptiveThresholder thresholder = new AdaptiveThresholder();
                BinaryImage         binImage    = thresholder.Threshold(greyImage);
                DateTime            ended       = DateTime.Now;
                Console.WriteLine("Duration thresholding: {0} ms", (ended - started).TotalMilliseconds);

                binImage.Invert();

                //for (int i = 0; i < 4; i++)
                for (int i = 3; i < 4; i++)
                {
                    oem = i;
                    oem = (int)eOcrEngineMode.OEM_TESSERACT_CUBE_COMBINED;
                    processor.Init(Workspace.TessdataFolder, language, oem);

                    string text = "";
                    unsafe
                    {
                        started = DateTime.Now;

                        //string text = processor.Recognize(bmp);
                        text = processor.RecognizeBinaryImage(
                            binImage.BinaryData, binImage.Width, binImage.Height);

                        ended = DateTime.Now;
                        Console.WriteLine("Duration recognition: {0} ms\n\n", (ended - started).TotalMilliseconds);
                    }

                    Console.WriteLine(
                        string.Format("RecognizeMode: {1}\nText:\n{0}\n++++++++++++++++++++++++++++++++\n", text, ((eOcrEngineMode)oem).ToString()));
                }
            }
        }
コード例 #5
0
        static void Simple2_AnalyseLayout()
        {
            int n_images = Images.Length;
            int i_image = 0;
            for (; i_image < n_images; i_image++)
            {
                string fileName = Images[i_image];

                Console.WriteLine("{0} Image: {1}", i_image, fileName);

                string imageFile = Path.Combine(InputFolder, fileName);

                string name = Path.GetFileNameWithoutExtension(imageFile);

                string outFile = Path.Combine(OutputFolder, string.Format("Simple2_{0}_layout.bmp", name));
                string outFile2 = Path.Combine(OutputFolder, string.Format("Simple2_{0}_grey.bmp", name));
                string outFile3 = Path.Combine(OutputFolder, string.Format("Simple2_{0}_bin.bmp", name));

                using (TesseractProcessor processor = new TesseractProcessor())
                {
                    processor.InitForAnalysePage();
                    //processor.SetPageSegMode(ePageSegMode.PSM_AUTO);

                    using (Bitmap bmp = Bitmap.FromFile(imageFile) as Bitmap)
                    {
                        using (GreyImage greyImage = GreyImage.FromImage(bmp))
                        {
                            greyImage.Save(ImageFormat.Bmp, outFile2);

                            ImageThresholder thresholder = new AdaptiveThresholder();
                            using (BinaryImage binImage = thresholder.Threshold(greyImage))
                            {
                                binImage.Save(ImageFormat.Bmp, outFile3);

                                DateTime started = DateTime.Now;
                                DateTime ended = DateTime.Now;

                                DocumentLayout doc = null;

                                unsafe
                                {
                                    started = DateTime.Now;

                                    doc = processor.AnalyseLayoutBinaryImage(
                                        binImage.BinaryData, greyImage.Width, greyImage.Height);

                                    ended = DateTime.Now;

                                    Console.WriteLine("Duration AnalyseLayout: {0} ms", (ended - started).TotalMilliseconds);
                                }
                                Console.WriteLine(doc.ToString());

                                using (Image tmp = new Bitmap(bmp.Width, bmp.Height)) // prevents one-byte index format
                                {
                                    using (Graphics grph = Graphics.FromImage(tmp))
                                    {
                                        Rectangle rect = new Rectangle(0, 0, tmp.Width, tmp.Height);

                                        grph.DrawImage(bmp, rect, rect, GraphicsUnit.Pixel);

                                        grph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                                        foreach (Block block in doc.Blocks)
                                        {
                                            Render.DrawBlock(grph, block);
                                        }
                                    }

                                    tmp.Save(outFile);
                                }
                            }
                        }
                    }
                }
            }
        }