public static async Task <string> CreatePersonInPersonGroup(PersonBindingModel model) { var createPersonResult = await FaceServiceClient.CreatePersonInPersonGroupAsync(model.PersonGroupId, model.Name); foreach (var faceUrl in model.FacesUrl) { await FaceServiceClient.AddPersonFaceInPersonGroupAsync(model.PersonGroupId, createPersonResult.PersonId, faceUrl); } return(createPersonResult.PersonId.ToString()); }
private async Task CreatePerson(string personname, string groupId, string imagePath) { CreatePersonResult person = await fsClient.CreatePersonInPersonGroupAsync(groupId, personname); Stream s = File.OpenRead(imagePath); await fsClient.AddPersonFaceInPersonGroupAsync(groupId, person.PersonId, s); }
/// <summary> /// Adds a person to a group - supports identifying them later. PersonId is the one returned from <see cref="CreatePersonInPersonGroupAsync"/> /// </summary> /// <param name="personId"></param> /// <param name="fileData"></param> /// <param name="groupId"></param> /// <returns></returns> public async Task <Guid> AddPersonFaceAsync(Guid personId, byte[] fileData, string groupId = "") { var targetGroup = string.IsNullOrEmpty(groupId) ? _defaultPersonGroupId : groupId; var group = await GetPersonGroupOrCreateAsync(targetGroup); try { //todo: check if person exists? //var person = await _client.GetPersonInPersonGroupAsync(group.PersonGroupId, personId); using (var ms = new MemoryStream(fileData)) { var result = await _client.AddPersonFaceInPersonGroupAsync(group.PersonGroupId, personId, ms); // todo: move this to a queue-triggered function, since it's a long-running process we can do out-of-band await _client.TrainPersonGroupAsync(group.PersonGroupId); return(result.PersistedFaceId); } } catch (Exception) { // todo: there are multiple discrete transactions here, so on error there is a lot to compensate for. fix it throw; } }
public async Task <string> RegisterUser(List <Stream> listUserImages, string personName = "") { try { #region TestBed if (listUserImages == null) { listUserImages = new List <Stream>(); string directoryPath = @"C:\Users\Sachin13390\Desktop\Face_Data"; string[] allDirectory = Directory.GetDirectories(directoryPath); foreach (string dir in allDirectory) { personName = Path.GetFileName(dir); string[] file_Paths = Directory.GetFiles(dir); foreach (string filePath in file_Paths) { listUserImages.Add(GetImageStream(filePath)); } } } #endregion #region Images CreatePersonResult person = await faceServiceClient.CreatePersonInPersonGroupAsync(personGroupId, personName); foreach (Stream imageStream in listUserImages) { await faceServiceClient.AddPersonFaceInPersonGroupAsync(personGroupId, person.PersonId, imageStream); } await faceServiceClient.TrainPersonGroupAsync(personGroupId); TrainingStatus trainingStatus = null; while (true) { trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId); if (trainingStatus.Status != Status.Running) { break; } await Task.Delay(1000); } #endregion return(SUCCESSFULL); } catch (Exception exception) { return(FAILURE); } }
public async void AddNewUser(string newUserName, string imagesFolderPath, string groupId) { CreatePersonResult person = await faceServiceClient.CreatePersonInPersonGroupAsync(groupId, newUserName); foreach (string imagePath in Directory.GetFiles(imagesFolderPath)) { using (Stream s = File.OpenRead(imagePath)) { await faceServiceClient.AddPersonFaceInPersonGroupAsync(groupId, person.PersonId, s); } } }
private async Task addPersonImageToGroup(PersonalItem personalItem) { foreach (string imagePath in personalItem.imagePaths) { using (Stream s = File.OpenRead(imagePath)) { // Detect faces in the image and add to Anna await faceServiceClient.AddPersonFaceInPersonGroupAsync( personalItem.personGroupId, personalItem.guid, s); } } }
public async void TrainFaces(string[] trainImages) { //await faceServiceClient.CreatePersonGroupAsync(personGroupId, "Admins"); CreatePersonResult admin = await faceServiceClient.CreatePersonInPersonGroupAsync(personGroupId, "Akos"); foreach (string imagePath in trainImages) { using (Stream s = File.OpenRead(imagePath)) { await faceServiceClient.AddPersonFaceInPersonGroupAsync(personGroupId, admin.PersonId, s); } } await faceServiceClient.TrainPersonGroupAsync(personGroupId); }
private async void AddToFaceIdList() { string name = imageFolder.DisplayName; var person = await faceServiceClient.CreatePersonAsync(HeroOrVillanPersonGroup.PersonGroupId, name); var imageFiles = await imageFolder.GetFilesAsync(); foreach (var imageFile in imageFiles) { using (var s = await imageFile.OpenReadAsync()) { // Detect faces in the image and add to Anna await faceServiceClient.AddPersonFaceInPersonGroupAsync( HeroOrVillanPersonGroup.PersonGroupId, person.PersonId, imageStream : s.AsStream(), userData : name); } } }
private async Task uploadImages(string grpId, Guid prsnId, string path) { try { foreach (string imagePath in Directory.GetFiles(path, "*.jpg")) { await WaitCallLimitPerSecondAsync(); using (Stream stream = File.OpenRead(imagePath)) { await faceServiceClient.AddPersonFaceInPersonGroupAsync(grpId, prsnId, stream); } } } catch (Exception ex) { ShowErrorMsg(ex.Message); } }
public async Task <String> ProcessRegistrationImage() { Stream userRegisterImage = await this.Request.Content.ReadAsStreamAsync(); userRegisterImage.Position = 0; string imageRegistrationStatus = string.Empty; if (userRegisterImage == null) { } else { try { string personImageName = string.Format("UserImage_{0}", new Random().Next(0, 999).ToString("D3")); CreatePersonResult person = await faceServiceClient.CreatePersonInPersonGroupAsync(personGroupId, personImageName); await faceServiceClient.AddPersonFaceInPersonGroupAsync(personGroupId, person.PersonId, userRegisterImage); await faceServiceClient.TrainPersonGroupAsync(personGroupId); TrainingStatus trainingStatus = null; while (true) { trainingStatus = await faceServiceClient.GetPersonGroupTrainingStatusAsync(personGroupId); if (trainingStatus.Status != Microsoft.ProjectOxford.Face.Contract.Status.Running) { break; } await Task.Delay(1000); } imageRegistrationStatus = trainingStatus.Status.ToString(); } catch (Exception ex) { imageRegistrationStatus = ex.Message; } } return(imageRegistrationStatus); }
private async void faceUploadButton_Click(object sender, EventArgs e) { DialogResult result = openFileDialog.ShowDialog(); if (result == DialogResult.OK) { // Send image(s) to server imgInfoBox.Text = ""; int count = 0; foreach (string filename in openFileDialog.FileNames) { #if USE_OPENCV count++; PersonGroup selectedPersonGroup = personGroups[personGroupSelector.SelectedIndex]; Person selectedPerson = persons[personSelector.SelectedIndex]; Mat image = new Mat(filename, ImreadModes.GrayScale); OpenCvSharp.Rect[] faces = DetectFacesInImage(image, HaarDetectionType.DoRoughSearch, new OpenCvSharp.Size(300, 300)); if (faces.Length > 1) { MessageBox.Show(filename + ": Too many faces detected in this image", "Error"); foreach (OpenCvSharp.Rect rect in faces) { Mat errCrop = new Mat(image, rect); Mat errResizedImage = new Mat(); Cv2.Resize(errCrop, errResizedImage, new OpenCvSharp.Size(400, 400)); Cv2.ImShow("Detected Face", errResizedImage); Cv2.WaitKey(0); Cv2.DestroyWindow("Detected Face"); } } else if (faces.Length < 1) { MessageBox.Show(filename + ": No faces detected in this image", "Error"); } else { Mat crop = new Mat(image, faces[0]); Mat resizedImage = new Mat(); Cv2.Resize(crop, resizedImage, new OpenCvSharp.Size(400, 400)); Cv2.ImShow("Added Image", resizedImage); Cv2.WaitKey(0); Cv2.DestroyWindow("Added Image"); using (StreamWriter s = new StreamWriter(selectedPersonGroup.PersonGroupId + "/TrainData.dat", true)) { s.WriteLine(filename + "|" + selectedPerson.UserData); s.Close(); } } #else System.IO.MemoryStream imageMemoryStream = new MemoryStream(); System.IO.FileStream imageFileStream = new FileStream(filename, FileMode.Open); ResizeImage(imageFileStream, imageMemoryStream); imageFileStream.Close(); PersonGroup selectedPersonGroup = personGroups[personGroupSelector.SelectedIndex]; Person selectedPerson = persons[personSelector.SelectedIndex]; try { await faceServiceClient.AddPersonFaceInPersonGroupAsync(selectedPersonGroup.PersonGroupId, selectedPerson.PersonId, imageMemoryStream); imgInfoBox.Text += filename + ": added successfully\r\n"; } catch (FaceAPIException ex) { imgInfoBox.Text += filename + ": " + ex.ErrorMessage + "(" + ex.ErrorCode + ")\r\n"; // This shouldn't happen because of the throttling that happens below, but handle it just in case if (ex.ErrorCode == "RateLimitExceeded") { System.Threading.Thread.Sleep(60000); } } // We're only allowed 20 API calls per minute. If we're up to 20, wait a minute before proceeding. if (++count > 19) { imgInfoBox.Text += "Reached rate limit - waiting 60 seconds\r\n"; System.Threading.Thread.Sleep(60000); count = 0; } #endif } } }