Exemple #1
0
        public bool ExtractFromImageTest(Bitmap image = null)
        {
            float meanConfidence;

            try
            {
                using (var engine = new TesseractEngine(tessdataPath, "eng", EngineMode.Default))
                {
                    // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            meanConfidence = page.GetMeanConfidence();
                            var resultText = page.GetText();
                            list.ingredientListRaw = resultText;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                exception = e;
                return(false);
            }
            var input  = list;
            var result = helper.Execute(ref input, DataCleanEnum.ListPrimary, meanConfidence);

            return(true);
        }
Exemple #2
0
 private void LeerCaptchaSunat()
 {
     Cursor = Cursors.WaitCursor;
     using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
     {
         using (var image = new System.Drawing.Bitmap(peCaptcha.Image))
         {
             using (var pix = PixConverter.ToPix(image))
             {
                 using (var page = engine.Process(pix))
                 {
                     page.GetMeanConfidence();
                     string captchaTexto  = page.GetText();
                     char[] eliminarChars = { '\n', ' ' };
                     captchaTexto = captchaTexto.TrimEnd(eliminarChars);
                     captchaTexto = captchaTexto.Replace(" ", string.Empty);
                     captchaTexto = Regex.Replace(captchaTexto, "[^a-zA-Z]+", string.Empty);
                     if (captchaTexto != string.Empty & captchaTexto.Length == 4)
                     {
                         iCaptcha.Text = captchaTexto.ToUpper();
                     }
                     else
                     {
                         ObtenerCaptcha();
                         LeerCaptchaSunat();
                     }
                 }
             }
         }
     }
     Cursor = Cursors.Default;
 }
Exemple #3
0
        public void ConvertToBitmap()
        {
            const double BaseRunTime = 793.382;
            const int    Runs        = 1000;

            var sourceFilePath = Path.Combine("./Data/Conversion", "photo_palette_8bpp.tif");

            using (var bmp = new Bitmap(sourceFilePath)) {
                // Don't include the first conversion since it will also handle loading the library etc (upfront costs).
                using (var pix = PixConverter.ToPix(bmp)) {
                }

                // copy 100 times take the average
                Stopwatch watch = new Stopwatch();
                watch.Start();
                for (int i = 0; i < Runs; i++)
                {
                    using (var pix = PixConverter.ToPix(bmp)) {
                    }
                }
                watch.Stop();

                var delta = watch.ElapsedTicks / (BaseRunTime * Runs);
                Console.WriteLine("Delta: {0}", delta);
                Console.WriteLine("Elapsed Ticks: {0}", watch.ElapsedTicks);
                Console.WriteLine("Elapsed Time: {0}ms", watch.ElapsedMilliseconds);
                Console.WriteLine("Average Time: {0}ms", (double)watch.ElapsedMilliseconds / Runs);

                Assert.That(delta, Is.EqualTo(1.0).Within(0.25));
            }
        }
        public void setImage(Bitmap screenshot)
        {
            this.screenshot = screenshot;
            Tuple <Double, Double> search1, search2, search3;

            //Get required values
            //Get values for top hints
            search1           = DetectColorWithUnsafe(screenshot, (byte)66, (byte)117, (byte)173, 0, 0, 0, screenshot.Size.Width, screenshot.Size.Height, 1);
            startPixel_Column = search1;
            search2           = DetectColorWithUnsafe(screenshot, (byte)82, (byte)146, (byte)198, 0, (int)search1.Item1, (int)search1.Item2, screenshot.Size.Width, screenshot.Size.Height, 1);
            widthColumn       = (int)(search2.Item1 - search1.Item1);
            search3           = DetectColorWithUnsafeDown(screenshot, (byte)255, (byte)255, (byte)255, 0, (int)search1.Item1, (int)search1.Item2, screenshot.Size.Width, screenshot.Size.Height, 1);
            heightColumn      = (int)(search3.Item2 - search1.Item2);
            //Get values for left hints
            search2        = DetectColorWithUnsafe(screenshot, (byte)82, (byte)146, (byte)198, 0, 0, (int)search3.Item2 - 1, (int)search1.Item1, screenshot.Size.Height, 1);
            startPixel_Row = search2;
            search1        = DetectColorWithUnsafe(screenshot, (byte)66, (byte)117, (byte)173, 0, (int)search2.Item1, (int)search2.Item2, screenshot.Size.Width, (int)search2.Item2 + 1, 1);
            widthRow       = (int)(search1.Item1 - search2.Item1);
            search3        = DetectColorWithUnsafeDown(screenshot, (byte)66, (byte)117, (byte)173, 0, (int)search2.Item1, (int)search2.Item2, screenshot.Size.Width, screenshot.Size.Height, 1);
            heightRow      = (int)(search3.Item2 - search2.Item2);

            //Grayscaling image and Playing with OCR
            Bitmap Section = MakeGrayscale(screenshot.Clone(new System.Drawing.Rectangle((int)startPixel_Column.Item1, (int)startPixel_Column.Item2, widthColumn, heightColumn), screenshot.PixelFormat));

            Section.Save("B:\\wat.png");
            Pix ocrImage = PixConverter.ToPix(Section);
            var page     = engine.Process(ocrImage);

            Console.WriteLine(page.GetText());

            Console.WriteLine("debug breakpoint");
        }
Exemple #5
0
        private void OnSubmitFileClicked(object sender, EventArgs args)
        {
            if (imageFile.PostedFile != null && imageFile.PostedFile.ContentLength > 0)
            {
                // for now just fail hard if there's any error however in a propper app I would expect a full demo.

                using (var engine = new TesseractEngine(Server.MapPath(@"~/tessdata"), "eng", EngineMode.Default))
                {
                    // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                    using (var image = new System.Drawing.Bitmap(imageFile.PostedFile.InputStream))
                    {
                        using (var pix = PixConverter.ToPix(image))
                        {
                            using (var page = engine.Process(pix))
                            {
                                meanConfidenceLabel.InnerText = String.Format("{0:P}", page.GetMeanConfidence());
                                resultText.InnerText          = page.GetText();
                            }
                        }
                    }
                }
                inputPanel.Visible  = false;
                resultPanel.Visible = true;
            }
        }
        // change ocr engine here
        public string GetText(Bitmap imgsource)
        {
            var ocrtext = string.Empty;

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var img = PixConverter.ToPix(imgsource))
                {
                    using (var page = engine.Process(img))
                    {
                        using (var iter = page.GetIterator())
                        {
                            iter.Begin();
                            do
                            {
                                // for each line in the paragraph
                                var line = iter.GetText(PageIteratorLevel.TextLine);
                                line     = line.Remove(line.Length - 1);
                                ocrtext += line + " ";
                            } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                        }

                        //ocrtext = page.GetText();
                    }
                }
            }

            return(ocrtext);
        }
Exemple #7
0
        private void OcrImage_Click(object sender, RoutedEventArgs e)
        {
            this.ScanResult.Text = string.Empty;

            this.ImageThumbnail.Source = this.GetThumbnail();

            using (var ocrEngine = new TesseractEngine(string.Format(CultureInfo.InvariantCulture, "{0}\\tessdata", System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location)), "eng", EngineMode.Default))
            {
                var scannableImage = this.GetScannableImage();

                var image = new MagickImage(scannableImage.ConvertToByteArray());
                try
                {
                    /* TextCleanerScript cleaner = new TextCleanerScript(); */

                    // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                    using (var pix = PixConverter.ToPix(scannableImage))
                    {
                        using (var page = ocrEngine.Process(pix))
                        {
                            // Console.WriteLine(page.GetMeanConfidence() + " : " + page.GetText());
                            this.ScanResult.Text += string.Format(
                                CultureInfo.InvariantCulture,
                                "Confidence {0} - #\"{1}\"#",
                                page.GetMeanConfidence(),
                                page.GetText());
                        }
                    }
                }
                finally
                {
                    image.Dispose();
                }
            }
        }
Exemple #8
0
        public string GetText(Bitmap imgsource)
        {
            var ocrtext = string.Empty;

            using (TesseractEngine engine = new TesseractEngine(Path.Combine(HttpRuntime.AppDomainAppPath, "tessdata"), "eng", EngineMode.Default))
            {
                //engine.DefaultPageSegMode = PageSegMode.SingleLine;
                using (var img = PixConverter.ToPix(imgsource))
                {
                    using (var page = engine.Process(img))
                    {
                        ocrtext = page.GetText();
                    }
                }
            }

            /*
             * var ocrtext = string.Empty;
             * using (var engine = new TesseractEngine(Path.Combine(HttpRuntime.AppDomainAppPath, "tessdata"), "eng", EngineMode.Default))
             * {
             *  using (var img = PixConverter.ToPix(imgsource))
             *  {
             *      using (var page = engine.Process(img))
             *      {
             *          ocrtext = page.GetText();
             *      }
             *  }
             * }
             */
            return(ocrtext);
        }
Exemple #9
0
        private void LeerCaptchaReniec()
        {
            Cursor = Cursors.WaitCursor;

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var image = new Bitmap(peCaptcha.Image))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            //var Porcentaje = String.Format("{0:P}", page.GetMeanConfidence());
                            page.GetMeanConfidence();
                            string captchaTexto  = page.GetText();
                            char[] eliminarChars = { '\n', ' ' };
                            captchaTexto = captchaTexto.TrimEnd(eliminarChars);
                            captchaTexto = captchaTexto.Replace(" ", string.Empty);
                            captchaTexto = Regex.Replace(captchaTexto, "[^a-zA-Z0-9]+", string.Empty);
                            if (captchaTexto != string.Empty & captchaTexto.Length == 4)
                            {
                                iCaptcha.Text = captchaTexto.ToUpper();
                            }
                            else
                            {
                                ObtenerCaptcha();
                                AplicacionFiltros();
                                LeerCaptchaReniec();
                            }
                        }
                    }
                }
            }
            Cursor = Cursors.Default;
        }
Exemple #10
0
        public async Task <(OCRPage, OctarineError, string)> GetTextFromStreamAsync(Stream stream)
        {
            System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
            OCRPage page             = new OCRPage(img);
            string  dir;

            switch (this.quality)
            {
            case 1:
                dir = Config.GetDataDir("tessdata_fast");
                break;

            case 3:
                dir = Config.GetDataDir("tessdata_best");
                break;

            default:
                dir = Config.GetDataDir("tessdata");
                break;
            }
            try {
                using (var engine = new TesseractEngine(dir, this.language.Code, EngineMode.Default)) {
                    Bitmap bmp = new System.Drawing.Bitmap(img);
                    Pix    pix = PixConverter.ToPix(bmp);
                    using (var oPage = engine.Process(pix)) {
                        await Task.Delay(1);

                        page.AddFragment(oPage.GetText(), oPage.RegionOfInterest.X1, oPage.RegionOfInterest.Y1, oPage.RegionOfInterest.Width, oPage.RegionOfInterest.Height);
                        return(page, OctarineError.Success, null);
                    }
                }
            } catch (Exception ex) {
                return(null, OctarineError.EngineError, ex.Message);
            }
        }
        private async void Import_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog()
            {
                Filter = "图像文件(.jpg;*.png)|*.jpg;*.png",
                Title  = "选取文件"
            };

            if (dialog.ShowDialog().GetValueOrDefault())
            {
                var filePath = dialog.FileName;
                using var ocr = new TesseractEngine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "tessdata"), "chi_sim", EngineMode.Default);
                var pix = PixConverter.ToPix(new System.Drawing.Bitmap(filePath));
                using var page = ocr.Process(pix);
                string text = page.GetText();
                if (!String.IsNullOrWhiteSpace(text))
                {
                    TextBlockSrc.Text = $"识别结果为{text}";
                    var result = await Translate(text);

                    TextBlockResult.Text = result;
                }
                else
                {
                    TextBlockSrc.Text = "未识别到文字";
                }
            }
        }
        public ActionResult Submit(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                ViewBag.ShowResult = true;
                ViewBag.Result     = "File not found";
                return(View("Index"));
            }



            using (var engine = new TesseractEngine(Server.MapPath(@"~/tessdata"), "eng", EngineMode.Default))
            {
                // have to load Pix via a bitmap since Pix doesn't support loading a stream.
                using (var image = new System.Drawing.Bitmap(file.InputStream))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            ViewBag.ShowResult      = true;
                            ViewBag.MeansConfidence = String.Format("{0:P}", page.GetMeanConfidence());
                            ViewBag.Result          = page.GetText();
                        }
                    }
                }
            }

            return(View("Index"));
        }
Exemple #13
0
        public string ocrByBitmap(Bitmap b)
        {
            //this.saveImageFile(b, Convert.ToInt64((DateTime.UtcNow - new DateTime(1970,1,1,0,0,0,0)).TotalSeconds).ToString());

            //TesseractEngine engine = new TesseractEngine(@"../tessdata", "fontyp", EngineMode.TesseractOnly);

            Pix  img;
            Page page;

            try
            {
                img = PixConverter.ToPix(b);

                page = engine.Process(img);

                string text = page.GetText();

                page.Dispose();

                return(text.ToString());
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }
        }
Exemple #14
0
        public async Task <string> GetOcrTextFromFile(IFormFile file)
        {
            using (var memoryStream = new MemoryStream())
            {
                await file.CopyToAsync(memoryStream);

                using (var img = Image.FromStream(memoryStream))
                {
                    string tesspath = Path.Combine(_env.WebRootPath, "tessdata");
                    using (var engine = new TesseractEngine(tesspath, "tur", EngineMode.Default))
                    {
                        using (var image = new System.Drawing.Bitmap(img))
                        {
                            using (var pix = PixConverter.ToPix(image))
                            {
                                using (var page = engine.Process(pix))
                                {
                                    return(page.GetText());
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #15
0
        void Button2Click(object sender, EventArgs e)
        {
            Bitmap roibmp;
            Pix    roipix;

            buttonRight.Enabled = false;
            buttonNext.Enabled  = false;
            button2.Enabled     = false;
            Rectangle roi = ramka.getRoi();

            Cursor = Cursors.WaitCursor;
            roibmp = ramka.Image.Clone(roi, ramka.Image.PixelFormat);
            roipix = PixConverter.ToPix(roibmp);
            roibmp.Dispose();

            Pix    roipix1 = roipix.Deskew();
            Bitmap newbm   = PixConverter.ToBitmap(roipix1);

            roipix.Dispose();
            ramka.Image         = newbm;
            Cursor              = Cursors.Default;
            buttonRight.Enabled = true;
            buttonNext.Enabled  = true;
            button2.Enabled     = true;
        }
Exemple #16
0
        /// <summary>
        /// 文字認識
        /// </summary>
        private void AnalyzeString()
        {
            // Bitmapを処理、適度に切り取った方がOCRを扱いやすい
            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(20, 90, 450, 100);
            SrcImg = SrcImg.Clone(rect, SrcImg.PixelFormat);

            // 確認用の画面に画像を出力
            this.Dispatcher.Invoke(() => {
                this._Image_crop.Source = ConvertBitmapToIS(SrcImg);
            });

            // 以下OCR
            string langPath = @"C:\tessdata";
            string lngStr   = "eng";

            //画像ファイルでテストするならパス指定
            //var img = new Bitmap(@"C:\test.jpg");
            var img = SrcImg;

            using (var tesseract = new Tesseract.TesseractEngine(langPath, lngStr))
            {
                // OCRの実行
                Pix            pix  = PixConverter.ToPix(img);
                Tesseract.Page page = tesseract.Process(pix);

                //表示
                Console.WriteLine(page.GetText());
                Console.ReadLine(); // err?
            }
        }
Exemple #17
0
        async void ButtonRight(object sender, EventArgs e)
        {
            buttonRight.Enabled = false;
            buttonNext.Enabled  = false;
            button2.Enabled     = false;

            Bitmap    roibmp;
            Pix       roipix;
            Rectangle roi = ramka.getRoi();

            Cursor = Cursors.WaitCursor;
            roibmp = ramka.Image.Clone(roi, ramka.Image.PixelFormat);
            roipix = PixConverter.ToPix(roibmp);
            roibmp.Dispose();

            Task <string> ocr = Task.Run <string>(() => {
                return(DoOCR(roipix));
            });
            string txt = await ocr;

            System.Diagnostics.Debug.WriteLine(txt);
            if (gOcrTxtForm == null || gOcrTxtForm.IsDisposed)
            {
                gOcrTxtForm = new OCRText();
                gOcrTxtForm.Show();
            }
            gOcrTxtForm.SetText(txt);
            buttonRight.Enabled = true;
            buttonNext.Enabled  = true;
            button2.Enabled     = true;
            Cursor = Cursors.Default;
            roipix.Dispose();
        }
Exemple #18
0
        public void DetectText(string fileToRecognize)
        {
            var findText = string.Empty;

            try
            {
                var bitmap = Common.GetBitmap(fileToRecognize);

                using (var engine = new TesseractEngine(tessdata, language, EngineMode.TesseractAndCube))
                {
                    versionEngine = engine.Version;

                    using (Pix img = PixConverter.ToPix(bitmap))
                    {
                        using (Page page = engine.Process(img))
                        {
                            findText = page.GetText();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                findText = ex.Message;
            }

            this.annotation = findText;
        }
Exemple #19
0
        /// <summary>
        /// 获取验证码并自动识别
        /// </summary>
        /// <returns></returns>
        public string GetVerficar(string path)
        {
            var result = "";

            httpItem.URL        = $"{urlbase}/verficar.do";
            httpItem.ResultType = ResultType.Byte;
            httpItem.Cookie     = cookie;
            httpResult          = httpHelper.GetHtml(httpItem);
            var image = byteArrayToImage(httpResult.ResultByte);

            image.Save(path + "code.bmp");

            cookie = httpResult?.Cookie.Replace("; Path=/erp", "").Trim();

            using (var engine = new TesseractEngine(path + "tessdata", "eng", EngineMode.Default))
            {
                engine.SetVariable("tessedit_char_whitelist", "0123456789");
                using (var pix = PixConverter.ToPix((Bitmap)image))
                {
                    using (var page = engine.Process(pix))
                    {
                        result = page.GetText();
                    }
                }
            }
            result = result.Replace("\n", "").Replace(" ", "").Trim();

            var timeSpan = (DateTime.Now.AddMilliseconds(expire * 1000) - DateTime.Now);

            RedisService.Instance.StringSet($"{RedisPrimaryKey.WebCrawlingCookie}/{uuid}", cookie, timeSpan);

            return(result);
        }
        public string Post()
        {
            var dirTessData = HttpContext.Current.Server.MapPath(@"\") + @"..\tessdata";

            var httpRequest = HttpContext.Current.Request;

            var inpStream = httpRequest.Files[0].InputStream;
            var hashCode  = GetHash(inpStream);
            var res       = MemoryCache.Default.Get(hashCode);

            //if cached get cached value
            if (res != null)
            {
                return(res as string);
            }

            byte[] buff = new byte[inpStream.Length];
            inpStream.Read(buff, 0, (int)inpStream.Length);

            TesseractEngine engine =
                new TesseractEngine(
                    dirTessData, "eng",
                    EngineMode.Default);

            System.Drawing.Bitmap b = new System.Drawing.Bitmap(inpStream);
            Pix img  = PixConverter.ToPix(b);
            var page = engine.Process(img);
            var text = page.GetText();

            MemoryCache.Default.Add(hashCode, text, DateTime.Now.AddMinutes(20));

            return(text);
        }
Exemple #21
0
        void Button2Click(object sender, EventArgs e)
        {
            Bitmap roibmp;
            Pix    roipix;
            Mat    src;

            buttonRight.Enabled = false;
            buttonNext.Enabled  = false;
            button2.Enabled     = false;
            OpenCvSharp.Rect roi = ramka.getRoi();
            Cursor = Cursors.WaitCursor;
            if (roi.X != -1)
            {
                src = ramka.Image[roi];
            }
            else
            {
                src = ramka.Image;
            }
            roibmp = src.ToBitmap();
            roipix = PixConverter.ToPix(roibmp);
            Pix roipix1 = roipix.Deskew();

            Bitmap newbm  = PixConverter.ToBitmap(roipix1);
            Mat    newmat = BitmapConverter.ToMat(newbm);

            ramka.Image = newmat;
            Cursor      = Cursors.Default;
            src.Dispose();
            newbm.Dispose();
            roipix.Dispose();
            buttonRight.Enabled = true;
            buttonNext.Enabled  = true;
            button2.Enabled     = true;
        }
        public void RecognizeText(int id)
        {
            Screenshot screenshot = this._repository.Get(id);

            //var i = new System.Drawing.Bitmap(new MemoryStream(screenshot.Data));
            //i.Save("image.bmp");

            using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
            {
                using (var image = new System.Drawing.Bitmap(new MemoryStream(screenshot.Data)))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            var text = page.GetText();
                            Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence());

                            Console.WriteLine("Text (GetText): \r\n{0}", text);
                            Console.WriteLine("Text (iterator):");
                            using (var iter = page.GetIterator())
                            {
                                iter.Begin();

                                do
                                {
                                    do
                                    {
                                        do
                                        {
                                            do
                                            {
                                                if (iter.IsAtBeginningOf(PageIteratorLevel.Block))
                                                {
                                                    Console.WriteLine("<BLOCK>");
                                                }

                                                var t = iter.GetText(PageIteratorLevel.Word);
                                                this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create(t));
                                                Console.Write(" ");

                                                if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word))
                                                {
                                                    Console.WriteLine();
                                                }
                                            } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word));

                                            if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine))
                                            {
                                                Console.WriteLine();
                                            }
                                        } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine));
                                    } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para));
                                } while (iter.Next(PageIteratorLevel.Block));
                            }
                        }
                    }
                }
            }
        }
Exemple #23
0
        public TesseractOCRResult OCRimage(Image ImageToUse, int Zoomlevel)
        {
            DateTime Starttime = DateTime.Now;

            //load in image
            LoadImage(ImageToUse);
            //post process the image
            ImageToOCR = AccordImageProcessing.AccordImageProc.ImageProcessing(ImageToOCR, Zoomlevel);
            //Convert to Tesseract format
            Pix img = PixConverter.ToPix(ImageToOCR);
            // OCR it
            Page page = TesseractOCRCore.Process(img);
            //get test
            string text = page.GetText();

            //Get confidence
            float Confidence = page.GetMeanConfidence();
            //Get Time
            DateTime EndTime   = DateTime.Now;
            double   TimeTaken = (EndTime - Starttime).TotalSeconds;

            page.Dispose();
            img.Dispose();
            return(new TesseractOCRResult(text, TimeTaken, Confidence));
        }
Exemple #24
0
        public string GetText(String image)
        {
            var OCRPicture = Util.PicToBitmap(image).Contrast().ToGreyscale();

            if (ImageEditing.IfRotated(OCRPicture))
            {
                OCRPicture.RotateFlip(RotateFlipType.Rotate90FlipNone);
            }
            //Test output
            OCRPicture.Save("OCR.jpg");



            var ocrtext = string.Empty;

            using (var engine = new TesseractEngine(@"../../tessdata", "lit4", EngineMode.Default, @"../../config"))
            {
                using (var img = PixConverter.ToPix(OCRPicture))
                {
                    using (var page = engine.Process(img))
                    {
                        ocrtext = page.GetText();
                    }
                }
            }

            return(ocrtext);
        }
Exemple #25
0
        async Task <Bitmap> OCRBasic()
        {
            var traineddata = @"C:\Users\huser\Desktop\book\ZycyCollecter\ZycyCollecter\tessdata";

            var orientations = new RotateFlipType[]
            {
                RotateFlipType.RotateNoneFlipNone,
                RotateFlipType.Rotate90FlipNone,
                RotateFlipType.Rotate180FlipNone,
                RotateFlipType.Rotate270FlipNone,
            };

            foreach (var orientation in orientations)
            {
                var bitmap = await GetBitmap();

                bitmap.RotateFlip(orientation);
                using var tesseranct = new TesseractEngine(traineddata, "jpn");
                using var page       = tesseranct.Process(PixConverter.ToPix(bitmap));
                var text = page.GetText().Split('\n').Select(s => s.Trim()).Where(s => !string.IsNullOrEmpty(s));
                displayText.Text += $"\n[{orientation.ToString()}]\n{string.Join("\n", text)}\n";
            }

            return(await GetBitmap());
        }
Exemple #26
0
        public async Task <object> GetText(dynamic input)
        {
            Rectangle rect = Rectangle.FromLTRB(input.x, input.y, input.x + input.width, input.y + input.height);

            // Copy a portion of the screen.
            Bitmap bmp = new Bitmap(rect.Width, rect.Height);

            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(screenImage, 0, 0, rect, GraphicsUnit.Pixel);
            }

            Pix pix = null;

            try
            {
                pix = PixConverter.ToPix(bmp).ConvertRGBToGray().Scale(4, 4);
            }
            catch (Exception e)
            {
                if (e.InnerException == null)
                {
                    throw;
                }
                else
                {
                    throw e.InnerException;
                }
            }

            using (Page page = engine.Process(pix))
            {
                return(page.GetText());
            }
        }
Exemple #27
0
 private void LeerCaptchaSunat()
 {
     using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
     {
         using (var image = new System.Drawing.Bitmap(pictureCapcha.Image))
         {
             using (var pix = PixConverter.ToPix(image))
             {
                 using (var page = engine.Process(pix))
                 {
                     var    Porcentaje    = String.Format("{0:P}", page.GetMeanConfidence());
                     string CaptchaTexto  = page.GetText();
                     char[] eliminarChars = { '\n', ' ' };
                     CaptchaTexto = CaptchaTexto.TrimEnd(eliminarChars);
                     CaptchaTexto = CaptchaTexto.Replace(" ", string.Empty);
                     CaptchaTexto = Regex.Replace(CaptchaTexto, "[^a-zA-Z]+", string.Empty);
                     if (CaptchaTexto != string.Empty & CaptchaTexto.Length == 4)
                     {
                         txttexto.Text = CaptchaTexto.ToUpper();
                     }
                     else
                     {
                         CargarImagenSunat();
                     }
                 }
             }
         }
     }
 }
Exemple #28
0
        /// <summary>
        /// Main OCR function (based on Google's open TesseractEngine)
        /// </summary>
        /// <param name="engine">Tesseract Instance</param>
        /// <param name="bmp">Image in question</param>
        /// <returns>ocr'd string from image</returns>
        public static String ocr(TesseractEngine engine, Bitmap bmp)
        {
            // Scale size of bitmap to increase ocr quality
            int width  = bmp.Width * 5;
            int height = bmp.Height * 5;

            Bitmap newImage = new Bitmap(width, height);

            // Enhance quality of image
            using (Graphics gr = Graphics.FromImage(newImage))
            {
                gr.SmoothingMode     = SmoothingMode.HighQuality;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                gr.DrawImage(bmp, new Rectangle(0, 0, width, height));
            }

            var ocrtext = string.Empty;

            // Turn colored image into grayscale Image (improves OCR success percentage)
            using (var img = PixConverter.ToPix(MakeGrayscale3(newImage)))
            {
                // Read Text from Image
                using (var page = engine.Process(img))
                {
                    ocrtext = page.GetText();
                }
            }
            // Get Rid off whitespaces and newlines (often too many whitespaces included)
            return(ocrtext.ToString().Replace(" ", "").Replace("\n", ""));
        }
Exemple #29
0
        public ActionResult submit(HttpPostedFileBase file)
        {
            if (file == null || file.ContentLength == 0)
            {
                ViewBag.Result = true;
                ViewBag.res    = "File not Found";
                return(View("AddBook"));
            }
            using (var engine = new TesseractEngine(Server.MapPath(@"~/tessdata"), "eng", EngineMode.Default))
            {
                using (var image = new System.Drawing.Bitmap(file.InputStream))
                {
                    using (var pix = PixConverter.ToPix(image))
                    {
                        using (var page = engine.Process(pix))
                        {
                            ViewBag.Result = true;
                            ViewBag.res    = page.GetText();
                            ViewBag.metin  = ISBN_No(ViewBag.res);
                            ViewBag.mean   = String.Format("{0:p}", page.GetMeanConfidence());
                        }
                    }
                }
            }

            return(View("AddBook"));
        }
Exemple #30
0
        public static string GetText(Bitmap imgsource)
        {
            try
            {
                if (useNetworkOCR)
                {
                    return(Networking.NetworkOCR(imgsource, networkOCRIP, networkOCRPort));
                }
            }
            catch (SocketException)
            {
                PluginAPI.WriteLine("Remote OCR server error. Defaulting to the local engine.");
                if (eng == null)
                {
                    PluginAPI.WriteLine("Local OCR engine is null!");
                    throw new Exception("OCR engine is null!");
                }
            }

            var ocrtext = string.Empty;

            using (var img = PixConverter.ToPix(imgsource))
            {
                lock (RSTools._lockObj)                 // You can only process one image at a time
                {
                    using (var page = eng.Process(img)) // Program.engine
                    {
                        ocrtext = page.GetText();
                    }
                }
            }

            //imgsource.Dispose();
            return(ocrtext);
        }