コード例 #1
0
        public async Task <IdentificationResponse> IdentifySpeaker(IdentifyProfile model)
        {
            var profiles = await GetProfiles();

            var identificationProfileIds = string.Join(",", profiles.Where(x => x.EnrollmentStatus != null && x.EnrollmentStatus.EnrollmentStatusEnrollmentStatus == "Enrolled").Select(x => x.Id));

            var url = $"{ Configuration["AudioAnalyticsAPI"] }identify?identificationProfileIds={ identificationProfileIds }";

            var key = Configuration["AudioAnalyticsKey"];

            using (var stream = model.Audio.OpenReadStream())
            {
                var response = await CognitiveServicesHttpClient.HttpPostAudio(stream, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();

                    return(new IdentificationResponse {
                        OperationLocation = operationLocation
                    });
                }

                return(JSONHelper.FromJson <IdentificationResponse>(responseBytes));
            }
        }
コード例 #2
0
        public async Task <VerifyFaceResponse> VerifyFace(VerifyFace model)
        {
            var face = await DetectFace(model.Image);

            if (face.Length == 0 || face.Length > 1)
            {
                return(new VerifyFaceResponse
                {
                    Error = new Error
                    {
                        Code = "Validation error",
                        Message = face.Length == 0 ? "No face found" : "More than one face found"
                    }
                });
            }

            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + "/verify";

            var key = Configuration["CognitiveFacesKey"];

            var payload = "{ \"faceId\": \"" + face.First().FaceId + "\", \"personId\": \"" + model.Id + "\", \"personGroupId\": \"" + groupId + "\"}";

            var byteData = Encoding.UTF8.GetBytes(payload);

            using (var content = new ByteArrayContent(byteData))
            {
                var response = await CognitiveServicesHttpClient.HttpPost(content, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                return(JSONHelper.FromJson <VerifyFaceResponse>(responseBytes));
            }
        }
コード例 #3
0
        public async Task <double> GetScore(string text)
        {
            var url = Configuration["TextAnalyticsAPI"] + "sentiment";
            var key = Configuration["TextAnalyticsKey"];

            var payload = $"{{ \"documents\": [ {{ \"language\": \"en\", \"id\": \"1\", \"text\": \"{text}\"}}]}}";

            var byteData = Encoding.UTF8.GetBytes(payload);

            using (var content = new ByteArrayContent(byteData))
            {
                var response = await CognitiveServicesHttpClient.HttpPost(content, url, key);

                if (response.IsSuccessStatusCode)
                {
                    var responseBytes = await response.Content.ReadAsStringAsync();

                    var result = JSONHelper.FromJson <TextAnalytics>(responseBytes);

                    return(result.Documents[0].Score);
                }
            }

            return(0);
        }
コード例 #4
0
        public async Task <string> GetKeyPhrases(string text)
        {
            var url = Configuration["TextAnalyticsAPI"] + "keyPhrases";
            var key = Configuration["TextAnalyticsKey"];

            var something = $"{{ \"documents\": [ {{ \"language\": \"en\", \"id\": \"1\", \"text\": \"{text}\"}}]}}";

            var byteData = Encoding.UTF8.GetBytes(something);

            using (var content = new ByteArrayContent(byteData))
            {
                var response = await CognitiveServicesHttpClient.HttpPost(content, url, key);

                if (response.IsSuccessStatusCode)
                {
                    var responseBytes = await response.Content.ReadAsStringAsync();

                    var result = JSONHelper.FromJson <KeyPhrases>(responseBytes);

                    return(string.Join(",", result.Documents[0].KeyPhrases));
                }
            }

            return(string.Empty);
        }
コード例 #5
0
        public async Task <Guid> CreateProfile(FaceVerificationProfile model)
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/persons";

            var key = Configuration["CognitiveFacesKey"];

            var payload = "{ \"name\": \"" + model.Name + "\"}";

            var byteData = Encoding.UTF8.GetBytes(payload);

            using (var content = new ByteArrayContent(byteData))
            {
                var response = await CognitiveServicesHttpClient.HttpPost(content, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var result = JSONHelper.FromJson <CreatePersonResponse>(responseBytes);

                    return(result.PersonId);
                }

                throw new Exception($"Failed request : {responseBytes} ");
            }
        }
コード例 #6
0
        public async Task <IQueryable <AudioProfile> > GetProfiles()
        {
            var url = $"{ Configuration["AudioAnalyticsAPI"] }identificationProfiles";

            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var enrollments = JSONHelper.FromJson <EnrollmentStatus[]>(responseBytes);

                var table = Configuration["AudioIdentificationProfileTable"];

                var audioProfiles = await CloudTableService.Retrieve <AudioProfile>(table);

                foreach (var enrollment in enrollments)
                {
                    var profile = audioProfiles.SingleOrDefault(x => x.Id.ToString() == enrollment.IdentificationProfileId);

                    if (profile != null)
                    {
                        profile.EnrollmentStatus = enrollment;
                    }
                }

                return(audioProfiles);
            }

            throw new Exception($"Failed request : { responseBytes } ");
        }
コード例 #7
0
        public async Task <string> PollIdentifySpeaker(string url)
        {
            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            return(await response.Content.ReadAsStringAsync());
        }
コード例 #8
0
        public async Task EnrollProfile(EnrollProfile model)
        {
            var url = $"{ Configuration["AudioAnalyticsAPI"] }identificationProfiles/{ model.Id }/enroll";

            var key = Configuration["AudioAnalyticsKey"];

            using (var stream = model.Audio.OpenReadStream())
            {
                await CognitiveServicesHttpClient.HttpPostAudio(stream, url, key);
            }
        }
コード例 #9
0
        public async Task <TrainingStatus> GetTrainingStatus()
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/training";

            var key = Configuration["CognitiveFacesKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            return(JSONHelper.FromJson <TrainingStatus>(responseBytes));
        }
コード例 #10
0
        public async Task DeleteProfiles()
        {
            var profiles = await GetProfiles();

            var url = $"{ Configuration["AudioAnalyticsAPI"] }identificationProfiles/";

            var key = Configuration["AudioAnalyticsKey"];

            foreach (var profile in profiles)
            {
                var profileUrl = url + profile.Id;
                await CognitiveServicesHttpClient.HttpDelete(profileUrl, key);
            }
        }
コード例 #11
0
        public async Task Train()
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/train";

            var key = Configuration["CognitiveFacesKey"];

            var byteData = Encoding.UTF8.GetBytes(String.Empty);

            using (var content = new ByteArrayContent(byteData))
            {
                await CognitiveServicesHttpClient.HttpPost(content, url, key);
            }
        }
コード例 #12
0
        public async Task <string> EnrollProfile(EnrollVerificationProfile model)
        {
            var url = $"{Configuration["AudioAnalyticsAPI"]}verificationProfiles/{model.Id}/enroll";

            var key = Configuration["AudioAnalyticsKey"];

            using (var inputStream = model.Audio.OpenReadStream())
            {
                var response = await CognitiveServicesHttpClient.HttpPostAudio(inputStream, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                return(responseBytes);
            }
        }
コード例 #13
0
        public async Task <VerifySpeakerResponse> VerifySpeaker(VerifySpeaker model)
        {
            var url = $"{Configuration["AudioAnalyticsAPI"]}verify?verificationProfileId={model.Id}";

            var key = Configuration["AudioAnalyticsKey"];

            using (var inputStream = model.Audio.OpenReadStream())
            {
                var response = await CognitiveServicesHttpClient.HttpPostAudio(inputStream, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                return(JSONHelper.FromJson <VerifySpeakerResponse>(responseBytes));
            }
        }
コード例 #14
0
        public async Task AddFace(FaceVerificationEnrollProfile model)
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/persons/{model.PersonId}/persistedFaces";

            var key = Configuration["CognitiveFacesKey"];

            using (var stream = model.Image.OpenReadStream())
            {
                using (HttpContent content = new StreamContent(stream))
                {
                    await CognitiveServicesHttpClient.HttpPostImage(content, url, key);
                }
            }
        }
コード例 #15
0
        public async Task DeleteProfiles()
        {
            var profiles = await GetPersons();

            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/persons/";

            var key = Configuration["CognitiveFacesKey"];

            foreach (var profile in profiles)
            {
                var deleteUrl = url + profile.PersonId;

                await CognitiveServicesHttpClient.HttpDelete(deleteUrl, key);
            }
        }
コード例 #16
0
        public async Task <IdentificationStatus> PollIdentifySpeaker(string url)
        {
            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var result = JSONHelper.FromJson <IdentificationStatus>(responseBytes);

                return(result);
            }

            throw new Exception($"Failed request : { responseBytes } ");
        }
コード例 #17
0
        public async Task CreatePersonGroup()
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}";

            var key = Configuration["CognitiveFacesKey"];

            var payload = "{ \"name\": \"group1\"}";

            var byteData = Encoding.UTF8.GetBytes(payload);

            using (var content = new ByteArrayContent(byteData))
            {
                await CognitiveServicesHttpClient.HttpPut(content, url, key);
            }
        }
コード例 #18
0
        public async Task <PersonGroups[]> GetPersonGroups()
        {
            var url = Configuration["CognitiveFacesUri"] + $"/persongroups";

            var key = Configuration["CognitiveFacesKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                return(JSONHelper.FromJson <PersonGroups[]>(responseBytes));
            }

            throw new Exception($"Failed request : { responseBytes } ");
        }
コード例 #19
0
        public async Task <EnrollmentPhrases[]> GetVerificationPhrases(string locale)
        {
            var url = $"{Configuration["AudioAnalyticsAPI"]}verificationPhrases?locale={locale}";

            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var result = JSONHelper.FromJson <EnrollmentPhrases[]>(responseBytes);

                return(result);
            }

            return(new EnrollmentPhrases[] { });
        }
コード例 #20
0
        public async Task <EnrollmentStatus> CheckEnrollmentStatus(Guid id)
        {
            var url = $"{ Configuration["AudioAnalyticsAPI"] }identificationProfiles/{ id }";

            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var result = JSONHelper.FromJson <EnrollmentStatus>(responseBytes);

                return(result);
            }

            throw new Exception($"Failed request : { responseBytes } ");
        }
コード例 #21
0
        public async Task <IQueryable <FaceVerificationProfile> > GetPersons()
        {
            var groupId = Configuration["PersonGroupId"];

            var url = Configuration["CognitiveFacesUri"] + $"/persongroups/{groupId}/persons";

            var key = Configuration["CognitiveFacesKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                return(JSONHelper.FromJson <List <FaceVerificationProfile> >(responseBytes).AsQueryable());
            }

            throw new Exception($"Failed request : {responseBytes} ");
        }
コード例 #22
0
        private async Task RegisterRoryMcIlroy()
        {
            var profile = new AudioProfile
            {
                Name           = "Rory McIlroy",
                SelectedLocale = "en-us"
            };

            profile.Id = await CreateProfile(profile);

            var url = $"{Configuration["AudioAnalyticsAPI"]}identificationProfiles/{profile.Id}/enroll";

            var key = Configuration["AudioAnalyticsKey"];

            var req = WebRequest.Create("https://cognitiveservice.blob.core.windows.net/fakes/Rory%20McIlroy.wav");

            using (var stream = req.GetResponse().GetResponseStream())
            {
                await CognitiveServicesHttpClient.HttpPostAudio(stream, url, key);
            }
        }
コード例 #23
0
        public async Task DeleteProfiles()
        {
            var url = $"{Configuration["AudioAnalyticsAPI"]}verificationProfiles/";

            var key = Configuration["AudioAnalyticsKey"];

            var response = await CognitiveServicesHttpClient.HttpGet(url, key);

            var responseBytes = await response.Content.ReadAsStringAsync();

            if (response.IsSuccessStatusCode)
            {
                var enrollments = JSONHelper.FromJson <EnrollmentStatus[]>(responseBytes);

                foreach (var enrollment in enrollments)
                {
                    var profileUrl = url + enrollment.VerificationProfileId;

                    var r = await CognitiveServicesHttpClient.HttpDelete(profileUrl, key);
                }
            }
        }
コード例 #24
0
        public async Task <Face[]> DetectFace(IFormFile file)
        {
            var url = Configuration["CognitiveFacesUri"] + $"/detect?returnFaceId=true";

            var key = Configuration["CognitiveFacesKey"];

            using (var stream = file.OpenReadStream())
            {
                using (HttpContent content = new StreamContent(stream))
                {
                    var response = await CognitiveServicesHttpClient.HttpPostImage(content, url, key);

                    var responseBytes = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        return(JSONHelper.FromJson <Face[]>(responseBytes));
                    }

                    throw new Exception($"Failed request : {responseBytes} ");
                }
            }
        }
コード例 #25
0
        public async Task <Guid> CreateProfile(AudioProfile profile)
        {
            var url = Configuration["AudioAnalyticsAPI"] + "identificationProfiles";

            var key = Configuration["AudioAnalyticsKey"];

            var tableName = Configuration["AudioIdentificationProfileTable"];

            var payload = $"{{\"locale\" : \"{ profile.SelectedLocale.ToLower() }\"}}";

            var byteData = Encoding.UTF8.GetBytes(payload);

            using (var content = new ByteArrayContent(byteData))
            {
                var response = await CognitiveServicesHttpClient.HttpPost(content, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var result = JSONHelper.FromJson <IdentificationProfile>(responseBytes);

                    profile.Id = Guid.Parse(result.IdentificationProfileId);

                    profile.RowKey = profile.Id.ToString();

                    profile.PartitionKey = profile.Id.ToString();

                    await CloudTableService.Insert(profile, tableName);

                    return(profile.Id);
                }

                throw new Exception($"Failed request : { responseBytes } ");
            }
        }
コード例 #26
0
        public async Task <string> IdentifySpeaker(IdentifyProfile model)
        {
            var profiles = await GetEnrolledProfiles();

            var identificationProfileIds = string.Join(",", profiles.Select(x => x.Id));

            var url = $"{Configuration["AudioAnalyticsAPI"]}identify?identificationProfileIds={identificationProfileIds}";

            var key = Configuration["AudioAnalyticsKey"];

            using (var stream = model.Audio.OpenReadStream())
            {
                var response = await CognitiveServicesHttpClient.HttpPostAudio(stream, url, key);

                var responseBytes = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    return(response.Headers.GetValues("Operation-Location").FirstOrDefault());
                }

                throw new Exception($"Failed request : {responseBytes} ");
            }
        }