Ejemplo n.º 1
0
        public async Task ProvideDataForObservers()
        {
            var inputImage     = new Image <Rgb, byte>(1, 1);
            var expectedImages = new List <Image <Rgb, byte> >();
            var i = 0;

            strategy.Setup(s => s.Denoise(inputImage)).Returns(() =>
            {
                var value = new Image <Rgb, byte>(++i, 1);
                expectedImages.Add(value);
                return(value);
            });

            var denoiser = new DenoisingStream(strategy.Object, new Counter(), CancellationToken.None, logger.Object);
            var observer = new TestImageObserver <Rgb>();

            using (denoiser.Subscribe(observer))
            {
                denoiser.Process(inputImage);
                await Task.Delay(TimeSpan.FromMilliseconds(100));

                denoiser.Process(inputImage);
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.Equal(2, observer.ReceivedItems.Count);
            Assert.Equal(expectedImages[0], observer.ReceivedItems.Keys.First());
            Assert.Equal(expectedImages[1], observer.ReceivedItems.Keys.Skip(1).First());
        }
Ejemplo n.º 2
0
        public async Task DropNewFramesIfPreviousOneIsStillInProgress()
        {
            var inputImage    = new Image <Rgb, byte>(1, 1);
            var expectedImage = new Image <Rgb, byte>(10, 10);

            strategy.Setup(s => s.Denoise(inputImage)).Returns(() => {
                Task.Delay(TimeSpan.FromMilliseconds(900)).Wait();
                return(expectedImage);
            });

            var denoiser = new DenoisingStream(strategy.Object, new Counter(), CancellationToken.None, logger.Object);
            var observer = new TestImageObserver <Rgb>();

            using (denoiser.Subscribe(observer))
            {
                var _ = Task.Run(() => denoiser.Process(inputImage));
                _ = Task.Run(() => denoiser.Process(inputImage));
                _ = Task.Run(() => denoiser.Process(inputImage));
                _ = Task.Run(() => denoiser.Process(inputImage));

                await Task.Delay(TimeSpan.FromMilliseconds(1300));
            }

            Assert.Single(observer.ReceivedItems);
        }
Ejemplo n.º 3
0
        public async Task CompletesStreamOnError()
        {
            videoMock.Setup(v => v.QueryFrame()).Throws <TestException>();
            var observer = new TestImageObserver <Rgb>();

            using (new CameraStream(videoMock.Object, CancellationToken.None, loggerMock.Object,
                                    TimeSpan.FromMilliseconds(10)).Subscribe(observer))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 4
0
        public async Task CompleteStreamOnReset()
        {
            var observer = new TestImageObserver <TOut>();

            using (stage.Output.Subscribe(observer))
            {
                await Task.Run(() => stage.Reset());

                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 5
0
        public async Task CompleteStreamWhenRequested()
        {
            var denoiser = new DenoisingStream(strategy.Object, new Counter(), CancellationToken.None, logger.Object);
            var observer = new TestImageObserver <Rgb>();

            using (denoiser.Subscribe(observer))
            {
                denoiser.Complete();
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 6
0
        public async Task ForwardErrors()
        {
            var observer = new TestImageObserver <TOut>();

            using (stage.Output.Subscribe(observer))
            {
                await Task.Run(() => stage.OnError(new TestException()));

                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.IsType <TestException>(observer.Error);
        }
Ejemplo n.º 7
0
        public async Task NotifyObserversAboutErrors()
        {
            videoMock.Setup(v => v.QueryFrame()).Throws <TestException>();
            var observer = new TestImageObserver <Rgb>();

            using (new CameraStream(videoMock.Object, CancellationToken.None, loggerMock.Object,
                                    TimeSpan.FromMilliseconds(10)).Subscribe(observer))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.NotNull(observer.Error);
            Assert.IsType <TestException>(observer.Error);
        }
Ejemplo n.º 8
0
        public async Task NotProvideDataForUnsubscribedObservers()
        {
            videoMock.Setup(v => v.QueryFrame()).Returns(() => new Image <Rgb, byte>(100, 100).Mat);
            var camera = new CameraStream(videoMock.Object, CancellationToken.None, loggerMock.Object,
                                          TimeSpan.FromMilliseconds(10));
            var observer = new TestImageObserver <Rgb>();

            using (camera.Subscribe(observer))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(1000));
            }

            var after = DateTime.Now;

            Assert.True(observer.ReceivedItems.Count > 0);
            Assert.DoesNotContain(observer.ReceivedItems, x => x.Value >= after);
        }
Ejemplo n.º 9
0
        public async Task CompleteStreamIfErrorOccurred()
        {
            var inputImage = new Image <Rgb, byte>(1, 1);

            strategy.Setup(s => s.Denoise(inputImage)).Throws <TestException>();
            var denoiser = new DenoisingStream(strategy.Object, new Counter(), CancellationToken.None, logger.Object);
            var observer = new TestImageObserver <Rgb>();

            using (denoiser.Subscribe(observer))
            {
                denoiser.Process(inputImage);
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.IsType <TestException>(observer.Error);
            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 10
0
        public async Task StopAutomaticallyIfVideoStreamFails()
        {
            var captureMock = new Mock <VideoCapture>(MockBehavior.Default, 0, VideoCapture.API.Any);

            captureMock.Setup(c => c.QueryFrame()).Throws <TestException>();

            var camera   = new Camera(cameraLogger.Object, streamLogger.Object, CaptureSettings.Default, _ => captureMock.Object);
            var observer = new TestImageObserver <Rgb>();

            camera.Start();
            using (camera.Stream.Subscribe(observer))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(500));
            }

            Assert.IsType <TestException>(observer.Error);
            Assert.False(camera.IsRunning);
        }
Ejemplo n.º 11
0
        public async Task DropNewFramesIfSubtractorIsPreviousIsStillInProgress()
        {
            SetupLongRunningPayload(TimeSpan.FromMilliseconds(200));

            var observer = new TestImageObserver <TOut>();

            using (stage.Output.Subscribe(observer))
            {
                var _ = Task.Run(() => stage.OnNext(new Image <TIn, byte>(1, 1)));
                _ = Task.Run(() => stage.OnNext(new Image <TIn, byte>(1, 1)));
                _ = Task.Run(() => stage.OnNext(new Image <TIn, byte>(1, 1)));
                await Task.Delay(TimeSpan.FromMilliseconds(300));
            }

            Assert.Single(observer.ReceivedItems);
            Assert.Equal((ulong)1, stage.Statistics.ProcessedFrames);
            Assert.Equal((ulong)2, stage.Statistics.DroppedFrames);
        }
Ejemplo n.º 12
0
        public async Task NotProvideDataForObserversAfterCompletion()
        {
            var inputImage    = new Image <Rgb, byte>(1, 1);
            var expectedImage = new Image <Rgb, byte>(10, 10);

            strategy.Setup(s => s.Denoise(inputImage)).Returns(expectedImage);

            var denoiser = new DenoisingStream(strategy.Object, new Counter(), CancellationToken.None, logger.Object);
            var observer = new TestImageObserver <Rgb>();

            using (denoiser.Subscribe(observer))
            {
                denoiser.Complete();
                denoiser.Process(inputImage);
                await Task.Delay(TimeSpan.FromMilliseconds(100));
            }

            Assert.Empty(observer.ReceivedItems);
        }
Ejemplo n.º 13
0
        public void CompleteStreamWhenStopped()
        {
            var camera   = new Camera(cameraLogger.Object, streamLogger.Object, CaptureSettings.Default, Camera.CreateVideoCapture);
            var logger   = new PropertyChangedLogger();
            var observer = new TestImageObserver <Rgb>();

            camera.PropertyChanged += logger.OnPropertyChanged;

            camera.Start();
            using (camera.Stream.Subscribe(observer))
            {
                camera.Stop();
            }

            Assert.Null(camera.Stream);
            Assert.Equal(2, logger.Notifications[camera].Count(x => x.Value == nameof(Camera.Stream)));

            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 14
0
        public async Task CompleteTheStreamIfCancellationRequested()
        {
            videoMock.Setup(v => v.QueryFrame()).Returns(() => new Image <Rgb, byte>(100, 100).Mat);

            var cts    = new CancellationTokenSource();
            var camera = new CameraStream(videoMock.Object, cts.Token, loggerMock.Object,
                                          TimeSpan.FromMilliseconds(10));
            var observer = new TestImageObserver <Rgb>();

            camera.Subscribe(observer);

            // ReSharper disable MethodSupportsCancellation
            await Task.Delay(TimeSpan.FromMilliseconds(500));

            cts.Cancel();

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            // ReSharper restore MethodSupportsCancellation

            Assert.True(observer.StreamCompleted);
        }
Ejemplo n.º 15
0
        public async Task NotSendNewFramesIfCancellationRequested()
        {
            videoMock.Setup(v => v.QueryFrame()).Returns(() => new Image <Rgb, byte>(100, 100).Mat);

            var cts    = new CancellationTokenSource();
            var camera = new CameraStream(videoMock.Object, cts.Token, loggerMock.Object,
                                          TimeSpan.FromMilliseconds(10));
            var observer = new TestImageObserver <Rgb>();

            camera.Subscribe(observer);

            // ReSharper disable MethodSupportsCancellation
            await Task.Delay(TimeSpan.FromMilliseconds(500));

            cts.Cancel();
            var after = DateTime.Now;

            await Task.Delay(TimeSpan.FromMilliseconds(500));

            // ReSharper restore MethodSupportsCancellation

            Assert.DoesNotContain(observer.ReceivedItems, x => x.Value > after);
        }
Ejemplo n.º 16
0
        //TODO: redesign blinking test
        public async Task UseProvidedSettingsToSetFrameInterval()
        {
            var settings = new CaptureSettings("Some source", TimeSpan.FromMilliseconds(50));

            var captureMock = new Mock <VideoCapture>(MockBehavior.Default, 0, VideoCapture.API.Any);

            captureMock.Setup(c => c.QueryFrame()).Returns(() => new Image <Rgb, byte>(100, 100).Mat);

            var camera   = new Camera(cameraLogger.Object, streamLogger.Object, CaptureSettings.Default, _ => captureMock.Object);
            var observer = new TestImageObserver <Rgb>();

            camera.Settings = settings;

            camera.Start();
            using (camera.Stream.Subscribe(observer))
            {
                await Task.Delay(TimeSpan.FromMilliseconds(500));

                camera.Stop();
            }

            Assert.True(11 >= observer.ReceivedItems.Count);
            Assert.True(9 <= observer.ReceivedItems.Count);
        }