示例#1
0
    public static void Example()
    {
        String collectionId = "MyCollection";
        String faceId       = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

        AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient();

        // Search collection for faces matching the face id.

        SearchFacesRequest searchFacesRequest = new SearchFacesRequest()
        {
            CollectionId       = collectionId,
            FaceId             = faceId,
            FaceMatchThreshold = 70F,
            MaxFaces           = 2
        };

        SearchFacesResponse searchFacesResponse = rekognitionClient.SearchFaces(searchFacesRequest);

        Console.WriteLine("Face matching faceId " + faceId);

        Console.WriteLine("Matche(s): ");
        foreach (FaceMatch face in searchFacesResponse.FaceMatches)
        {
            Console.WriteLine("FaceId: " + face.Face.FaceId + ", Similarity: " + face.Similarity);
        }
    }
示例#2
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            SearchFacesResponse response = new SearchFacesResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("FaceMatches", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <FaceMatch, FaceMatchUnmarshaller>(FaceMatchUnmarshaller.Instance);
                    response.FaceMatches = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("SearchedFaceId", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.SearchedFaceId = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
示例#3
0
        // snippet-start:[Rekognition.dotnetv3.SearchFacesMatchingIdExample]
        public static async Task Main()
        {
            string collectionId = "MyCollection";
            string faceId       = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

            var rekognitionClient = new AmazonRekognitionClient();

            // Search collection for faces matching the face id.
            var searchFacesRequest = new SearchFacesRequest
            {
                CollectionId       = collectionId,
                FaceId             = faceId,
                FaceMatchThreshold = 70F,
                MaxFaces           = 2,
            };

            SearchFacesResponse searchFacesResponse = await rekognitionClient.SearchFacesAsync(searchFacesRequest);

            Console.WriteLine("Face matching faceId " + faceId);

            Console.WriteLine("Matche(s): ");
            searchFacesResponse.FaceMatches.ForEach(face =>
            {
                Console.WriteLine($"FaceId: {face.Face.FaceId} Similarity: {face.Similarity}");
            });
        }
        public void cloudCheck()
        {
            String aaaa = path + "aaa.png";

            Console.WriteLine(aaaa);
            String ImagBase64 = ImgToBase64String(aaaa);

            //String ImagBase64 = "";
            //StreamWriter sw = new StreamWriter(Application.StartupPath + "\\Log.txt", true, Encoding.Default);
            //sw.WriteLine(ImagBase64);
            //sw.Close();
            //  Console.WriteLine(ImagBase64);


            try
            {
                Credential cred = new Credential
                {
                    SecretId  = "XXXX",
                    SecretKey = "yyyy"
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile   httpProfile   = new HttpProfile();
                httpProfile.Endpoint      = ("iai.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                IaiClient          client    = new IaiClient(cred, "ap-seoul", clientProfile);
                SearchFacesRequest req       = new SearchFacesRequest();
                string             strParams = "{\"GroupIds\":[\"hjtest\"],\"Image\":\"" + ImagBase64 + "\"}";
                req = SearchFacesRequest.FromJsonString <SearchFacesRequest>(strParams);
                SearchFacesResponse resp = client.SearchFacesSync(req);
                String tt = AbstractModel.ToJsonString(resp);
                //   Console.WriteLine(tt);
                String temp = GetPerson(tt);
                if (temp.Equals("error"))
                {
                    //MessageBox.Show("존재하지 않는 Person");
                }
                else
                {
                }
                // Console.WriteLine(temp);
                ListBoxItemAdd(this, this.listBox1, temp);
            }
            catch (Exception e)
            {
                ListBoxItemAdd(this, this.listBox1, "존재하지 않는 Person");
                Console.WriteLine(e.ToString());
            }
            Console.Read();
        }
示例#5
0
        public List <FaceRecord> Recognize(string collectionId, Amazon.Rekognition.Model.Image image)
        {
            //1- Detect faces in the input image and adds them to the specified collection.
            AmazonRekognitionClient rekognitionClient = AmazonClient.GetInstance();

            IndexFacesRequest indexFacesRequest = new IndexFacesRequest()
            {
                Image               = image,
                CollectionId        = collectionId,
                DetectionAttributes = new List <String>()
                {
                    "DEFAULT"
                }
            };

            IndexFacesResponse indexFacesResponse = rekognitionClient.IndexFaces(indexFacesRequest);

            //2- Search all detected faces in the collection
            SearchFacesResponse searchFacesResponse = null;

            List <FaceRecord> matchedFaces = new List <FaceRecord>();

            if (null != indexFacesResponse && null != indexFacesResponse.FaceRecords && 0 != indexFacesResponse.FaceRecords.Count)
            {
                foreach (FaceRecord face in indexFacesResponse.FaceRecords)
                {
                    searchFacesResponse = rekognitionClient.SearchFaces(new SearchFacesRequest
                    {
                        CollectionId       = collectionId,
                        FaceId             = face.Face.FaceId,
                        FaceMatchThreshold = 70F,
                        MaxFaces           = 2
                    });

                    if (searchFacesResponse.FaceMatches != null && searchFacesResponse.FaceMatches.Count != 0)
                    {
                        matchedFaces.Add(face);
                    }
                }

                //Remove newly added faces to the collection

                _collectionService.RemoveFacesFromCollection(collectionId, indexFacesResponse.FaceRecords.Select(x => x.Face.FaceId).ToList());
            }

            return(matchedFaces);
        }
示例#6
0
        /// <summary>
        /// A function for responding to S3 create events. It will determine if the object is an image and use Amazon Rekognition
        /// to detect labels and add the labels as tags on the S3 object.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(S3Event input, ILambdaContext context)
        {
            foreach (var record in input.Records)
            {
                int foundCount = 0;
                if (!SupportedImageTypes.Contains(Path.GetExtension(record.S3.Object.Key)))
                {
                    Console.WriteLine($"Object {record.S3.Bucket.Name}:{record.S3.Object.Key} is not a supported image type");
                    continue;
                }

                var bucket = record.S3.Bucket.Name;
                var key    = record.S3.Object.Key;

                Console.WriteLine($"Processing image {key} from bucket {bucket}");

                var metadataResponse = await this.S3Client.GetObjectMetadataAsync(new GetObjectMetadataRequest
                {
                    BucketName = bucket,
                    Key        = key
                });

                var user = metadataResponse.Metadata["x-amz-meta-user"];

                if (user != null)
                {
                    var listFacesRes = await this.RekognitionClient.ListFacesAsync(new ListFacesRequest
                    {
                        CollectionId = user
                    });

                    foreach (var face in listFacesRes.Faces)
                    {
                        Console.WriteLine(face.FaceId);
                    }

                    var detectFacesResponses = await this.RekognitionClient.IndexFacesAsync(new IndexFacesRequest
                    {
                        CollectionId = user,
                        Image        = new Image
                        {
                            S3Object = new Amazon.Rekognition.Model.S3Object
                            {
                                Bucket = bucket,
                                Name   = key
                            }
                        }
                    });

                    Console.WriteLine($"Detected {detectFacesResponses.FaceRecords.Count} faces");
                    listFacesRes = await this.RekognitionClient.ListFacesAsync(new ListFacesRequest
                    {
                        CollectionId = user
                    });

                    foreach (var face in listFacesRes.Faces)
                    {
                        Console.WriteLine(face.FaceId);
                    }
                    if (detectFacesResponses.FaceRecords.Count > 0)
                    {
                        foreach (var detectedFace in detectFacesResponses.FaceRecords)
                        {
                            SearchFacesResponse searchFacesReponse = null;
                            try
                            {
                                Console.WriteLine($"Col {user} - Faceid {detectedFace.Face.FaceId} ");
                                searchFacesReponse = await this.RekognitionClient.SearchFacesAsync(new SearchFacesRequest
                                {
                                    CollectionId       = user,
                                    FaceId             = detectedFace.Face.FaceId,
                                    FaceMatchThreshold = 80f,
                                    MaxFaces           = 1
                                });
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message + "\n" + e.GetType().ToString());
                            }
                            if (searchFacesReponse != null)
                            {
                                Console.WriteLine($"Matched {searchFacesReponse.FaceMatches.Count} faces");
                                var match = searchFacesReponse.FaceMatches.FirstOrDefault();
                                if (match != null)
                                {
                                    try
                                    {
                                        Table facesTable   = Table.LoadTable(this.DynamoDBClient, FACES_TABLE_NAME);
                                        var   detectedUser = await facesTable.GetItemAsync(match.Face.FaceId);

                                        Console.WriteLine($"Matched faces {detectedFace.Face.FaceId} with {match.Face.FaceId} confidence {match.Similarity}");
                                        foundCount++;
                                        var updateRequest = new UpdateItemRequest
                                        {
                                            TableName = USER_PHOTOS_TABLE_NAME,
                                            Key       = new Dictionary <string, AttributeValue>()
                                            {
                                                { "Face", new AttributeValue {
                                                      S = detectedUser["Email"]
                                                  } }
                                            },
                                            ExpressionAttributeNames = new Dictionary <string, string>()
                                            {
                                                { "#P", "Photo" }
                                            },
                                            ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                                            {
                                                { ":photokey", new AttributeValue {
                                                      SS = { key }
                                                  } }
                                            },
                                            UpdateExpression = "ADD #P :photokey"
                                        };
                                        await this.DynamoDBClient.UpdateItemAsync(updateRequest);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine(e.Message + "\n" + e.GetType().ToString());
                                    }
                                }
                            }
                        }
                    }
                    var cleanup = await this.RekognitionClient.DeleteFacesAsync(new DeleteFacesRequest
                    {
                        CollectionId = user,
                        FaceIds      = detectFacesResponses.FaceRecords.Select(x => x.Face.FaceId).ToList()
                    });

                    Console.WriteLine($"Found a match for {foundCount} out of {detectFacesResponses.FaceRecords.Count} detected faces");
                }
                else
                {
                    Console.WriteLine("There was no username metadata");
                }
                return;
            }
        }