public void AddFaceAsyncTest() { AddFaceResult result = null; var identifier = System.Guid.NewGuid().ToString(); try { var creation_result = APIReference.Instance.FaceListInstance.CreateAsync(identifier, identifier, identifier).Result; System.Diagnostics.Trace.Write($"Creation Result: {creation_result}"); if (creation_result) { dynamic jUserData = new JObject(); jUserData.UserDataSample = "User Data Sample"; var rUserData = JsonConvert.SerializeObject(jUserData); result = APIReference.Instance.FaceListInstance.AddFaceAsync(identifier, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty).Result; } } catch { throw; } finally { var deletion_result = APIReference.Instance.FaceListInstance.DeleteAsync(identifier).Result; System.Diagnostics.Trace.Write($"Deletion Result: {deletion_result}"); } Assert.True(result != null); }
public async void DeleteFaceAsyncTest() { bool result = false; var identifier = System.Guid.NewGuid().ToString(); try { var creation_result = await ApiReference.Instance.FaceList.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.FaceList.AddFaceAsync(identifier, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty); if (addface_result != null) { result = await ApiReference.Instance.FaceList.DeleteFaceAsync(identifier, addface_result.persistedFaceId); } } } catch { throw; } finally { var deletion_result = await ApiReference.Instance.FaceList.DeleteAsync(identifier); } Assert.True(result); }
public async void IdentifyAsyncTest() { bool resultTrainLargePersonGroup = false; AddFaceResult add_face_result = null; CreateResult creation_person_result = null; List <DetectResult> face_detect_result = null; List <IdentifyResult> result = null; var largePersonGroupId = System.Guid.NewGuid().ToString(); try { var creation_group_result = await ApiReference.Instance.LargePersonGroup.CreateAsync(largePersonGroupId, "person-group-name", "recognition_02"); if (creation_group_result) { creation_person_result = await ApiReference.Instance.LargePersonGroupPerson.CreateAsync(largePersonGroupId, "person-name", "User-provided data attached to the person."); dynamic jUserData = new JObject(); jUserData.UserDataSample = "User Data Sample"; var rUserData = JsonConvert.SerializeObject(jUserData); add_face_result = await ApiReference.Instance.LargePersonGroupPerson.AddFaceAsync(largePersonGroupId, creation_person_result.personId, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty); resultTrainLargePersonGroup = await ApiReference.Instance.LargePersonGroup.TrainAsync(largePersonGroupId); while (true) { System.Threading.Tasks.Task.Delay(1000).Wait(); var status = await ApiReference.Instance.LargePersonGroup.GetTrainingStatusAsync(largePersonGroupId); if (status.status != "running") { break; } } face_detect_result = await ApiReference.Instance.Face.DetectAsync(faceAPISettingsFixture.TestImageUrl, "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise", true, true); result = await ApiReference.Instance.Face.IdentifyAsync(largePersonGroupId, new[] { face_detect_result[0].faceId }, 1, 0); } } catch { throw; } finally { var deletion_result = await ApiReference.Instance.LargePersonGroup.DeleteAsync(largePersonGroupId); } Assert.True(result != null); }
public async void GetTrainingStatusAsyncTest() { GetTrainingStatusResult 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) { bool training_result = false; training_result = await ApiReference.Instance.LargeFaceList.TrainAsync(identifier); if (training_result) { while (true) { System.Threading.Tasks.Task.Delay(1000).Wait(); result = await ApiReference.Instance.LargeFaceList.GetTrainingStatusAsync(identifier); if (result.status != "running") { break; } } } } } } catch { throw; } finally { var deletion_result = await ApiReference.Instance.LargeFaceList.DeleteAsync(identifier); } Assert.True(result != null); }
public void UpdateFaceAsyncTest() { TimeoutHelper.ThrowExceptionInTimeout(() => { bool result = false; 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.UpdateFaceAsync(identifier, personId, addface_result.persistedFaceId, "User Data Sample").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); }); }
public void GetTrainingStatusAsyncTest() { TimeoutHelper.ThrowExceptionInTimeout(() => { GetTrainingStatusResult result = null; var identifier = System.Guid.NewGuid().ToString(); try { var creation_result = APIReference.Instance.LargeFaceListInstance.CreateAsync(identifier, identifier, identifier).Result; System.Diagnostics.Trace.Write($"Creation Result: {creation_result}"); AddFaceResult addface_result = null; if (creation_result) { dynamic jUserData = new JObject(); jUserData.UserDataSample = "User Data Sample"; var rUserData = JsonConvert.SerializeObject(jUserData); addface_result = APIReference.Instance.LargeFaceListInstance.AddFaceAsync(identifier, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty).Result; if (addface_result != null) { bool training_result = false; training_result = APIReference.Instance.LargeFaceListInstance.TrainAsync(identifier).Result; System.Diagnostics.Trace.Write($"Train Result: {training_result}"); if (training_result) { result = APIReference.Instance.LargeFaceListInstance.GetTrainingStatusAsync(identifier).Result; } } } } catch { throw; } finally { var deletion_result = APIReference.Instance.LargeFaceListInstance.DeleteAsync(identifier).Result; System.Diagnostics.Trace.Write($"Deletion Result: {deletion_result}"); } Assert.True(result != null); }); }
public void FindSimilarAsyncTest() { TestsHelper.CompletesIn(10, () => { List <FindSimilarResult> result = null; var identifier = System.Guid.NewGuid().ToString(); try { var creation_result = APIReference.Instance.FaceListInstance.CreateAsync(identifier, identifier, identifier).Result; System.Diagnostics.Trace.Write($"Creation Result: {creation_result}"); AddFaceResult addface_result = null; if (creation_result) { dynamic jUserData = new JObject(); jUserData.UserDataSample = "User Data Sample"; var rUserData = JsonConvert.SerializeObject(jUserData); addface_result = APIReference.Instance.FaceListInstance.AddFaceAsync(identifier, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty).Result; if (addface_result != null) { List <DetectResult> detection_result = APIReference.Instance.FaceInstance.DetectAsync(faceAPISettingsFixture.TestImageUrl, "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise", true, true).Result; if (detection_result != null) { result = APIReference.Instance.FaceInstance.FindSimilarAsync(detection_result[0].faceId, identifier, string.Empty, new string[] { }, 10, "matchPerson").Result; } } } } catch { throw; } finally { var deletion_result = APIReference.Instance.FaceListInstance.DeleteAsync(identifier).Result; System.Diagnostics.Trace.Write($"Deletion Result: {deletion_result}"); } Assert.True(result != null); }); }
public async void GetFaceAsyncTest() { GetFaceResult result = null; var identifier = System.Guid.NewGuid().ToString(); var personId = string.Empty; try { var creation_group_result = await ApiReference.Instance.LargePersonGroup.CreateAsync(identifier, identifier, identifier); var creation_person_result = await ApiReference.Instance.LargePersonGroupPerson.CreateAsync(identifier, identifier, identifier); personId = creation_person_result.personId; AddFaceResult addface_result = null; if (creation_group_result) { dynamic jUserData = new JObject(); jUserData.UserDataSample = "User Data Sample"; var rUserData = JsonConvert.SerializeObject(jUserData); addface_result = await ApiReference.Instance.LargePersonGroupPerson.AddFaceAsync(identifier, personId, faceAPISettingsFixture.TestImageUrl, rUserData, string.Empty); if (addface_result != null) { result = await ApiReference.Instance.LargePersonGroupPerson.GetFaceAsync(identifier, personId, addface_result.persistedFaceId); } } } catch { throw; } finally { var deletion_person_result = await ApiReference.Instance.LargePersonGroupPerson.DeleteAsync(identifier, personId); var deletion_group_result = await ApiReference.Instance.LargePersonGroup.DeleteAsync(identifier); } Assert.True(result != null); }
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); } }
private static async void AddFaceToLargeFaceListAsync() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var processed = 0; var IterationsToRetry = Settings.AddFaceRetries; var TimeToSleepForRetry = Settings.AddFaceTimeToSleepInMs; using (var operation = telemetryClient.StartOperation <RequestTelemetry>("Add face to large face list")) { try { if (!Directory.Exists(Settings.ImageFolderPath)) { throw new Exception("There was a problem validating the images folder"); } List <string> imageList = Directory.GetFiles(Settings.ImageFolderPath, "*.jpg").ToList(); telemetryClient.TrackTrace($"ImageFolderPath: {Settings.ImageFolderPath}"); foreach (string s in imageList) { for (int i = 0; i <= IterationsToRetry; i++) { FileInfo imageFile = null; try { imageFile = new FileInfo(s); var noExtension = imageFile.Name.Replace(imageFile.Extension, ""); Metadata metadata = new Metadata() { id = noExtension }; var imageBytes = File.ReadAllBytes(imageFile.FullName); var stream = new MemoryStream(imageBytes); var imageUri = await StorageHelper.UploadFileAsync(stream, $"{imageFile.Name}", "images", Settings.AzureWebJobsStorage, "image/jpeg"); AddFaceResult resultFaceToList = await APIReference.Instance.LargeFaceList.AddFaceAsync(Settings.LargeFaceListId, imageUri, metadata.id, string.Empty); if (resultFaceToList == null) { return; } processed++; Console.WriteLine($"Processed: {processed}, PersistedFaceId: {resultFaceToList.persistedFaceId}"); telemetryClient.TrackTrace($"Processed: {processed}, PersistedFaceId: {resultFaceToList.persistedFaceId}"); break; } catch (Exception ex) { var message = string.Empty; if (i > 0) { message = $"Retry #{i} in image: {imageFile.Name}, Exception: {ex.Message}"; } else { message = $"There was an error in image: {imageFile.Name}, Exception: {ex.Message}"; } Console.WriteLine(message); List <Exception> exs = new List <Exception>(); exs.Add(ex); AggregateException aex = new AggregateException(message, exs); telemetryClient.TrackException(aex); Task.Delay(TimeToSleepForRetry).Wait(); } } } Console.WriteLine($"Processed: {processed}"); operation.Telemetry.Properties["ProcessedFaces"] = processed.ToString(); } catch (Exception ex) { Console.WriteLine(ex.Message); telemetryClient.TrackException(ex); } stopwatch.Stop(); telemetryClient.TrackEvent($"Add face to large face list - completed at {stopwatch.ElapsedMilliseconds} ms."); telemetryClient.StopOperation(operation); } }