Пример #1
0
    public async Task ExecuteAsync()
    {
        var credentials  = new BasicAWSCredentials(this.options.AccessKey, this.options.SecretKey);
        var clientConfig = new AmazonRekognitionConfig
        {
            RegionEndpoint          = this.options.Region,
            AllowAutoRedirect       = true,
            DisableLogging          = true,
            MaxConnectionsPerServer = null,
            LogMetrics           = false,
            LogResponse          = false,
            UseDualstackEndpoint = false,
        };

        clientConfig.Validate();

        using var client = new AmazonRekognitionClient(credentials, clientConfig);

        try
        {
            await client.DescribeCollectionAsync(
                new DescribeCollectionRequest { CollectionId = this.options.CollectionID }).ConfigureAwait(false);
        }
        catch (ResourceNotFoundException)
        {
            await client.CreateCollectionAsync(
                new CreateCollectionRequest { CollectionId = this.options.CollectionID }).ConfigureAwait(false);
        }

        var files = this.options.Directory.EnumerateFiles(this.options.Pattern, enumerationOptions);

        // Output CSV data:
        Console.WriteLine("FileName,FaceID");

        foreach (FileInfo file in files)
        {
            using var stream = new DecoyMemoryStream(file.OpenRead(), leaveOpen: false);

            var result = await client.IndexFacesAsync(new IndexFacesRequest
            {
                CollectionId        = this.options.CollectionID,
                DetectionAttributes = detectionAttributes,
                MaxFaces            = 1,
                QualityFilter       = QualityFilter.AUTO,
                Image = new Image {
                    Bytes = stream
                }
            }).ConfigureAwait(false);

            if (result.FaceRecords != null && result.FaceRecords.Count > 0)
            {
                Console.WriteLine("{0},{1}", file.Name, result.FaceRecords[0].Face.FaceId);
            }
            else
            {
                Console.WriteLine("{0},{1}", file.Name, "NotDetected");
            }
        }
    }
Пример #2
0
    public async Task ExecuteAsync()
    {
        var credentials  = new BasicAWSCredentials(this.options.AccessKey, this.options.SecretKey);
        var clientConfig = new AmazonRekognitionConfig
        {
            RegionEndpoint          = this.options.Region,
            AllowAutoRedirect       = true,
            DisableLogging          = true,
            MaxConnectionsPerServer = null,
            LogMetrics           = false,
            LogResponse          = false,
            UseDualstackEndpoint = false,
        };

        clientConfig.Validate();

        using var client = new AmazonRekognitionClient(credentials, clientConfig);

        var files = this.options.Directory.EnumerateFiles(this.options.Pattern, enumerationOptions);

        // CSV column headers:
        Console.WriteLine("FileName,FileSize,Dimensions,TimeTaken,FaceID,Similarity");

        var watch = new Stopwatch();

        foreach (FileInfo file in files)
        {
            using var stream = new DecoyMemoryStream(file.OpenRead(), leaveOpen: false);

            string imageDimensions = null;

            using (var image = System.Drawing.Image.FromStream(stream, true, true))
            {
                imageDimensions = $"{image.Width}x{image.Height}";
            }

            stream.Position = 0L;

            watch.Restart();

            var result = await client.SearchFacesByImageAsync(new SearchFacesByImageRequest
            {
                CollectionId       = this.options.CollectionID,
                FaceMatchThreshold = 90f,
                QualityFilter      = QualityFilter.AUTO,
                MaxFaces           = 1,
                Image = new Image {
                    Bytes = stream
                }
            }).ConfigureAwait(false);

            watch.Stop();

            // CSV values:
            Console.Write("{0},{1},{2},{3},", file.Name, file.Length, imageDimensions, watch.ElapsedMilliseconds);

            if (result.FaceMatches != null && result.FaceMatches.Count > 0)
            {
                Console.WriteLine("{0},{1}", result.FaceMatches[0].Face.FaceId, result.FaceMatches[0].Similarity);
            }
            else
            {
                Console.WriteLine("null,0");
            }
        }
    }