/// <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(); } } } } }
/// <summary> /// Event Callback for the Black Screen event generated by the VideoProcessor /// </summary> private void VideoProcessor_BlackScreen(object sender, VideoProcessor.VideoEventArgs e) { log.Debug("Detected a black screen"); BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing black screen...")); // Is this a new level? Image <Bgr, byte> frame = e.frameBuffer[e.frameBuffer.Length - 5]; double imageMatchPercent = ImageLibrary.CompareImages(frame, levelSelectScreen); if (imageMatchPercent > 0.94) { log.Info("Detected new level."); previewer.SetLastMatch(frame, new Rectangle[] { levelCodeArea, creatorNameArea, levelTitleArea }); BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing level screen...")); Level level = getLevelFromCurrentFrame(frame); writeLevelToFile(level); SMMServer.BroadcastLevel(level); BeginInvoke((MethodInvoker)(() => ocrTextBox.Text = level.code + " | " + level.author + " | " + level.name)); BeginInvoke((MethodInvoker)(() => processingLabel.Text = "")); } 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 }, }; BeginInvoke((MethodInvoker)(() => processingLabel.Text = "Processing events...")); foreach (Image <Bgr, byte> f in e.frameBuffer) { 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}.", evt.Key)); SMMServer.BroadcastEvent(evt.Key); } } } }