Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        protected void getPrimeFromFile(Uri imageUri, string filename)
        {
            // Wait for IO to complete and stuff
            // Required to let game save the file properly
            Thread.Sleep(500);

            // Crop image, save temporarily
            using (Bitmap image = new Bitmap(imageUri.AbsolutePath))
            {
                // TODO: Add the crop rect config to the option sheet when implementing
                Rectangle croprect = new Rectangle(757, 884, 406, 32);
                Bitmap    temp     = new Bitmap(croprect.Width, croprect.Height);
                Graphics  g        = Graphics.FromImage(temp);
                g.DrawImage(image, -croprect.X, -croprect.Y);
                temp.Save("./tempcrop.jpg");
            }

            // OCR
            using (TesseractEngine engine = new TesseractEngine("./tessdata", "eng", EngineMode.Default))
            {
                // Tweaking some things for this use case
                engine.SetVariable("load_system_dawg", false);
                engine.SetVariable("load_freq_dawg", false);
                engine.SetVariable("user_words_suffix", "user-words");
                engine.SetVariable("tessedit_char_whitelist", "QWERTYUIOPASDFGHJKLZXCVBNM &");
                engine.SetVariable("tessedit_char_blacklist", ".,/=-^*()!@#$%\\][{}");
                engine.SetVariable("language_model_penalty_non_dict_word", 0.6);

                using (Pix image = Pix.LoadFromFile("./tempcrop.jpg"))
                {
                    // Scale up for additional reliability
                    Pix filteredimage = image.Scale(3.0f, 3.0f);

                    using (var page = engine.Process(filteredimage))
                    {
                        string text = page.GetText().Trim();

                        if (String.IsNullOrWhiteSpace(text))
                        {
                            text = "UNKNOWN";
                        }

                        else
                        // Call sheets
                        if (!Sheet.UpdateItemCount(text))
                        {
                            text = "[UNSYNCED] " + text;
                        }

                        Dispatcher.Invoke(() =>
                        {
                            ItemList.Items.Add(string.Format("{0} - {1}", filename, text));
                            LogFile.Write(string.Format("{0} - {1}" + Environment.NewLine, filename, text));
                            LogFile.Flush(); // To ensure no data gets lost, since i'm leaving the stream open
                        });
                    }
                }
            }

            // Cleanup!
            File.Delete("./tempcrop.jpg");
        }
Ejemplo n.º 3
0
 public override Pix Handle(Pix request)
 {
     request = request?.Scale(this.ScaleX, this.ScaleY);
     return(base.Handle(request));
 }
Ejemplo n.º 4
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)
            {
                lock (MainWindow.ImageLock) { 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.Scale(Config.OCRScaleFactor, Config.OCRScaleFactor)) { //was ScaleGrayLI
                        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 = 0;

                            /* 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 : pixb) { //pixb.Invert()
                                if (debug)
                                {
                                    pixi.Save(Application.StartupPath + $@"\debug\04_invert_{pixAvg > 0.5f}.png");
                                }
                                using (Pix pixn = pixi.Clone()) { //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.Clone()) { //pixn.AddBorder(Config.OCRPadding, 0)
                                        if (debug)
                                        {
                                            lock (MainWindow.ImageLock) { 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;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }