コード例 #1
0
        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());
            }
        }