예제 #1
0
        public async void GetFaceAsyncTest()
        {
            GetFaceResult result     = null;
            var           identifier = System.Guid.NewGuid().ToString();

            try
            {
                var creation_result = await ApiReference.Instance.LargeFaceList.CreateAsync(identifier, identifier, identifier);

                AddFaceResult addface_result = null;
                if (creation_result)
                {
                    dynamic jUserData = new JObject();
                    jUserData.UserDataSample = "User Data Sample";
                    var rUserData = JsonConvert.SerializeObject(jUserData);

                    addface_result = await ApiReference.Instance.LargeFaceList.AddFaceAsync(identifier, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty);

                    if (addface_result != null)
                    {
                        result = await ApiReference.Instance.LargeFaceList.GetFaceAsync(identifier, addface_result.persistedFaceId);
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                var deletion_result = await ApiReference.Instance.LargeFaceList.DeleteAsync(identifier);
            }

            Assert.True(result != null);
        }
예제 #2
0
        public void GetFaceAsyncTest()
        {
            TimeoutHelper.ThrowExceptionInTimeout(() =>
            {
                GetFaceResult result = null;
                var identifier       = System.Guid.NewGuid().ToString();
                var personId         = string.Empty;

                try
                {
                    var creation_group_result = APIReference.Instance.LargePersonGroupInstance.CreateAsync(identifier, identifier, identifier).Result;
                    System.Diagnostics.Trace.Write($"Creation Result: {creation_group_result}");

                    var creation_person_result = APIReference.Instance.LargePersonGroupPersonInstance.CreateAsync(identifier, identifier, identifier).Result;
                    personId = creation_person_result.personId;
                    System.Diagnostics.Trace.Write($"Creation Result: {creation_person_result.personId}");

                    AddFaceResult addface_result = null;
                    if ((creation_person_result != null) && creation_group_result)
                    {
                        dynamic jUserData        = new JObject();
                        jUserData.UserDataSample = "User Data Sample";
                        var rUserData            = JsonConvert.SerializeObject(jUserData);

                        addface_result = APIReference.Instance.LargePersonGroupPersonInstance.AddFaceAsync(identifier, personId, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty).Result;

                        if (addface_result != null)
                        {
                            result = APIReference.Instance.LargePersonGroupPersonInstance.GetFaceAsync(identifier, personId, addface_result.persistedFaceId).Result;
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    var deletion_person_result = APIReference.Instance.LargePersonGroupPersonInstance.DeleteAsync(identifier, personId).Result;
                    System.Diagnostics.Trace.Write($"Deletion Result: {deletion_person_result}");

                    var deletion_group_result = APIReference.Instance.LargePersonGroupInstance.DeleteAsync(identifier).Result;
                    System.Diagnostics.Trace.Write($"Deletion Result: {deletion_group_result}");
                }

                Assert.True(result != null);
            });
        }
예제 #3
0
        public static void DemoV2()
        {
            // Use FrsClient::GetV2() to get v2 api

            AuthInfo  authInfo  = new AuthInfo("https://face.cn-north-1.myhuaweicloud.com", "ak", "sk");
            FrsClient frsClient = new FrsClient(authInfo, "project id");

            try
            {
                Console.WriteLine("compare");
                CompareFaceResult compareFaceResult = frsClient.GetV2().GetCompareService().CompareFaceByUrl("/obs/1.jpg", "/obs/1.jpg");
                Console.WriteLine(compareFaceResult.GetJsonString());
                compareFaceResult = frsClient.GetV2().GetCompareService().CompareFaceByFile("/resource/1.jpeg", "/resource/1.jpeg");
                Console.WriteLine(compareFaceResult.GetJsonString());

                Console.WriteLine("detect");
                DetectFaceResult detectFaceResult = frsClient.GetV2().GetDetectService().DetectFaceByUrl("/obs/1.jpg", "0,1,2");
                Console.WriteLine(detectFaceResult.GetJsonString());
                DetectFaceResult detectFaceResult2 = frsClient.GetV2().GetDetectService().DetectFaceByFile("/resource/1.jpeg", "1,2");
                Console.WriteLine(detectFaceResult2.GetJsonString());

                Console.WriteLine("live detect");
                LiveDetectResult liveDetectResult = frsClient.GetV2().GetLiveDetectService().LiveDetectByUrl("/obs/1.jpg", "1", "0-1000");
                Console.WriteLine(liveDetectResult.GetJsonString());
                liveDetectResult = frsClient.GetV2().GetLiveDetectService().LiveDetectByFile("/resource/3.png", "1");
                Console.WriteLine(liveDetectResult.GetJsonString());

                Console.WriteLine("create face set");
                //CreateFaceSetResult createFaceSetResult = frsClient.GetFaceSetService().CreateFaceSet("face_set_name_test");
                //Console.WriteLine(createFaceSetResult.GetJsonString());
                CreateExternalFields createExternalFields = new CreateExternalFields();
                createExternalFields.AddField("testInt", FieldType.INTEGER);
                createExternalFields.AddField("testStr", FieldType.STRING);
                CreateFaceSetResult createFaceSetResult = frsClient.GetV2().GetFaceSetService().CreateFaceSet("face_set_name_test", 10000, createExternalFields);
                Console.WriteLine(createFaceSetResult.GetJsonString());

                Console.WriteLine("get all face set");
                GetAllFaceSetsResult getAllFaceSetsResult = frsClient.GetV2().GetFaceSetService().GetAllFaceSets();
                Console.WriteLine(getAllFaceSetsResult.GetJsonString());

                Console.WriteLine("get face set");
                GetFaceSetResult getFaceSetResult = frsClient.GetV2().GetFaceSetService().GetFaceSet("face_set_name_test");
                Console.WriteLine(getFaceSetResult.GetJsonString());

                Console.WriteLine("add face");
                AddFaceResult addFaceResult = frsClient.GetV2().GetFaceService().AddFaceByUrl("face_set_name_test", "/obs/1.jpg");
                Console.WriteLine(addFaceResult.GetJsonString());
                AddExternalFields addExternalFields = new AddExternalFields();
                addExternalFields.AddField("testInt", 1);
                addExternalFields.AddField("testStr", "str");
                addFaceResult = frsClient.GetV2().GetFaceService().AddFaceByFile("face_set_name_test", "/resource/3.png", addExternalFields);
                Console.WriteLine(addFaceResult.GetJsonString());

                Console.WriteLine("get all face");
                GetFaceResult getFaceResult = frsClient.GetV2().GetFaceService().GetFaces("face_set_name_test", 0, 10);
                Console.WriteLine(getFaceResult.GetJsonString());

                Console.WriteLine("search face");
                SearchFaceResult searchFaceResult = frsClient.GetV2().GetSearchService().SearchFaceByFile("face_set_name_test", "/resource/3.png", 10, 0.1);
                Console.WriteLine(searchFaceResult.GetJsonString());
                SearchSort searchSort = new SearchSort();
                searchSort.AddSortField("testInt", SortType.ASC);
                SearchReturnFields searchReturnFields = new SearchReturnFields();
                searchReturnFields.AddReturnField("testInt");
                searchFaceResult = frsClient.GetV2().GetSearchService().SearchFaceByUrl("face_set_name_test", "/obs/1.jpg", 10, 0, searchSort, searchReturnFields, null);
                Console.WriteLine(searchFaceResult.GetJsonString());

                Console.WriteLine("delete face set");
                DeleteFaceSetResult deleteFaceSetResult = frsClient.GetV2().GetFaceSetService().DeleteFaceSet("face_set_name_test");
                Console.WriteLine(deleteFaceSetResult.GetJsonString());
            }
            catch (Exception e)
            {
                Console.WriteLine("=======ERROR CLEAN==========");
                DeleteFaceSetResult deleteFaceSetResult = frsClient.GetV2().GetFaceSetService().DeleteFaceSet("face_set_name_test");
                Console.WriteLine(deleteFaceSetResult.GetJsonString());
                Console.WriteLine(e);
            }
        }
예제 #4
0
        private static void FindSimilarFacesInAllLargeFaceListsAsync()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            var processed = 0;

            using (var operation = telemetryClient.StartOperation <RequestTelemetry>("Find similar faces in all large face lists"))
            {
                try
                {
                    var filePath   = Path.Combine(Settings.FindSimilarFolderPath, "input.jpg");
                    var imageBytes = File.ReadAllBytes(filePath);
                    var stream     = new System.IO.MemoryStream(imageBytes);
                    var imageUri   = StorageHelper.UploadFileAsync(stream, $"input.jpg", "uploads", Settings.AzureWebJobsStorage, "image/jpeg").Result;

                    List <FaceClientSDK.Domain.Face.DetectResult> list = APIReference.Instance.Face.DetectAsync(imageUri, "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise", true, true).Result;
                    bool isValid = true;

                    if (list.Count == 0)
                    {
                        isValid = false;

                        //delete file from storage
                        var res = StorageHelper.DeleteFileAsync($"input.jpg", "uploads", Settings.AzureWebJobsStorage).Result;

                        Console.WriteLine($"no face detected!");
                        telemetryClient.TrackTrace($"no face detected!");
                    }

                    if (list.Count > 1)
                    {
                        isValid = false;

                        //delete file from storage
                        var res = StorageHelper.DeleteFileAsync($"input.jpg", "uploads", Settings.AzureWebJobsStorage).Result;

                        Console.WriteLine($"multiple faces detected!");
                        telemetryClient.TrackTrace($"multiple faces detected!");
                    }

                    if (isValid)
                    {
                        var detectedFaceId = list.First().faceId.ToString();

                        List <FaceClientSDK.Domain.Face.FindSimilarResult> facesFoundInAllFaceLists = new List <FaceClientSDK.Domain.Face.FindSimilarResult>();
                        List <ListResult> faceList = APIReference.Instance.LargeFaceList.ListAsync("0", "1000").Result;

                        if (Parallel.ForEach(faceList, (s) => {
                            try
                            {
                                Settings.LargeFaceListId = s.largeFaceListId;
                                Console.WriteLine($"Find similar faces in LargeFaceListId: {s.largeFaceListId}");
                                telemetryClient.TrackTrace($"Find similar faces in LargeFaceListId: {s.largeFaceListId}");

                                List <FaceClientSDK.Domain.Face.FindSimilarResult> similarFaces = APIReference.Instance.Face.FindSimilarAsync(detectedFaceId, string.Empty, Settings.LargeFaceListId, new string[] { }, 10, "matchPerson").Result;

                                foreach (FaceClientSDK.Domain.Face.FindSimilarResult fs in similarFaces)
                                {
                                    facesFoundInAllFaceLists.Add(fs);
                                }

                                processed++;
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                                telemetryClient.TrackException(ex);
                                return;
                            }
                        }).IsCompleted)
                        {
                            if (Parallel.ForEach(facesFoundInAllFaceLists, (fs) => {
                                try
                                {
                                    GetFaceResult face = APIReference.Instance.LargeFaceList.GetFaceAsync(Settings.LargeFaceListId, fs.persistedFaceId).Result;

                                    Console.WriteLine($"PersistedFaceId: {fs.persistedFaceId}, UserData: {face.userData}, Confidence: {fs.confidence}");
                                    telemetryClient.TrackTrace($"PersistedFaceId: {fs.persistedFaceId}, UserData: {face.userData}, Confidence: {fs.confidence}");
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine(ex.Message);
                                    telemetryClient.TrackException(ex);
                                    return;
                                }
                            }).IsCompleted)
                            {
                                Console.WriteLine($"Processed lists: {processed}");
                                operation.Telemetry.Properties["ProcessedLists"] = processed.ToString();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    telemetryClient.TrackException(ex);
                }

                stopwatch.Stop();
                telemetryClient.TrackEvent($"Find similar faces in all large face lists - completed at {stopwatch.ElapsedMilliseconds} ms.");

                telemetryClient.StopOperation(operation);
            }
        }