Beispiel #1
0
        public async ValueTask <TOut> ProcessAsync(TIn value, IAsyncStageProgress progress, ILoggerFactory loggerFactory, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var currentValue   = (object?)value;
            var pipelineLogger = loggerFactory.CreateLogger <Pipeline <TIn, TOut> >();

            pipelineLogger.LogDebug("Starting pipeline with value: {Value}.", currentValue);

            foreach (var item in _items)
            {
                var startTime = DateTimeOffset.Now;

                pipelineLogger.LogDebug("Started stage {Identifier} at {Time}.", item.Identifier, startTime);

                using (await progress.OpenScopeAsync(item.Identifier, cancellationToken))
                {
                    var logger = loggerFactory.CreateLogger(item.Identifier);
                    currentValue = await item.ProcessAsync(currentValue, progress, logger, cancellationToken);
                }

                pipelineLogger.LogDebug("Completed stage {Identifier} (took {Time}ms): {Value}.",
                                        item.Identifier, (DateTimeOffset.Now - startTime).TotalMilliseconds, currentValue);
            }

            pipelineLogger.LogDebug("Completed pipeline with value: {Value}.", currentValue);

            return((TOut)currentValue !);
        }
Beispiel #2
0
        /// <inheritdoc/>
        public ValueTask <IReadOnlyList <IPooledBitmap> > ProcessAsync(IReadOnlyList <IPooledBitmap> value, IAsyncStageProgress progress, ILogger logger, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            const int DesiredWidth  = 416;
            const int DesiredHeight = 416;

            var bitmaps = new List <IPooledBitmap>();

            foreach (var bitmap in value)
            {
                if (bitmap.Bitmap.Width == DesiredWidth && bitmap.Bitmap.Height == DesiredHeight)
                {
                    // pass-through
                    bitmaps.Add(bitmap);
                }
                else
                {
                    // rescale
                    var resizedBitmap = new Bitmap(bitmap.Bitmap, DesiredWidth, DesiredHeight);
                    bitmaps.Add(new NonPooledBitmap(resizedBitmap));
                    // TODO: dispose old
                }
            }

            return(new ValueTask <IReadOnlyList <IPooledBitmap> >(bitmaps));
        }
Beispiel #3
0
 /// <inheritdoc/>
 public async Task <object?> ProcessAsync(object?value, IAsyncStageProgress progress, ILogger logger, CancellationToken cancellationToken = default)
 {
     cancellationToken.ThrowIfCancellationRequested();
     return(await _stage.ProcessAsync((TItemIn)value !, progress, logger, cancellationToken));
 }
Beispiel #4
0
        /// <inheritdoc/>
        public async ValueTask <IReadOnlyList <IReadOnlyList <YoloBoundingBox> > > ProcessAsync(IReadOnlyList <IPooledBitmap> bitmaps, IAsyncStageProgress progress, ILogger logger, CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ITransformer onnxTransformer;

            using (await progress.OpenScopeAsync("Loading ONNX model", cancellationToken))
            {
                onnxTransformer = _objectRecognizer.LoadModel("TinyYolo2_model.onnx");
            }

            IReadOnlyList <IReadOnlyList <YoloBoundingBox> > boundingBoxes;

            using (await progress.OpenScopeAsync("Analyzing data", cancellationToken))
            {
                var data = bitmaps.Select(x => new ImageData {
                    Image = x.Bitmap
                });
                var dataView = _objectRecognizer.MlContext.Data.LoadFromEnumerable(new[] { data.First() }); // TODO: remove first

                boundingBoxes = onnxTransformer
                                .Transform(dataView)
                                .GetColumn <float[]>("grid")
                                .Select(x => _outputParser.ParseOutputs(x))
                                .ToList();

                foreach (var bitmap in bitmaps)
                {
                    bitmap.Dispose();
                }
            }

            return(boundingBoxes);
        }
 /// <inheritdoc/>
 public async ValueTask <IReadOnlyList <TableBoundary> > ProcessAsync(IReadOnlyList <IReadOnlyList <YoloBoundingBox> > value, IAsyncStageProgress progress, ILogger logger, CancellationToken cancellationToken = default)
 {
     return(value[0].Select(x => new TableBoundary(
                                x: (int)x.Dimensions.X,
                                y: (int)x.Dimensions.Y,
                                width: (int)x.Dimensions.Width,
                                height: (int)x.Dimensions.Height)).ToArray());
 }
Beispiel #6
0
        /// <inheritdoc/>
        public async ValueTask <IReadOnlyList <IPooledBitmap> > ProcessAsync(ICamera value, IAsyncStageProgress progress, ILogger logger, CancellationToken cancellationToken = default)
        {
            var images = new List <IPooledBitmap>(capacity: 50);

            // record frames for 5 seconds, with a maximum of 50 frames
            var stopwatch = new Stopwatch();

            stopwatch.Restart();

            await using var providingEnumerator = value
                                                  .ReadAllAsync(cancellationToken)
                                                  .GetAsyncEnumerator(cancellationToken);

            while (await providingEnumerator.MoveNextAsync() && stopwatch.ElapsedMilliseconds < 5000 && images.Count < 50)
            {
                await progress.ReportAsync(
                    progress : images.Count / 50F,
                    status : $"Loading images ({images.Count} / 50, {stopwatch.Elapsed.TotalSeconds:0} of 5s)",
                    cancellationToken : cancellationToken);

                var bitmap = providingEnumerator.Current;
                images.Add(bitmap);

                //await Task.Delay(250, cancellationToken);
            }

            return(images);
        }