public async Task UserProvidedBufferNumAndSize()
        {
            TestHelper.BeginTest("UserProvidedBufferNumAndSize");
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests");

            MMALCameraConfig.UserBufferNum  = 10;
            MMALCameraConfig.UserBufferSize = 20000;

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "jpg"))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder())
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.I420, quality: 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        Fixture.MMALCamera.Camera.StillPort
                        .ConnectTo(imgEncoder);
                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        // Camera warm up time
                        await Task.Delay(2000);

                        await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);

                        Fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                    }
        }
Beispiel #2
0
        /// <summary>
        /// Self-contained method for capturing a continual images from the camera still port for a specified period of time.
        /// An MMALImageEncoder component will be created and attached to the still port.
        /// </summary>
        /// <param name="handler">The image capture handler to apply to the encoder component.</param>
        /// <param name="encodingType">The image encoding type e.g. JPEG, BMP.</param>
        /// <param name="pixelFormat">The pixel format to use with the encoder e.g. I420 (YUV420).</param>
        /// <param name="cancellationToken">A cancellationToken to trigger stop capturing.</param>
        /// <param name="burstMode">When enabled, burst mode will increase the rate at which images are taken, at the expense of quality.</param>
        /// <returns>The awaitable Task.</returns>
        public async Task TakePictureTimeout(ImageStreamCaptureHandler handler, MMALEncoding encodingType, MMALEncoding pixelFormat, CancellationToken cancellationToken, bool burstMode = false)
        {
            if (burstMode)
            {
                this.Camera.StillPort.SetParameter(MMALParametersCamera.MMAL_PARAMETER_CAMERA_BURST_CAPTURE, true);
            }

            using (var imgEncoder = new MMALImageEncoder(handler))
                using (var renderer = new MMALNullSinkComponent())
                {
                    this.ConfigureCameraSettings();

                    imgEncoder.ConfigureOutputPort(encodingType, pixelFormat, 90);

                    // Create our component pipeline.
                    this.Camera.StillPort.ConnectTo(imgEncoder);
                    this.Camera.PreviewPort.ConnectTo(renderer);

                    // Camera warm up time
                    await Task.Delay(2000);

                    while (!cancellationToken.IsCancellationRequested)
                    {
                        await this.ProcessAsync(this.Camera.StillPort);
                    }
                }
        }
        public async Task TakePictureWithCustomConnectionCallbackHandler()
        {
            TestHelper.BeginTest("TakePictureWithCustomConnectionCallbackHandler");
            TestHelper.SetConfigurationDefaults();

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "jpg"))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder())
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.I420, quality: 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        var connection = Fixture.MMALCamera.Camera.StillPort
                                         .ConnectTo(imgEncoder, 0, true);

                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        // Register our custom connection callback handler.
                        connection.RegisterCallbackHandler(new CustomConnectionCallbackHandler(connection));

                        // Camera warm up time
                        await Task.Delay(2000);

                        await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);

                        Fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                    }
        }
        public async Task TakePictureRawBayer(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            TestHelper.BeginTest("TakePictureRawBayer", encodingType.EncodingName, pixelFormat.EncodingName);
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests");

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", extension))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder(true))
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(encodingType, pixelFormat, quality: 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        Fixture.MMALCamera.Camera.StillPort
                        .ConnectTo(imgEncoder);
                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        // Camera warm up time
                        await Task.Delay(2000);

                        await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);

                        Fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                    }
        }
        public async Task TakePictureTimeout()
        {
            TestHelper.BeginTest("TakePictureTimeout");
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests/split_tests");

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests/split_tests", "jpg"))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder())
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.I420, quality: 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        Fixture.MMALCamera.Camera.StillPort
                        .ConnectTo(imgEncoder);
                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        // Camera warm up time
                        await Task.Delay(2000);

                        var timeout = DateTime.Now.AddSeconds(10);
                        while (DateTime.Now.CompareTo(timeout) < 0)
                        {
                            await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);
                        }

                        Fixture.CheckAndAssertDirectory(imgCaptureHandler.Directory);
                    }
        }
        public void TakePictureRawBayer(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            AsyncContext.Run(async() =>
            {
                var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", extension);

                TestHelper.CleanDirectory("/home/pi/images/tests");

                using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler, true))
                {
                    imgEncoder.ConfigureOutputPort(0, encodingType, pixelFormat, 90);

                    //Create our component pipeline.
                    fixture.MMALCamera.Camera.StillPort
                    .ConnectTo(imgEncoder);
                    fixture.MMALCamera.Camera.PreviewPort
                    .ConnectTo(new MMALNullSinkComponent());

                    fixture.MMALCamera.ConfigureCameraSettings();

                    await fixture.MMALCamera.BeginProcessing(fixture.MMALCamera.Camera.StillPort, imgEncoder);
                }

                if (System.IO.File.Exists(imgCaptureHandler.GetFilepath()))
                {
                    var length = new System.IO.FileInfo(imgCaptureHandler.GetFilepath()).Length;
                    Assert.True(length > 0);
                }
                else
                {
                    Assert.True(false, $"File {imgCaptureHandler.GetFilepath()} was not created");
                }
            });
        }
        /// <summary>
        /// Self-contained method for capturing timelapse images.
        /// An MMALImageEncoder component will be created and attached to the still port.
        /// </summary>
        /// <param name="handler">The image capture handler to apply to the encoder component</param>
        /// <param name="encodingType">The image encoding type e.g. JPEG, BMP</param>
        /// <param name="pixelFormat">The pixel format to use with the encoder e.g. I420 (YUV420)</param>
        /// <param name="timelapse">A Timelapse object which specifies the timeout and rate at which images should be taken</param>
        /// <returns>The awaitable Task</returns>
        public async Task TakePictureTimelapse(ImageStreamCaptureHandler handler, MMALEncoding encodingType, MMALEncoding pixelFormat, Timelapse timelapse)
        {
            int interval = 0;

            if (timelapse == null)
            {
                throw new PiCameraError("Timelapse object null. This must be initialized for Timelapse mode");
            }

            while (DateTime.Now.CompareTo(timelapse.Timeout) < 0)
            {
                switch (timelapse.Mode)
                {
                case TimelapseMode.Millisecond:
                    interval = timelapse.Value;
                    break;

                case TimelapseMode.Second:
                    interval = timelapse.Value * 1000;
                    break;

                case TimelapseMode.Minute:
                    interval = (timelapse.Value * 60) * 1000;
                    break;
                }

                await Task.Delay(interval);

                await TakePicture(handler, encodingType, pixelFormat);
            }
        }
Beispiel #8
0
        private void TakePicture(object sender, EventArgs e)
        {
            if (ReloadConfig)
            {
                this.MMALCamera.ConfigureCameraSettings();
                ConfigForm.ReloadConfig = false;
            }

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                    using (var renderer = new MMALVideoRenderer())
                    {
                        this.MMALCamera.ConfigureCameraSettings();

                        // Create our component pipeline.
                        imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                        this.MMALCamera.Camera.StillPort.ConnectTo(imgEncoder);
                        this.MMALCamera.Camera.PreviewPort.ConnectTo(renderer);

                        Task.Factory.Run(async() =>
                        {
                            // Camera warm up time
                            await Task.Delay(5000);
                            await this.MMALCamera.BeginProcessing(this.MMALCamera.Camera.StillPort);
                        });
                    }
        }
Beispiel #9
0
        public async Task TakePicturesFromVideoPortWithCustomFilename(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            TestHelper.BeginTest("TakePicturesFromVideoPortWithCustomFilename", encodingType.EncodingName, pixelFormat.EncodingName);
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests");

            using (var imgCaptureHandler = new ImageStreamCaptureHandler($"/home/pi/images/tests/fromVideoPort.{extension}"))
                using (var splitter = new MMALSplitterComponent())
                    using (var preview = new MMALNullSinkComponent())
                        using (var imgEncoder = new MMALImageEncoder(continuousCapture: true))
                        {
                            Fixture.MMALCamera.ConfigureCameraSettings();

                            var portConfig = new MMALPortConfig(encodingType, pixelFormat, 90);

                            imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                            // Create our component pipeline.
                            Fixture.MMALCamera.Camera.VideoPort
                            .ConnectTo(splitter);
                            splitter.Outputs[0].ConnectTo(imgEncoder);
                            Fixture.MMALCamera.Camera.PreviewPort
                            .ConnectTo(preview);

                            // Camera warm up time
                            await Task.Delay(2000);

                            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));

                            await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.VideoPort, cts.Token);

                            Fixture.CheckAndAssertDirectory(imgCaptureHandler.Directory);
                        }
        }
Beispiel #10
0
        public void ImagesToVideo()
        {
            TestHelper.BeginTest("ImagesToVideo");
            TestHelper.SetConfigurationDefaults();

            AsyncContext.Run(async() =>
            {
                TestHelper.CleanDirectory("/home/pi/videos/tests");
                TestHelper.CleanDirectory("/home/pi/images/tests");

                // This example will take an image every 5 seconds for 1 minute.
                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "jpg"))
                {
                    var cts = new CancellationTokenSource(TimeSpan.FromMinutes(1));

                    var tl = new Timelapse {
                        Mode = TimelapseMode.Second, CancellationToken = cts.Token, Value = 5
                    };
                    await _fixture.MMALCamera.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, tl);

                    // Process all images captured into a video at 2fps.
                    imgCaptureHandler.ImagesToVideo("/home/pi/videos/tests", 2);

                    _fixture.CheckAndAssertFilepath("/home/pi/videos/tests/out.avi");
                }
            });
        }
        public async Task EdgeDetectionKernelProcessor(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            TestHelper.BeginTest("EdgeDetectionKernelProcessor", encodingType.EncodingName, pixelFormat.EncodingName);
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests");

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", extension))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder())
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(encodingType, pixelFormat, 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        Fixture.MMALCamera.Camera.StillPort
                        .ConnectTo(imgEncoder);
                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        imgCaptureHandler.Manipulate(context =>
                        {
                            context.Apply(new EdgeDetection(EDStrength.High));
                        }, ImageFormat.Jpeg);

                        // Camera warm up time
                        await Task.Delay(2000);

                        await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);

                        Fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                    }
        }
Beispiel #12
0
        private async Task ResizePicture(string extension, MMALEncoding encoding, MMALEncoding pixelFormat, int width, int height)
        {
            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", extension))
                using (var resizer = new MMALResizerComponent())
                    using (var imgEncoder = new MMALImageEncoder())
                        using (var nullSink = new MMALNullSinkComponent())
                        {
                            this.Cam.ConfigureCameraSettings();

                            await Task.Delay(2000);

                            var resizerConfig = new MMALPortConfig(pixelFormat, pixelFormat, width: width, height: height);
                            var encoderConfig = new MMALPortConfig(encoding, pixelFormat, quality: 90);

                            // Create our component pipeline.
                            resizer.ConfigureInputPort(new MMALPortConfig(MMALCameraConfig.Encoding, MMALCameraConfig.EncodingSubFormat), this.Cam.Camera.StillPort, null);
                            resizer.ConfigureOutputPort(resizerConfig, null);
                            imgEncoder.ConfigureOutputPort(encoderConfig, imgCaptureHandler);

                            this.Cam.Camera.StillPort.ConnectTo(resizer);
                            resizer.Outputs[0].ConnectTo(imgEncoder);
                            this.Cam.Camera.PreviewPort.ConnectTo(nullSink);

                            await this.Cam.ProcessAsync(this.Cam.Camera.StillPort);
                        }
        }
        public void TakePictureTimeout(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            AsyncContext.Run(async() =>
            {
                var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests/split_tests", extension);

                TestHelper.CleanDirectory("/home/pi/images/tests/split_tests");

                using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                {
                    fixture.MMALCamera.ConfigureCameraSettings();

                    imgEncoder.ConfigureOutputPort(0, encodingType, pixelFormat, 90);

                    //Create our component pipeline.
                    fixture.MMALCamera.Camera.StillPort
                    .ConnectTo(imgEncoder);
                    fixture.MMALCamera.Camera.PreviewPort
                    .ConnectTo(new MMALNullSinkComponent());

                    var timeout = DateTime.Now.AddSeconds(30);

                    //Camera warm up time
                    await Task.Delay(2000);

                    while (DateTime.Now.CompareTo(timeout) < 0)
                    {
                        await fixture.MMALCamera.BeginProcessing(fixture.MMALCamera.Camera.StillPort);
                    }
                }
            });
        }
        public void TakePictureTimeout()
        {
            TestHelper.BeginTest("TakePictureTimeout");
            TestHelper.SetConfigurationDefaults();

            AsyncContext.Run(async() =>
            {
                TestHelper.CleanDirectory("/home/pi/images/tests/split_tests");

                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests/split_tests", "jpg"))
                    using (var preview = new MMALNullSinkComponent())
                        using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        {
                            _fixture.MMALCamera.ConfigureCameraSettings();

                            imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                            // Create our component pipeline.
                            _fixture.MMALCamera.Camera.StillPort
                            .ConnectTo(imgEncoder);
                            _fixture.MMALCamera.Camera.PreviewPort
                            .ConnectTo(preview);

                            // Camera warm up time
                            await Task.Delay(2000);

                            var timeout = DateTime.Now.AddSeconds(10);
                            while (DateTime.Now.CompareTo(timeout) < 0)
                            {
                                await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.StillPort);
                            }
                        }
            });
        }
        public void TakePictureRawSensor(string extension, MMALEncoding encodingType, MMALEncoding pixelFormat)
        {
            TestHelper.BeginTest("TakePictureRawSensor", encodingType.EncodingName, pixelFormat.EncodingName);
            TestHelper.SetConfigurationDefaults();

            AsyncContext.Run(async() =>
            {
                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", extension))
                {
                    TestHelper.CleanDirectory("/home/pi/images/tests");

                    await _fixture.MMALCamera.TakeRawPicture(imgCaptureHandler);

                    var encodings = _fixture.MMALCamera.Camera.StillPort.GetSupportedEncodings();

                    if (System.IO.File.Exists(imgCaptureHandler.GetFilepath()))
                    {
                        var length = new System.IO.FileInfo(imgCaptureHandler.GetFilepath()).Length;

                        if (encodings.Contains(encodingType.EncodingVal))
                        {
                            Assert.True(length > 1);
                        }
                    }
                    else
                    {
                        Assert.True(false, $"File {imgCaptureHandler.GetFilepath()} was not created");
                    }
                }
            });
        }
Beispiel #16
0
        /// <summary>
        /// Takes a picture and saves that in the default location
        /// </summary>
        /// <returns>The filename of the picture,
        /// without path or extension</returns>
        public async Task <string> TakePicture()
        {
            // Instantiate the camera, if needed
            // In my demos I always have the cam upside down. Fix that (not needed
            // for analyzing, just for the user...)
            if (_cameraDevice == null)
            {
                MMALCameraConfig.Flips = MMAL_PARAM_MIRROR_T.MMAL_PARAM_MIRROR_BOTH;
                _cameraDevice          = MMALCamera.Instance;
            }

            // Create the handler that will receive the data
            using (var imgCaptureHandler = new ImageStreamCaptureHandler(
                       "/home/pi/images/",
                       "jpg"))
            {
                // Call the camera and collect bytes
                await _cameraDevice.TakePicture(
                    imgCaptureHandler,
                    MMALEncoding.JPEG,
                    MMALEncoding.I420);

                // Get the last generated filename
                var fileName = imgCaptureHandler.GetFilename();

                return(fileName);
            }
        }
Beispiel #17
0
 private async Task TakeRawSensor(string extension)
 {
     using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", extension))
     {
         await this.Cam.TakeRawPicture(imgCaptureHandler);
     }
 }
Beispiel #18
0
        public async Task JpegThumbnail()
        {
            TestHelper.BeginTest("Image - JpegThumbnail");
            TestHelper.SetConfigurationDefaults();
            TestHelper.CleanDirectory("/home/pi/images/tests");

            JpegThumbnail tm = new JpegThumbnail(true, 200, 200, 90);

            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "jpg"))
                using (var preview = new MMALNullSinkComponent())
                    using (var imgEncoder = new MMALImageEncoder(thumbnailConfig: tm))
                    {
                        Fixture.MMALCamera.ConfigureCameraSettings();

                        var portConfig = new MMALPortConfig(MMALEncoding.JPEG, MMALEncoding.I420, 90);

                        imgEncoder.ConfigureOutputPort(portConfig, imgCaptureHandler);

                        // Create our component pipeline.
                        Fixture.MMALCamera.Camera.StillPort
                        .ConnectTo(imgEncoder);
                        Fixture.MMALCamera.Camera.PreviewPort
                        .ConnectTo(preview);

                        imgCaptureHandler.Manipulate(context =>
                        {
                            context.StripBayerMetadata(CameraVersion.OV5647);
                        }, ImageFormat.Jpeg);

                        // Camera warm up time
                        await Task.Delay(2000);

                        await Fixture.MMALCamera.ProcessAsync(Fixture.MMALCamera.Camera.StillPort);
                    }
        }
Beispiel #19
0
        public static void TakePictureManualMode()
        {
            MMALCamera cam = MMALCamera.Instance;

            AsyncContext.Run(async() =>
            {
                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                    using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        using (var nullSink = new MMALNullSinkComponent())
                        {
                            cam.ConfigureCameraSettings();

                            // Create our component pipeline.
                            imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                            cam.Camera.StillPort.ConnectTo(imgEncoder);
                            cam.Camera.PreviewPort.ConnectTo(nullSink);

                            // Camera warm up time
                            await Task.Delay(2000);
                            await cam.ProcessAsync(cam.Camera.StillPort);
                        }
            });

            cam.Cleanup();
        }
        public void TakePictureTimelapse()
        {
            TestHelper.BeginTest("TakePictureTimelapse");
            TestHelper.SetConfigurationDefaults();

            AsyncContext.Run(async() =>
            {
                TestHelper.CleanDirectory("/home/pi/images/tests/split_tests");

                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests/split_tests", "jpg"))
                    using (var preview = new MMALNullSinkComponent())
                        using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        {
                            _fixture.MMALCamera.ConfigureCameraSettings();

                            imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                            // Create our component pipeline.
                            _fixture.MMALCamera.Camera.StillPort
                            .ConnectTo(imgEncoder);
                            _fixture.MMALCamera.Camera.PreviewPort
                            .ConnectTo(preview);

                            CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
                            Timelapse tl = new Timelapse
                            {
                                Mode = TimelapseMode.Second,
                                CancellationToken = cts.Token,
                                Value             = 5
                            };

                            // Camera warm up time
                            await Task.Delay(2000);

                            while (!tl.CancellationToken.IsCancellationRequested)
                            {
                                int interval = tl.Value * 1000;

                                await Task.Delay(interval);

                                await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.StillPort);
                            }

                            DirectoryInfo info = new DirectoryInfo(imgCaptureHandler.Directory);

                            if (info.Exists)
                            {
                                var files = info.EnumerateFiles();

                                Assert.True(files != null && files.Count() == 6);
                            }
                            else
                            {
                                Assert.True(false, $"File {imgCaptureHandler.GetFilepath()} was not created");
                            }
                        }
            });
        }
Beispiel #21
0
        async Task PrepareTimelapseVideoAsync(Task captureImageTask, ImageStreamCaptureHandler imgCaptureHandler, string path)
        {
            try
            {
                Logger.Log($"Prepare timelapse: {captureImageTask.IsCompletedSuccessfully}, {captureImageTask.IsFaulted}");

                if (captureImageTask.IsCompletedSuccessfully)
                {
                    Logger.Log($"Will create timelapse {currentTimelapseId} video from {path}");

                    if (imgCaptureHandler.ProcessedFiles.Count == 0)
                    {
                        return;
                    }

                    var process = new Process
                    {
                        StartInfo =
                        {
                            UseShellExecute = false,
                            CreateNoWindow  = true,
                            FileName        = "ffmpeg"
                        }
                    };

                    var extension = imgCaptureHandler.ProcessedFiles.First().Extension;

                    var targetDirectory     = Path.Combine(path, "out");
                    var targetFilePath      = Path.Combine(targetDirectory, string.Concat(currentTimelapseId, ".avi"));
                    var targetDirectoryInfo = Directory.CreateDirectory(targetDirectory);

                    var fps  = 2;
                    var args = $"-framerate {fps} -f image2 -pattern_type glob -y -i {path + "/*." + extension} {targetFilePath}";
                    Logger.Log($"Starting ffmpeg with args: {args}");
                    process.StartInfo.Arguments = args;
                    process.Start();
                    process.WaitForExit();

                    Logger.Log($"Timelapse video for {currentTimelapseId} created");

                    await UploadFileAsync("timelapse", targetFilePath);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Failed to create timelapse video");
            }
            finally
            {
                currentTimelapseId = null;
                imgCaptureHandler?.Dispose();
                timelapseCts?.Dispose();
                timelapseCts = null;

                cameraInUse.Release();
            }
        }
        /// <summary>
        /// Self-contained method for capturing a continual images from the camera still port for a specified period of time.
        /// An MMALImageEncoder component will be created and attached to the still port.
        /// </summary>
        /// <param name="handler">The image capture handler to apply to the encoder component</param>
        /// <param name="encodingType">The image encoding type e.g. JPEG, BMP</param>
        /// <param name="pixelFormat">The pixel format to use with the encoder e.g. I420 (YUV420)</param>
        /// <param name="timeout">The DateTime which capturing should stop</param>
        /// <param name="burstMode">When enabled, burst mode will increase the rate at which images are taken, at the expense of quality</param>
        /// <returns>The awaitable Task</returns>
        public async Task TakePictureTimeout(ImageStreamCaptureHandler handler, MMALEncoding encodingType, MMALEncoding pixelFormat, DateTime timeout, bool burstMode = false)
        {
            if (burstMode)
            {
                this.Camera.StillPort.SetParameter(MMALParametersCamera.MMAL_PARAMETER_CAMERA_BURST_CAPTURE, true);
            }

            while (DateTime.Now.CompareTo(timeout) < 0)
            {
                await TakePicture(handler, encodingType, pixelFormat);
            }
        }
Beispiel #23
0
        static void Main(string[] args)
        {
            MMALCamera cam = MMALCamera.Instance;

            // Create observable that will generate an incrementing number every second
            var observable = Observable.Generate(1, x => true, x => x + 1, x => x, x => TimeSpan.FromSeconds(1));

            var relay  = OutputPort.Create(17, OutputPort.InitialValue.Low).Result;
            var light1 = OutputPort.Create(27, OutputPort.InitialValue.Low).Result;
            var light2 = OutputPort.Create(22, OutputPort.InitialValue.Low).Result;
            var button = InputPort.Create(24, GpioEdge.Both).Result;

            // Write true whenever the number is even and odd when the number is odd
            using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                using (observable.Select(x => x % 2 == 0).Subscribe(relay))
                    using (observable.Select(x => x % 2 == 0).Subscribe(light1))
                        //using (observable.Select(x => x % 2 != 0).Subscribe(light2))
                        //using (button.Do(pressed => Console.WriteLine(pressed)).Subscribe())
                        using (button.Subscribe(light2))
                            using (var i2cBus = new I2CBusPI("/dev/i2c-1"))
                            {
                                var takePictureTask = cam.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                                var i2cDevice = new I2CDevicePI(i2cBus, Display.DefaultI2CAddress);

                                var sensor = new BME280Sensor(i2cBus, 1014);

                                var display = new SSD1306.Display(i2cDevice, 128, 64);
                                display.Init();

                                var dfont = new AdafruitSinglePageFont();

                                for (int i = 0; i < 100; i++)
                                {
                                    display.WriteLineBuff(dfont, $"Temperature: {sensor.ReadTemperature().Result} °C", $"Pressure: {sensor.ReadPressure().Result} Pa", $"Humidity: {sensor.ReadHumidity().Result} %", $"Altitude: {sensor.ReadAltitude().Result} m", "Line 5", "Line 6", "Line 7", "Line 8");
                                    display.DisplayUpdate();
                                }

                                //for (int i = 0; i < 100; i++)
                                //    display.DrawPixel(i, i);

                                takePictureTask.Wait();
                                display.ClearDisplay();
                            }
            // releasing relay
            relay.Write(true);
            // turning of light
            light1.Write(false);
            light2.Write(false);
            // Cleanup disposes all unmanaged resources and unloads Broadcom library. To be called when no more processing is to be done
            // on the camera.
            cam.Cleanup();
        }
Beispiel #24
0
        static async Task jpg()
        {
            var cam      = GetConfiguredCamera();
            var pathname = ramdiskPath + "snapshot.jpg";

            using var handler = new ImageStreamCaptureHandler(pathname);
            Console.WriteLine($"Capturing JPG: {pathname}");
            await cam.TakePicture(handler, MMALEncoding.JPEG, MMALEncoding.I420).ConfigureAwait(false);

            cam.Cleanup();
            Console.WriteLine("Exiting.");
        }
Beispiel #25
0
        private async Task <string> CapturePictureAndGetFileName()
        {
            string fileName = null;

            using (var imgCaptureHandler = new ImageStreamCaptureHandler(picStoragePath, picExtension))
            {
                await MMALCamera.TakePicture(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420);

                fileName = imgCaptureHandler.GetFilename();
            }
            return(fileName);
        }
Beispiel #26
0
        private void ConfigureButton()
        {
            _buttonPin.PinMode       = GpioPinDriveMode.Input;
            _button                  = new Unosquare.RaspberryIO.Peripherals.Button(_buttonPin);
            _buttonPin.InputPullMode = GpioPinResistorPullMode.PullUp;

            MMALLog.Logger.Debug($"Input button configured");

            _button.Released += (s, e) =>
            {
                MMALLog.Logger.Debug($"Button released");
            };

            _button.Pressed += (s, e) =>
            {
                MMALLog.Logger.Debug($"Button pressed");

                if (Processing)
                {
                    return;
                }

                Processing = true;

                if (ReloadConfig)
                {
                    this.MMALCamera.ConfigureCameraSettings();
                    ConfigForm.ReloadConfig = false;
                }

                AsyncContext.Run(async() =>
                {
                    using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/", "jpg"))
                        using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                            using (var renderer = new MMALVideoRenderer())
                            {
                                this.MMALCamera.ConfigureCameraSettings();

                                // Create our component pipeline.
                                imgEncoder.ConfigureOutputPort(0, MMALEncoding.JPEG, MMALEncoding.I420, 90);

                                this.MMALCamera.Camera.StillPort.ConnectTo(imgEncoder);
                                this.MMALCamera.Camera.PreviewPort.ConnectTo(renderer);

                                await Task.Delay(2000);
                                await this.MMALCamera.BeginProcessing(this.MMALCamera.Camera.StillPort);
                                Processing = false;
                            }
                });
            };
        }
Beispiel #27
0
        /// <summary>
        /// Useful for Timelapse captures. Enables you to convert a list of images associated with an ImageStreamCaptureHandler to a video.
        /// </summary>
        /// <param name="result">The list of images we wish to process.</param>
        /// <param name="targetDirectory">The target directory we want to save the video to.</param>
        /// <param name="fps">The framerate in fps to set as -framerate parameter for FFmpeg.</param>
        public static void ImagesToVideo(this ImageStreamCaptureHandler result, string targetDirectory, int fps)
        {
            var process = new Process
            {
                StartInfo =
                {
                    UseShellExecute = false,
                    CreateNoWindow  = true,
                    FileName        = "ffmpeg"
                }
            };

            if (result.ProcessedFiles.Count == 0)
            {
                return;
            }

            // Create temporary directory and copy all files in the capture handler to it.
            var tempDirectory = result.ProcessedFiles.FirstOrDefault().Directory.TrimEnd('/') + "/mmalsharptemp/";
            var extension     = result.ProcessedFiles.FirstOrDefault().Extension;

            try
            {
                System.IO.Directory.CreateDirectory(tempDirectory);

                foreach (var tuple in result.ProcessedFiles)
                {
                    System.IO.File.Copy($"{tuple.Directory.TrimEnd('/')}/{tuple.Filename.TrimEnd('.')}.{tuple.Extension}", $"{tempDirectory}{tuple.Filename.TrimEnd('.')}.{tuple.Extension}");
                }

                targetDirectory.TrimEnd('/');

                if (fps == 0)
                {
                    // Default to 25fps - FFmpeg defaults to this value if nothing is specified
                    fps = 25;
                }

                process.StartInfo.Arguments = $"-framerate {fps} -f image2 -pattern_type glob -y -i {tempDirectory + "'*." + extension + "'"} {targetDirectory}/out.avi";
                process.Start();
                process.WaitForExit();
            }
            finally
            {
                // Make sure we try to cleanup even if error occurs.
                if (System.IO.Directory.Exists(tempDirectory))
                {
                    System.IO.Directory.Delete(tempDirectory, true);
                }
            }
        }
Beispiel #28
0
        public async Task <TakeTimelapseResponse> StartTimelapseAsync(TakeTimelapseRequest req)
        {
            if (IsTakingTimelapse())
            {
                return(new TakeTimelapseResponse()
                {
                    ErrorMessage = $"Timelapse {currentTimelapseId?.ToString()} is being taken",
                });
            }

            var cameraUsed = false;

            try
            {
                var path = EnsureLocalDirectoryExists();
                currentTimelapseId = Guid.NewGuid().ToString();
                var pathForImages = Path.Combine(path, currentTimelapseId);

                await cameraInUse.WaitAsync();

                cameraUsed = true;

                // This example will take an image every 10 seconds for 4 hours
                var imgCaptureHandler = new ImageStreamCaptureHandler(pathForImages, "jpg");
                timelapseCts = new CancellationTokenSource(TimeSpan.FromSeconds(req.Duration));
                var tl = new Timelapse {
                    Mode = TimelapseMode.Second, CancellationToken = timelapseCts.Token, Value = req.Interval
                };

                Logger.Log($"Starting timelapse {currentTimelapseId}");
                _ = camera.TakePictureTimelapse(imgCaptureHandler, MMALEncoding.JPEG, MMALEncoding.I420, tl)
                    .ContinueWith(async(t) => await PrepareTimelapseVideoAsync(t, imgCaptureHandler, pathForImages));

                return(new TakeTimelapseResponse()
                {
                    Id = currentTimelapseId,
                    Duration = req.Duration,
                    Interval = req.Interval,
                });
            }
            catch
            {
                if (cameraUsed)
                {
                    cameraInUse.Release();
                }

                throw;
            }
        }
Beispiel #29
0
        /// <summary>
        /// Self-contained method for capturing timelapse images.
        /// An MMALImageEncoder component will be created and attached to the still port.
        /// </summary>
        /// <param name="handler">The image capture handler to apply to the encoder component.</param>
        /// <param name="encodingType">The image encoding type e.g. JPEG, BMP.</param>
        /// <param name="pixelFormat">The pixel format to use with the encoder e.g. I420 (YUV420).</param>
        /// <param name="timelapse">A Timelapse object which specifies the timeout and rate at which images should be taken.</param>
        /// <returns>The awaitable Task.</returns>
        /// <exception cref="ArgumentNullException"/>
        public async Task TakePictureTimelapse(ImageStreamCaptureHandler handler, MMALEncoding encodingType, MMALEncoding pixelFormat, Timelapse timelapse)
        {
            int interval = 0;

            if (timelapse == null)
            {
                throw new ArgumentNullException(nameof(timelapse), "Timelapse object null. This must be initialized for Timelapse mode");
            }

            using (var imgEncoder = new MMALImageEncoder(handler))
                using (var renderer = new MMALNullSinkComponent())
                {
                    this.ConfigureCameraSettings();

                    imgEncoder.ConfigureOutputPort(encodingType, pixelFormat, 90);

                    // Create our component pipeline.
                    this.Camera.StillPort.ConnectTo(imgEncoder);
                    this.Camera.PreviewPort.ConnectTo(renderer);

                    // Camera warm up time
                    await Task.Delay(2000);

                    while (!timelapse.CancellationToken.IsCancellationRequested)
                    {
                        switch (timelapse.Mode)
                        {
                        case TimelapseMode.Millisecond:
                            interval = timelapse.Value;
                            break;

                        case TimelapseMode.Second:
                            interval = timelapse.Value * 1000;
                            break;

                        case TimelapseMode.Minute:
                            interval = (timelapse.Value * 60) * 1000;
                            break;
                        }

                        await Task.Delay(interval);

                        MMALLog.Logger.Info($"Preparing to take picture. Resolution: {imgEncoder.Width} x {imgEncoder.Height}. " +
                                            $"Encoder: {encodingType.EncodingName}. Pixel Format: {pixelFormat.EncodingName}.");

                        await this.ProcessAsync(this.Camera.StillPort);
                    }
                }
        }
        public void ChangeEncodingType()
        {
            TestHelper.BeginTest("Image - ChangeEncodingType");
            TestHelper.SetConfigurationDefaults();

            AsyncContext.Run(async() =>
            {
                TestHelper.CleanDirectory("/home/pi/images/tests");

                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "jpg"))
                    using (var preview = new MMALNullSinkComponent())
                        using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        {
                            _fixture.MMALCamera.ConfigureCameraSettings();

                            imgEncoder.ConfigureOutputPort(MMALEncoding.JPEG, MMALEncoding.I420, 90);

                            // Create our component pipeline.
                            _fixture.MMALCamera.Camera.StillPort
                            .ConnectTo(imgEncoder);
                            _fixture.MMALCamera.Camera.PreviewPort
                            .ConnectTo(preview);

                            // Camera warm up time
                            await Task.Delay(2000);

                            await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.StillPort);

                            _fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                        }

                using (var imgCaptureHandler = new ImageStreamCaptureHandler("/home/pi/images/tests", "bmp"))
                    using (var preview = new MMALNullSinkComponent())
                        using (var imgEncoder = new MMALImageEncoder(imgCaptureHandler))
                        {
                            imgEncoder.ConfigureOutputPort(MMALEncoding.BMP, MMALEncoding.I420, 90);

                            // Create our component pipeline.
                            _fixture.MMALCamera.Camera.StillPort
                            .ConnectTo(imgEncoder);
                            _fixture.MMALCamera.Camera.PreviewPort
                            .ConnectTo(preview);

                            await _fixture.MMALCamera.ProcessAsync(_fixture.MMALCamera.Camera.StillPort);

                            _fixture.CheckAndAssertFilepath(imgCaptureHandler.GetFilepath());
                        }
            });
        }