Home > OS >  Doing compare face requests on multiple target images via list object method
Doing compare face requests on multiple target images via list object method

Time:11-22

I am trying to create a compare face request that will take an input source image from one bucket and compare it to all target images within a second bucket. I am attempting this by creating a list objects request for the second bucket, then creating a foreach object loop that bases the target image on the object key. However, I am getting a convert type error.

This is the code I have:

public async Task<bool> FunctionHandler(string input, ILambdaContext context)
    {
        //number of matched faces
        int matched = 0;

        //Client setup
        var rekognitionclient = new AmazonRekognitionClient();
        var s3client = new AmazonS3Client();

        //Create list of target images
        ListObjectsRequest list = new ListObjectsRequest
        { 
            BucketName = "bucket2"
        };
        ListObjectsResponse listre = s3client.ListObjectsAsync(list);

        //loop of list
        foreach(Amazon.S3.Model.S3Object obj in listre.S3Objects)
        {
            //face request with input and obj.key images
            var comparefacesrequest = new CompareFacesRequest
            {
                SourceImage = new Image
                {
                    S3Object = new S3Objects
                    {
                        Bucket = "bucket1",
                        Name = input
                    }
                },
                TargetImage = new Image
                {
                    S3Object = new S3Objects
                    {
                        Bucket = "bucket2",
                        Name = obj.Key
                    }
                },

            };

            //compare with confidence of 75 (subject to change) to current target image
            var detectresponse = await rekognitionclient.CompareFacesAsync(comparefacesrequest);
     
            detectresponse.FaceMatches.ForEach(match =>
            {
                ComparedFace face = match.Face;
                if (match.Similarity > 75)
                {
                    //if face detected, raise matched
                    matched  ;
                }
            });
        }
        if (matched > 0)
        {
            return true;
        }
        return false;
    }

The error I am getting is with s3client.ListObjectsAsync(list), which produces the following error

CS0029 Cannot implicitly convert type 'System.Threading.Tasks.Task<Amazon.S3.Model.ListObjectsResponse>' to 'Amazon.S3.Model.ListObjectsResponse'

I have no idea what the system is meaning by making the list objects response be labeled as a task. Does anyone have any idea on what is happening?

CodePudding user response:

Without knowing anything about S3, I would guess that you need to await this: s3client.ListObjectsAsync(list); so the code would be:

ListObjectsResponse listre = await s3client.ListObjectsAsync(list);

When method names are postfixed with Async, it's normally a hint that you need to await them.

  • Related