Esempio n. 1
0
        private static string GetCaptchaText(string captchaFilePath)
        {
            string captchaText = null;
            Pix    captcha     = null;

            try
            {
                captcha = Pix.LoadFromFile(captchaFilePath);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error loading captcha file");
            }

            if (captcha != null)
            {
                var grayCaptcha      = captcha.ConvertRGBToGray();
                var binarizedCaptcha = grayCaptcha.BinarizeSauvolaTiled(10, 0.75f, 1, 2);

                var engine = new TesseractEngine(Path.GetFullPath("tessdata"), "eng", EngineMode.Default);
                engine.SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
                var page = engine.Process(binarizedCaptcha, PageSegMode.SparseText);

                captchaText = page.GetText();
                captchaText = Regex.Replace(captchaText, @"\s+", string.Empty);
            }

            return(captchaText);
        }
Esempio n. 2
0
 /// <summary>
 /// Converts Leptonica
 /// <see cref="Tesseract.Pix"/>
 /// to grayscale.
 /// </summary>
 /// <remarks>
 ///  In .Net image is converted only if this is 32bpp image. In java image is
 ///  converted anyway using different Leptonica methods depending on
 ///  image depth.
 /// </remarks>
 /// <param name="pix">
 ///
 /// <see cref="Tesseract.Pix"/>
 /// object to be processed
 /// </param>
 /// <returns>
 /// preprocessed
 /// <see cref="Tesseract.Pix"/>
 /// object
 /// </returns>
 internal static Pix ConvertToGrayscale(Pix pix)
 {
     if (pix != null)
     {
         int depth = pix.Depth;
         if (depth == 32)
         {
             return(pix.ConvertRGBToGray());
         }
         else
         {
             LogManager.GetLogger(typeof(TesseractOcrUtil))
             .Info(MessageFormatUtil.Format(Tesseract4LogMessageConstant.CANNOT_CONVERT_IMAGE_TO_GRAYSCALE, depth));
             return(pix);
         }
     }
     else
     {
         return(pix);
     }
 }
        private void bw_DEBUG_OCR(object sender, DoWorkEventArgs e)
        {
            var    watch = Stopwatch.StartNew();
            string processedTextResults    = "";
            string preProcessedTextResults = "";
            int    hits = 0;

            BackgroundWorker worker = sender as BackgroundWorker;

            Process[] processes = Process.GetProcessesByName("Eternal");

            Process p = processes.FirstOrDefault();
            IntPtr  windowHandle;

            if (p != null)
            {
                windowHandle = p.MainWindowHandle;

                // difference is 20 ms between these two function
                //Image img22 = CaptureWindow(windowHandle);
                Pix img = CaptureWindowPix(windowHandle);

                img = img.ConvertRGBToGray(0.40f, 0.34f, 0.26f);

                //img = img.BinarizeOtsuAdaptiveThreshold(img.Width / 5, img.Height / 5, 10, 10, 0.1f);
                // img = img.BinarizeSauvolaTiled();
                //img = img.INVERT

                img = img.Scale(scalingFactor, scalingFactor);
                //img = img.BinarizeOtsuAdaptiveThreshold(img.Width / 5, img.Height / 5, 10, 10, 0.1f);

                //img = img.UNSHARPMASK
                //img = img.BinarizeOtsuAdaptiveThreshold(2000, 2000, 0, 0, 0.0f);
                //img = img.SELECTBYSIZE // removeNoise

                //var dpiX = 300;
                //var dpiY = 300;

                //Bitmap screenshotBitmap = PixConverter.ToBitmap(img);
                //screenshotBitmap.SetResolution(dpiX, dpiY);

                //DEBUG_PrintImage(img, "manualPreProcessing", watch);

                for (int i = 0; i < 12; i++)
                {
                    //formGraphics.DrawRectangle(new Pen(new SolidBrush(Color.Pink)), Cards[i].WholeCardBounding);
                    //formGraphics.DrawRectangle(new Pen(new SolidBrush(Color.Aqua)), Cards[i].TextboxBounding);
                    //formGraphics.DrawRectangle(new Pen(new SolidBrush(Color.Bisque)), Cards[i].RankLocation.X, Cards[i].RankLocation.Y, 10, 10);

                    Rect textbox_Scaled = new Rect(
                        Cards[i].TextboxBounding.X * (int)scalingFactor,
                        Cards[i].TextboxBounding.Y * (int)scalingFactor,
                        Cards[i].TextboxBounding.Width * (int)scalingFactor,
                        Cards[i].TextboxBounding.Height * (int)scalingFactor);

                    using (Page processedImage = ocrEngine.Process(img, textbox_Scaled))
                    {
                        DEBUG_PrintImage(processedImage.GetThresholdedImage(), "lower rez (individual)" + i, watch);

                        var text = processedImage.GetText();
                        preProcessedTextResults += text.Replace("\n", "") + Environment.NewLine;
                        text = CleanText(text);
                        processedTextResults += text + Environment.NewLine;
                        if (cardRankings.ContainsKey(text))
                        {
                            Cards[i].Rank = cardRankings[text];
                            hits++;
                        }
                        else if (!String.IsNullOrEmpty(text))
                        {
                            List <SymSpell.suggestItem> suggestions = null;
                            suggestions = SymSpell.Lookup(text, "", SymSpell.editDistanceMax);
                            if (suggestions.Count > 0)
                            {
                                Cards[i].Rank = cardRankings[suggestions.First().term];
                                hits++;
                            }
                            else
                            {
                                Cards[i].Rank = "U";
                            }
                        }
                        else
                        {
                            Cards[i].Rank = string.Empty;
                        }
                    }
                }

                RenderRankings();
            }

            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;

            OutputTestResults(elapsedMs, processedTextResults, hits, preProcessedTextResults);
        }
Esempio n. 4
0
 public override Pix Handle(Pix request)
 {
     request = request?.ConvertRGBToGray();
     return(base.Handle(request));
 }
Esempio n. 5
0
        public static void Capture(IntPtr handle, Point pos, Size size, ref OCRCapture ocrCapture, bool debug = false)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }
            Bitmap b = CaptureWindowArea(handle, pos, size);

            if (b == null)
            {
                return;
            }
            if (debug)
            {
                Directory.CreateDirectory(Application.StartupPath + @"\debug");
            }
            if (debug)
            {
                b.Save(Application.StartupPath + @"\debug\00_input.png");
            }
            using (Pix pixc = PixConverter.ToPix(b)) {
                b.Dispose();
                using (Pix pixg = pixc.ConvertRGBToGray(0, 0, 0)) {
                    if (debug)
                    {
                        pixg.Save(Application.StartupPath + @"\debug\01_grayscale.png");
                    }
                    using (Pix pixs = pixg.ScaleGrayLI(Config.OCRScaleFactor, Config.OCRScaleFactor)) {
                        if (debug)
                        {
                            pixs.Save(Application.StartupPath + @"\debug\02_scale.png");
                        }
                        //pix = pix.UnsharpMaskingGray(5, 2.5f); //issues with light text on light bg
                        using (Pix pixb = pixs.BinarizeOtsuAdaptiveThreshold(2000, 2000, 0, 0, 0.0f)) {
                            if (debug)
                            {
                                pixb.Save(Application.StartupPath + @"\debug\03_binarize.png");
                            }
                            float pixAvg = pixb.AverageOnLine(0, 0, pixb.Width - 1, 0, 1);
                            pixAvg += pixb.AverageOnLine(0, pixb.Height - 1, pixb.Width - 1, pixb.Height - 1, 1);
                            pixAvg += pixb.AverageOnLine(0, 0, 0, pixb.Height - 1, 1);
                            pixAvg += pixb.AverageOnLine(pixb.Width - 1, 0, pixb.Width - 1, pixb.Height - 1, 1);
                            pixAvg /= 4.0f;
                            using (Pix pixi = pixAvg > 0.5f ? pixb.Invert() : pixb) {
                                if (debug)
                                {
                                    pixi.Save(Application.StartupPath + $@"\debug\04_invert_{pixAvg > 0.5f}.png");
                                }
                                using (Pix pixn = pixi.SelectBySize(Config.OCRNoiseSize, Config.OCRNoiseSize, Config.OCRNoiseConnectivity, Config.OCRNoiseType, Config.OCRNoiseRelation)) {
                                    if (debug)
                                    {
                                        pixn.Save(Application.StartupPath + @"\debug\05_removenoise.png");
                                    }
                                    //pixn.ClipToForeground(IntPtr.Zero);
                                    using (Pix pix = pixn.AddBorder(Config.OCRPadding, 0)) {
                                        if (debug)
                                        {
                                            pix.Save(Application.StartupPath + @"\debug\06_border.png");
                                        }
                                        pix.XRes = 300;
                                        pix.YRes = 300;

                                        using (Page page = Engine.Process(pix, PageSegMode.SingleLine)) {
                                            ocrCapture.Text       = page.GetText().Trim();
                                            ocrCapture.Confidence = page.GetMeanConfidence();
                                            ocrCapture.Iterations++;
                                            if (ocrCapture.Confidence >= Config.OCRMinimumConfidence)
                                            {
                                                if (decimal.TryParse(ocrCapture.Text, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out decimal cooldown))
                                                {
                                                    if (cooldown < Config.CooldownMaxPossible)
                                                    {
                                                        ocrCapture.Cooldown = cooldown;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }