public async Task <IActionResult> AddFaces([FromRoute] string listId, [FromForm] List <IFormFile> faces, [FromForm] IFormFile metadata) { var userData = new Dictionary <string, string>(); if (faces == null) { return(BadRequest()); } if (metadata != null) { using (var reader = new StreamReader(metadata.OpenReadStream())) { string line = await reader.ReadLineAsync(); while (!string.IsNullOrEmpty(line)) { string[] tokens = line.Split('\t'); userData.Add(tokens[0], tokens[1]); line = await reader.ReadLineAsync(); } } } var faceErrors = new Dictionary <string, string>(); foreach (var face in faces) { try { var parameters = new Dictionary <string, object> { { "largeFaceListId", listId } }; if (userData.Count > 0) { parameters.Add("userData", userData[face.FileName]); } string faceId = await _faceRecognition.AddFace(listId, face.OpenReadStream(), parameters); await _storage.AddFaceToContainer(listId, faceId, face.OpenReadStream()); } catch (Exception e) { faceErrors.Add(face.FileName, e.Message); } } // Train the collection after bulk uploading all the new faces await _faceRecognition.TrainCollection(listId, new Dictionary <string, object> { { "largeFaceListId", listId } }); if (faceErrors.Count > 0) { return(Ok(faceErrors)); } else { return(Ok()); } }
/// <inheritdoc /> public async Task <Guid> SignUp(string email, string givenName, string familyName, Stream faceStream) { List <Face> detectedFaces = await _faceRecognition.Detect(faceStream); // If more than one face is detected, throw a fit if (detectedFaces.Count > 1) { throw new Exception("More than one face detected. Please try again with only one person in the picture."); } Guid personId = Guid.Empty; Guid faceId = Guid.Empty; var additionalParameters = new Dictionary <string, object> { { "largePersonGroupId", _group.ToLower().Replace(' ', '-') }, { "name", $"{givenName} {familyName}" } }; var collectionExists = await _faceRecognition.GetCollection(_group, additionalParameters); if (collectionExists == null) { await _faceRecognition.CreateCollection(_group, additionalParameters); } string result = await _faceRecognition.AddFace(_group, faceStream, additionalParameters); if (result.IndexOf('|') > -1) { personId = Guid.Parse(result.Split('|')[0]); faceId = Guid.Parse(result.Split('|')[1]); additionalParameters.Add("personId", personId.ToString()); } else { faceId = Guid.Parse(result); } var document = await LoadUserData(); if (document == null) { document = new XmlDocument(); document.LoadXml("<users></users>"); var header = document.CreateXmlDeclaration("1.0", "utf-8", null); document.InsertBefore(header, document.DocumentElement); } using (var xmlStream = new MemoryStream()) { using (var xmlWriter = XmlWriter.Create(xmlStream)) { this.SaveUser(document, personId, email, givenName, familyName, faceId); document.WriteTo(xmlWriter); } await _storage.WriteUserData(_group, USER_DATA, xmlStream); await _storage.SaveFace(_group, faceId.ToString(), faceStream); return(personId); } }