Exemple #1
0
        public async Task RunAsync(CancellationToken cancellationToken)
        {
            IMediaProcessorFlow flow = _flowFactory.CreateFlow("ScanFaces");

            List <Store.Media> medias = await _storeContext.Medias.AsQueryable()
                                        .Where(x =>
                                               x.FaceCount == 0 &&
                                               x.MediaType == MediaType.Image &&
                                               x.State == MediaState.Active)

                                        .OrderByDescending(x => x.DateTaken)
                                        .Take(100)
                                        .ToListAsync(cancellationToken);

            foreach (Media media in medias)
            {
                try
                {
                    Console.WriteLine($"Scanning faces: {media.Id}");
                    var context = new MediaProcessorContext
                    {
                        Media = media,
                        Image = await GetImageAsync(media)
                    };

                    await flow.ExecuteAsync(context, cancellationToken);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }
Exemple #2
0
        public async Task ScanByMediaIdAsync(Guid mediaId, CancellationToken cancellationToken)
        {
            Media media = await _mediaService.GetByIdAsync(mediaId, cancellationToken);

            Stream stream = _mediaService.GetMediaStream(media);

            var context = new MediaProcessorContext
            {
                Media = media,
                Image = await Image.LoadAsync(stream)
            };

            await _flow.ExecuteAsync(context, cancellationToken);
        }
Exemple #3
0
        public async Task DiscoverAsync(
            FileSystemDiscoveryOptions options,
            CancellationToken cancellationToken)
        {
            var todo = new List <MediaDiscoveryIdentifier>();

            foreach (IMediaSourceDiscovery source in _discoveryFactory.GetSources())
            {
                IEnumerable <MediaDiscoveryIdentifier> identifiers = await source
                                                                     .DiscoverMediaAsync(options, cancellationToken);

                todo.AddRange(identifiers);
            }

            IMediaProcessorFlow flow = _flowFactory.CreateFlow("ImportImage");

            foreach (MediaDiscoveryIdentifier file in todo)
            {
                IMediaSourceDiscovery src = _discoveryFactory.GetSource(file.Source);

                byte[] data = await src.GetMediaDataAsync(file.Id, default);

                var context = new MediaProcessorContext
                {
                    OriginalData = data,
                    File         = file
                };
                try
                {
                    Console.WriteLine($"Importing: {file.Id}");
                    await flow.ExecuteAsync(context, default);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                //TODO: Move to Imported or delete
            }
        }
        public async Task Consume(ConsumeContext <NewMediaAddedMessage> context)
        {
            Media media = await _mediaService.GetByIdAsync(
                context.Message.Id,
                context.CancellationToken);

            if (media.MediaType == MediaType.Image)
            {
                await _mediaFaceScanner.ScanByMediaIdAsync(
                    context.Message.Id,
                    context.CancellationToken);
            }

            if (media.MediaType == MediaType.Video)
            {
                await _videoFlow.ExecuteAsync(new MediaProcessorContext
                {
                    Media = media
                }, context.CancellationToken);
            }
        }
Exemple #5
0
        public async Task RunAsync(int count, CancellationToken cancellationToken)
        {
            IMediaProcessorFlow flow = _processorFlowFactory.CreateFlow("ImportImage");

            int todo = count;

            while (todo > 0)
            {
                foreach (ISampleDataSource source in _sources)
                {
                    SampleMedia media = await source.LoadAsync(cancellationToken);

                    await flow.ExecuteAsync(new MediaProcessorContext
                    {
                        File = new MediaDiscoveryIdentifier
                        {
                            Id     = media.Filename,
                            Source = MediaDiscoverySource.WebUpload
                        },
                        OriginalData = media.Data
                    }, cancellationToken);
                }
            }
        }
Exemple #6
0
        public async Task ScanExistingAsync(
            FileSystemDiscoveryOptions options,
            CancellationToken cancellationToken)
        {
            var todo = new List <MediaDiscoveryIdentifier>();

            foreach (IMediaSourceDiscovery source in _discoveryFactory.GetSources())
            {
                IEnumerable <MediaDiscoveryIdentifier> identifiers = await source
                                                                     .DiscoverMediaAsync(options, cancellationToken);

                todo.AddRange(identifiers);
            }

            IMediaProcessorFlow imageFlow = _flowFactory.CreateFlow("ImportImageNoFace");
            IMediaProcessorFlow videoFlow = _flowFactory.CreateFlow("ImportVideo");

            var processionOptions = new MediaProcessingOptions
            {
                SaveMedia = new SaveMediaFileOptions
                {
                    SaveMode     = SaveMediaMode.KeepInSource,
                    SourceAction = SaveMediaSourceAction.Keep
                }
            };


            foreach (MediaDiscoveryIdentifier file in todo)
            {
                IMediaSourceDiscovery src = _discoveryFactory.GetSource(file.Source);

                var extension = Path.GetExtension(file.Id);

                if (extension == ".mp4")
                {
                    var context = new MediaProcessorContext
                    {
                        File      = file,
                        Options   = processionOptions,
                        MediaType = MediaType.Video
                    };
                    try
                    {
                        Console.WriteLine($"Importing vide: {file.Id}");
                        await videoFlow.ExecuteAsync(context, default);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    byte[] data = await src.GetMediaDataAsync(file.Id, default);

                    var context = new MediaProcessorContext
                    {
                        OriginalData = data,
                        File         = file,
                        Options      = processionOptions,
                        MediaType    = MediaType.Image
                    };
                    try
                    {
                        Console.WriteLine($"Importing: {file.Id}");
                        await imageFlow.ExecuteAsync(context, default);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
        }