From c1b88cbe06a6c0773c3b2f1b151fc29aacebec23 Mon Sep 17 00:00:00 2001 From: dram55 Date: Sun, 11 Aug 2019 16:51:18 -0400 Subject: [PATCH] Use Read() for VideoProcessor NO_DEVICE [ci-skip] --- MarioMaker2OCR/VideoProcessor.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/MarioMaker2OCR/VideoProcessor.cs b/MarioMaker2OCR/VideoProcessor.cs index 173028e..1e7bca7 100644 --- a/MarioMaker2OCR/VideoProcessor.cs +++ b/MarioMaker2OCR/VideoProcessor.cs @@ -28,6 +28,8 @@ class VideoProcessor : IDisposable private Image[] frameBuffer = new Image[16]; // Frame buffer that is passed to events private (bool IsBlack, bool IsClear, bool ShouldStop) flags = (false, false, false); // Contains flags indicating the current status of the video processor + private const int FRAME_BUFFER_INTERVAL = 250; // put frame in buffer every 250ms + /// /// Essentially copied from https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose /// @@ -81,7 +83,7 @@ public void Start(bool blocking=false) if(deviceId != NO_DEVICE) { frameBufferTimer = new System.Threading.Timer(frameBuffer_tick); - frameBufferTimer.Change(0, 250); + frameBufferTimer.Change(0, FRAME_BUFFER_INTERVAL); } if(blocking) @@ -219,18 +221,22 @@ public void processingLoop() flags.IsBlack = false; flags.IsClear = false; bool WaitForClearStats = false; - while (true) { - cap.Retrieve(currentFrame); if(deviceId == NO_DEVICE) { + cap.Read(currentFrame); + if (currentFrame.IsEmpty) return; + data = currentFrame.GetRawData(); double fps = cap.GetCaptureProperty(CapProp.Fps); double i = cap.GetCaptureProperty(CapProp.PosFrames); - cap.SetCaptureProperty(CapProp.PosFrames, i + 1); - if (i == cap.GetCaptureProperty(CapProp.FrameCount)-1) return; - if (i % (int)Math.Floor(fps / 4) == 0) frameBuffer_tick(); + if (i % (int)Math.Floor(fps / (1000/FRAME_BUFFER_INTERVAL)) == 0) frameBuffer_tick(); + } + else + { + cap.Retrieve(currentFrame); } + Dictionary hues = getHues(data, frameSize, skip); if (flags.IsBlack)