Exemplo n.º 1
0
        /// <summary>
        /// Event callback for the Clear Screen event generatead by the VideoProcessor
        /// </summary>
        private void VideoProcessor_ClearScreen(object sender, VideoProcessor.ClearScreenEventArgs e)
        {
            log.Debug("Detected Level Clear");

            Image <Gray, byte> grayscaleFrame = e.currentFrame.Mat.ToImage <Gray, byte>().Resize(640, 480, Inter.Cubic);
            //e.currentFrame.Save("clearmatch_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png");

            Dictionary <String, bool> events = new Dictionary <String, bool>
            {
                { "worldrecord", false },
                { "firstclear", false },
            };
            List <Rectangle> boundaries = new List <Rectangle>();

            foreach (EventTemplate tmpl in clearTemplates)
            {
                if (events[tmpl.eventType])
                {
                    continue;
                }
                Point loc = tmpl.getLocation(grayscaleFrame);
                if (!loc.IsEmpty)
                {
                    events[tmpl.eventType] = true;
                    boundaries.Add(ImageLibrary.ChangeSize(new Rectangle(loc.X, loc.Y, tmpl.template.Width, tmpl.template.Height), grayscaleFrame.Size, e.currentFrame.Size));
                    previewer.SetLastMatch(e.currentFrame, boundaries.ToArray());
                }
            }

            foreach (var evt in events)
            {
                if (evt.Value)
                {
                    log.Info(String.Format("Detected {0}.", evt.Key));
                    SMMServer.BroadcastEvent(evt.Key);
                }
            }

            if (Properties.Settings.Default.WarpWorldEnabled)
            {
                WarpWorld?.win();
            }

            // Read time from screen
            string clearTime = OCRLibrary.GetClearTimeFromFrame(e.currentFrame, e.commentsEnabled);

            SMMServer.BroadcastDataEvent("clear", clearTime);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Event Callback for the Black Screen event generated by the VideoProcessor
        /// </summary>
        private void VideoProcessor_BlackScreen(object sender, VideoProcessor.BlackScreenEventArgs e)
        {
            log.Debug(String.Format("Detected a black screen [{0}]", e.seconds));
            BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing black screen..."));

            double imageMatchPercent = ImageLibrary.CompareImages(e.currentFrame, levelDetailScreen);

            // Is this frame a 90% match to a level screen?
            if (imageMatchPercent > 0.90)
            {
                log.Info(String.Format("Detected new level. [{0}]", e.seconds));

                BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing level screen..."));

                Level level = OCRLibrary.GetLevelFromFrame(e.currentFrame);
                writeLevelToFile(level);
                SMMServer.BroadcastLevel(level);

                BeginInvoke((MethodInvoker)(() => ocrTextBox.Text = level.code + "  |  " + level.author + "  |  " + level.name));
                BeginInvoke((MethodInvoker)(() => processingLabel.Text = ""));

                previewer.SetLastMatch(e.currentFrame);
            }
            else
            {
                // Not a new level, see if we can detect a template.
                Dictionary <String, bool> events = new Dictionary <String, bool>
                {
                    { "death", false },
                    { "restart", false },
                    { "exit", false },
                    { "gameover", false },
                    { "skip", false }
                };

                BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing events..."));
                foreach (Image <Bgr, byte> f in e.frameBuffer)
                {
                    // Skip any empty frames in the buffer
                    if (f == null)
                    {
                        continue;
                    }

                    Image <Gray, byte> grayscaleFrame = f.Mat.ToImage <Gray, byte>().Resize(640, 480, Inter.Cubic);
                    //grayscaleFrame.Save("frame_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".png"); // XXX: Useful for debugging template false-negatives, and for getting templates

                    List <Rectangle> boundaries = new List <Rectangle>();
                    foreach (EventTemplate tmpl in templates)
                    {
                        if (events[tmpl.eventType])
                        {
                            continue;
                        }
                        Point loc = tmpl.getLocation(grayscaleFrame);
                        if (!loc.IsEmpty)
                        {
                            events[tmpl.eventType] = true;
                            boundaries.Add(ImageLibrary.ChangeSize(new Rectangle(loc.X, loc.Y, tmpl.template.Width, tmpl.template.Height), grayscaleFrame.Size, f.Size));
                            previewer.SetLastMatch(f, boundaries.ToArray());
                        }
                    }
                }
                BeginInvoke((MethodInvoker)(() => processingLabel.Text = ""));

                foreach (var evt in events)
                {
                    if (evt.Value)
                    {
                        log.Info(String.Format("Detected {0} [{1}].", evt.Key, e.seconds));
                        SMMServer.BroadcastEvent(evt.Key);

                        // Even though this will get sent on exiting after a clear, it only matters if a entry is active, and after marking it as a win it goes inactive.
                        if (evt.Key == "exit")
                        {
                            if (Properties.Settings.Default.WarpWorldEnabled)
                            {
                                WarpWorld?.lose();
                            }
                            if (JsonSettings.ClearOnExit)
                            {
                                clearJsonFile();
                            }
                        }
                        if (evt.Key == "skip" && JsonSettings.ClearOnSkip)
                        {
                            clearJsonFile();
                        }
                        if (evt.Key == "gameover" && JsonSettings.ClearOnGameover)
                        {
                            clearJsonFile();
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        private static string getStringFromLevelCodeImage(Image <Bgr, byte> image)
        {
            Image <Gray, byte> ocrReadyImage = ImageLibrary.PrepareImageForOCR(image);

            return(OCRLibrary.GetStringFromLevelCodeImage(ocrReadyImage));
        }