示例#1
0
        public void Analyze(IImageProxy image)
        {
            var currentTime = DateTime.Now;

            _frameTimestamps.Enqueue(currentTime);
            while (_frameTimestamps.Count >= _frameRateWindow)
            {
                _frameTimestamps.Dequeue();
            }

            var timestampFirst = _frameTimestamps.First();
            var timestampLast  = _frameTimestamps.Dequeue();

            var framePerSecond = 1.0 / ((timestampFirst - timestampLast).TotalMilliseconds) * 1000f;

            var buffer = image.GetPlanes()[0].Buffer;

            var data = buffer.ToByteArray();

            var pixels = data !.Select(b => b & 0xFf)
                         .ToArray();

            var luma = pixels.Average();

            _lumaListener.OnFrameAnalyzed(luma);
            image.Close();
        }
示例#2
0
            // Analyzes an image to produce a result.
            //
            // <p>The caller is responsible for ensuring this analysis method can be executed quickly
            // enough to prevent stalls in the image acquisition pipeline. Otherwise, newly available
            // images will not be acquired and analyzed.
            //
            // <p>The image passed to this method becomes invalid after this method returns. The caller
            // should not store external references to this image, as these references will become
            // invalid.
            //
            // @param image image being analyzed VERY IMPORTANT: Analyzer method implementation must
            // call image.close() on received images when finished using them. Otherwise, new images
            // may not be received or the camera may stall, depending on back pressure setting.
            //
            public void Analyze(IImageProxy image)
            {
                // If there are no listeners attached, we don't need to perform analysis
                if (listeners.Length == 0)
                {
                    image.Close();
                    return;
                }

                // Keep track of frames analyzed
                var currentTime = Java.Lang.JavaSystem.CurrentTimeMillis();

                frameTimestamps.Enqueue(currentTime);

                // Compute the FPS using a moving average
                while (frameTimestamps.Count > frameRateWindow)
                {
                    frameTimestamps.Dequeue();
                }
                var timestampFirst = frameTimestamps.First();
                var timestampLast  = frameTimestamps.Last();

                framesPerSecond = 1.0 / ((timestampLast - timestampFirst) /
                                         System.Math.Max(1, frameTimestamps.Count - 1)) * 1000.0;
                if (++frameCounter % frameRateWindow == 0)
                {
                    Log.Debug(Tag, "Frames per second: " + framesPerSecond.ToString("0.00"));
                }

                // Analysis could take an arbitrarily long amount of time
                // Since we are running in a different thread, it won't stall other use cases

                lastAnalyzedTimestamp = frameTimestamps.Last();

                // Since format in ImageAnalysis is YUV, image.planes[0] contains the luminance plane
                var buffer = image.GetPlanes()[0].Buffer;

                // Extract image data from callback object
                ToByteArray(buffer);

                // Compute average luminance for the image
                double luma = 0;

                for (int i = 0; i < data.Length; i++)
                {
                    luma += data[i];
                }
                luma /= data.Length;

                // Call all listeners with new value
                foreach (LumaListener item in listeners)
                {
                    item(luma);
                }

                image.Close();
            }
示例#3
0
            // Analyzes an image to produce a result.
            //
            // <p>The caller is responsible for ensuring this analysis method can be executed quickly
            // enough to prevent stalls in the image acquisition pipeline. Otherwise, newly available
            // images will not be acquired and analyzed.
            //
            // <p>The image passed to this method becomes invalid after this method returns. The caller
            // should not store external references to this image, as these references will become
            // invalid.
            //
            // @param image image being analyzed VERY IMPORTANT: Analyzer method implementation must
            // call image.close() on received images when finished using them. Otherwise, new images
            // may not be received or the camera may stall, depending on back pressure setting.
            //
            public void Analyze(IImageProxy image)
            {
                // If there are no listeners attached, we don't need to perform analysis
                if (listeners.Length == 0)
                {
                    image.Close();
                    return;
                }

                // Keep track of frames analyzed
                var currentTime = Java.Lang.JavaSystem.CurrentTimeMillis();

                frameTimestamps.Enqueue(currentTime);

                // Compute the FPS using a moving average
                while (frameTimestamps.Count > frameRateWindow)
                {
                    frameTimestamps.Dequeue();
                }
                var timestampFirst = frameTimestamps.First();
                var timestampLast  = frameTimestamps.Last();

                framesPerSecond = 1.0 / ((timestampLast - timestampFirst) /
                                         System.Math.Max(1, frameTimestamps.Count)) * 1000.0;

                // Analysis could take an arbitrarily long amount of time
                // Since we are running in a different thread, it won't stall other use cases

                lastAnalyzedTimestamp = frameTimestamps.Last();

                // Since format in ImageAnalysis is YUV, image.planes[0] contains the luminance plane
                var buffer = image.GetPlanes()[0].Buffer;

                // Extract image data from callback object
                var data = ToByteArray(buffer);

                // Convert the data into an array of pixel values ranging 0-255
                var pixels = data.Select(x => (int)x & 0xFF).ToArray();

                // Compute average luminance for the image
                var luma = pixels.Average();

                // Call all listeners with new value
                foreach (LumaListener item in listeners)
                {
                    item(luma);
                }

                image.Close();
            }
示例#4
0
        public void Analyze(IImageProxy image)
        {
            var buffer = image.GetPlanes()[0].Buffer;
            var data   = ToByteArray(buffer);

            var pixels = data.ToList();

            pixels.ForEach(x => x = (byte)((int)x & 0xFF));
            var luma = pixels.Average(x => x);

            //Log.Debug(TAG, $"Average luminosity: {luma}");

            image.Close();

            lumaListerner.Invoke(luma);
        }