Esempio n. 1
0
        /// <summary>
        /// Begins watching for match points
        /// </summary>
        /// <returns></returns>
        public async Task RunAsync()
        {
            Logger.Info("Starting Watch", LOG_APP);

            while (true)
            {
                //Find a stream
                Logger.Trace("Finding a channel to check...", LOG_APP);
                var stream = await GetStreamAsync();

                //Before we are even allowed to start OCR, lets validate we can host this channel
                Logger.Info("Checking Channel {0}", LOG_APP, stream.Channel.Name);
                if (await CanHostAsync(stream.Channel.Name))
                {
                    //Cleanup the previous capture and create a new one
                    Logger.Trace("Creating a channel capture", LOG_APP);
                    using (_capture = new OCRCapture(stream.Channel.Name)
                    {
                        Logger = Logger
                    })
                    {
                        //_capture.ShowWindows();

                        //Begin capturing
                        _capture.Begin();
                        await Task.Delay(10);

                        try
                        {
                            //Scan the captured channel when it becomes available after some time
                            await WatchCaptureAsync();
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Failed to scan channel {0}, {1}", LOG_APP, stream.Channel.Name, e.Message);
                        }

                        //End capturing
                        _capture.End();
                        Logger.Trace("Capture Ended", LOG_APP);
                    }

                    //Set capture to null
                    _capture = null;
                    await Task.Delay(100);
                }
            }
        }
Esempio n. 2
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;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }