private async void GetEnrollmentOperationStatus(OperationLocation location)
        {
            try
            {
                while (true)
                {
                    EnrollmentOperation result = await _speakerIdentificationClient.CheckEnrollmentStatusAsync(location);

                    if (result.Status != Status.Running)
                    {
                        RaiseOnIdentificationStatusUpdated(new SpeakerIdentificationStatusUpdateEventArgs(result.Status.ToString(),
                                                                                                          $"Enrollment finished. Enrollment status: {result.ProcessingResult.EnrollmentStatus.ToString()}"));

                        break;
                    }

                    RaiseOnIdentificationStatusUpdated(new SpeakerIdentificationStatusUpdateEventArgs(result.Status.ToString(), "Enrolling..."));

                    await Task.Delay(1000);
                }
            }
            catch (IdentificationException ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to get operation status: {ex.Message}"));
            }
            catch (Exception ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to get operation status: {ex.Message}"));
            }
        }
Beispiel #2
0
        internal void Insert()
        {
            var database = client.GetDatabase(databaseName);
            IMongoCollection <Operation> collection = database.GetCollection <Operation>(collectionName);
            var result = new List <Operation>(operationCount);

            for (int i = 0; i < operationCount; i++)
            {
                var operation = new Operation
                {
                    Id             = Guid.NewGuid(),
                    Comment        = $"{i}",
                    CreatedOn      = DateTime.Now,
                    OperationState = Beezie.Domain.Enums.OperationStates.Finalized,
                    OperationType  = i % 1000 == 0 ? Beezie.Domain.Enums.OperationTypes.Inventory : Beezie.Domain.Enums.OperationTypes.Out,
                };
                var operationLocations = new List <OperationLocation>();

                for (int j = 0; j < operationLocationCount; j++)
                {
                    var operationLocation = new OperationLocation
                    {
                        Id = Guid.NewGuid(),
                        OperationLocationType = operation.OperationType == Beezie.Domain.Enums.OperationTypes.Inventory
                        ? Beezie.Domain.Enums.OperationLocationTypes.Inventory
                        : Beezie.Domain.Enums.OperationLocationTypes.Sale,
                        StorageId = 42,
                    };

                    var operationDetails = new List <OperationDetail>();
                    for (int k = 0; k < operationDetailCount; k++)
                    {
                        var operationDetail = new OperationDetail
                        {
                            Id                = Guid.NewGuid(),
                            CreatedOn         = DateTime.Now,
                            FinalizedOn       = DateTime.Now.AddMinutes(5),
                            PaidOn            = DateTime.Now.AddMinutes(5),
                            PriceId           = 42,
                            ProductLocationId = k,
                            Quantity          = 42 * k + 1
                        };
                        operationDetails.Add(operationDetail);
                    }
                    operationLocation.OperationDetails = operationDetails;
                    operationLocations.Add(operationLocation);
                }

                operation.OperationLocations = operationLocations;
                result.Add(operation);
            }
            collection.InsertMany(result);

            Console.WriteLine($"insert {operationCount}*{operationLocationCount}*{operationDetailCount} = {operationCount * operationLocationCount * operationDetailCount}");
        }
 public virtual IdentificationOperation CheckIdentificationStatus(OperationLocation location)
 {
     return(PolicyService.ExecuteRetryAndCapture400Errors(
                "SpeakerVerificationService.CheckIdentificationStatus",
                ApiKeys.SpeakerRecognitionRetryInSeconds,
                () =>
     {
         var result = SpeakerIdentificationRepository.CheckIdentificationStatus(location);
         return result;
     },
                null));
 }
Beispiel #4
0
        /// <summary>
        /// Gets the enrollment operation status or result asynchronously
        /// </summary>
        /// <param name="location">The location returned upon enrollment</param>
        /// <exception cref="EnrollmentException">Thrown in case of internal server error or an invalid url</exception>
        /// <exception cref="TimeoutException">Thrown in case the connection timed out</exception>
        /// <returns>The enrollment object encapsulating the result</returns>
        public async Task <EnrollmentOperation> CheckEnrollmentStatusAsync(OperationLocation location)
        {
            try
            {
                EnrollmentOperation operation = await CheckStatusAsync <EnrollmentOperation, EnrollmentException>(location.Url).ConfigureAwait(false);

                return(operation);
            }
            catch (TaskCanceledException exception)
            {
                throw new TimeoutException("Connection timed out: " + exception.Message);
            }
        }
Beispiel #5
0
        // ================

        public OperationContext(
            CancellationToken cancellationToken = default(CancellationToken),
            TimeSpan?timeout = null,
            [CallerMemberName] string memberName = null,
            [CallerFilePath] string filePath     = null,
            [CallerLineNumber] int lineNumber    = 0
            )
        {
            CancellationToken = cancellationToken;
            _values           = OperationValueDictionary.Empty;
            _created          = _tick.Elapsed;
            _timeout          = timeout ?? TimeSpan.FromMinutes(5); // Azure function default timeout
            _location         = new OperationLocation(memberName, filePath, lineNumber);
        }
        public virtual IdentificationOperation CheckIdentificationStatus(OperationLocation location)
        {
            try
            {
                var result = SpeakerIdentificationRepository.CheckIdentificationStatus(location);

                return(result);
            }
            catch (Exception ex)
            {
                Logger.Error("SpeakerIdentificationService.CheckIdentificationStatus failed", this, ex);
            }

            return(null);
        }
Beispiel #7
0
        /// <summary>
        /// Enrolls a speaker profile from an audio stream asynchronously
        /// </summary>
        /// <param name="audioStream">The audio stream to use for enrollment</param>
        /// <param name="id">The speaker profile ID to enroll</param>
        /// <exception cref="EnrollmentException">Thrown in case of invalid audio format, internal server error or an invalid ID</exception>
        /// <exception cref="TimeoutException">Thrown in case the connection timed out</exception>
        /// <returns>The operation location object encapsulating the Url that can be used to check the status of the operation</returns>
        public async Task <OperationLocation> EnrollAsync(Stream audioStream, Guid id)
        {
            try
            {
                // Send the request
                string            requestUri = _IDENTIFICATION_PROFILE_URI + "/" + id.ToString("D") + "/enroll";
                OperationLocation location   = await OperationAsync <EnrollmentException>(audioStream, requestUri).ConfigureAwait(false);

                return(location);
            }
            catch (TaskCanceledException exception)
            {
                throw new TimeoutException("Connection timed out: " + exception.Message);
            }
        }
        public virtual EnrollmentOperation CheckEnrollmentStatus(OperationLocation location)
        {
            try
            {
                var result = Task.Run(async() => await SpeakerIdentificationRepository.CheckEnrollmentStatusAsync(location)).Result;

                return(result);
            }
            catch (Exception ex)
            {
                Logger.Error("SpeakerIdentificationService.CheckEnrollmentStatus failed", this, ex);
            }

            return(null);
        }
Beispiel #9
0
        private void EnrollSpeaker(Stream stream)
        {
            // Reset pointer
            stream.Seek(0, SeekOrigin.Begin);

            SpeakerIdentificationServiceClient speakerIDClient = new SpeakerIdentificationServiceClient("c6b005dcf13e45b6a91485d38763277b");

            //Creating Speaker Profile...
            CreateProfileResponse creationResponse = speakerIDClient.CreateProfileAsync("en-US").Result;
            //Speaker Profile Created.
            //Retrieving The Created Profile...
            Profile profile = speakerIDClient.GetProfileAsync(creationResponse.ProfileId).Result;
            //Speaker Profile Retrieved."
            //Enrolling Speaker
            OperationLocation processPollingLocation = speakerIDClient.EnrollAsync(stream, profile.ProfileId, false).Result;

            EnrollmentOperation enrollmentResult;
            int      numOfRetries       = 10;
            TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0);

            while (numOfRetries > 0)
            {
                Task.Delay(timeBetweenRetries);
                enrollmentResult = speakerIDClient.CheckEnrollmentStatusAsync(processPollingLocation).Result;

                if (enrollmentResult.Status == Status.Succeeded)
                {
                    break;
                }
                else if (enrollmentResult.Status == Status.Failed)
                {
                    throw new EnrollmentException(enrollmentResult.Message);
                }
                numOfRetries--;
            }
            if (numOfRetries <= 0)
            {
                throw new EnrollmentException("Enrollment operation timeout.");
            }

            //Enrollment Done.
            // Store profile in memory cache
            ObjectCache memCache = MemoryCache.Default;
            var         profiles = memCache.Get("SpeakerProfiles") != null?memCache.Get("SpeakerProfiles") as List <Profile> : new List <Profile>();

            memCache.Remove("SpeakerProfiles");
            memCache.Add("SpeakerProfiles", profiles, DateTimeOffset.UtcNow.AddHours(2));
        }
Beispiel #10
0
 private OperationContext(
     OperationContext parent,
     CancellationToken cancellationToken,
     OperationValueDictionary values,
     TimeSpan created,
     TimeSpan timeout,
     OperationLocation location
     )
 {
     _parent           = parent;
     CancellationToken = cancellationToken;
     _values           = values;
     _created          = created;
     _timeout          = timeout;
     _location         = location;
 }
Beispiel #11
0
        /// <summary>
        /// Identifies an audio stream asynchronously
        /// </summary>
        /// <param name="audioStream">The audio stream to identify</param>
        /// <param name="ids">The list of possible speaker profile IDs to identify from</param>
        /// <exception cref="IdentificationException">Thrown on cases of internal server error, invalid IDs or wrong audio format</exception>
        /// <exception cref="TimeoutException">Thrown in case the connection timed out</exception>
        /// <returns>An object encapsulating the The Url that can be used to query the identification operation status</returns>
        public async Task <OperationLocation> IdentifyAsync(Stream audioStream, Guid[] ids)
        {
            try
            {
                // Construct the request URI
                string            testProfileIds = string.Join(",", ids.Select(id => id.ToString("D")));
                string            requestUri     = _IDENTIFICATION_URI + "?identificationProfileIds=" + testProfileIds;
                OperationLocation location       = await OperationAsync <IdentificationException>(audioStream, requestUri).ConfigureAwait(false);

                return(location);
            }
            catch (TaskCanceledException exception)
            {
                throw new TimeoutException("Connection timed out: " + exception.Message);
            }
        }
        /// <summary>
        ///     Identify a stream of audio
        /// </summary>
        /// <param name="stream">Audio buffer to be recognized</param>
        /// <param name="serviceClient">Client used in identifying the streamed audio wave</param>
        /// <param name="clientId">Client ID</param>
        /// <param name="requestId">Request ID</param>
        public async Task IdentifyStreamAsync(Stream stream, SpeakerIdentificationServiceClient serviceClient,
                                              Guid clientId, int requestId)
        {
            try
            {
                OperationLocation processPollingLocation = await serviceClient.IdentifyAsync(stream, _speakerIds, true).ConfigureAwait(false);

                var numberOfPollingRetries = 3;
                while (numberOfPollingRetries > 0)
                {
                    await Task.Delay(TimeSpan.FromSeconds(TimeSpanBetweenPollingRetries));

                    IdentificationOperation identificationResponse = await serviceClient.CheckIdentificationStatusAsync(processPollingLocation);

                    if (identificationResponse.Status == Status.Succeeded)
                    {
                        var result = new RecognitionResult(identificationResponse.ProcessingResult, clientId,
                                                           requestId);
                        _resultCallback(result);
                        break;
                    }

                    if (identificationResponse.Status == Status.Failed)
                    {
                        var failureResult = new RecognitionResult(false, identificationResponse.Message, requestId);

                        _resultCallback(failureResult);
                        return;
                    }

                    numberOfPollingRetries--;
                }


                if (numberOfPollingRetries <= 0)
                {
                    var failureResult = new RecognitionResult(false, "Request timeout.", requestId);
                    _resultCallback(failureResult);
                }
            }
            catch (Exception ex)
            {
                var result = new RecognitionResult(false, ex.Message, requestId);
                _resultCallback(result);
            }
        }
Beispiel #13
0
        private void RecognizeSpeaker(Stream stream)
        {
            // Reset pointer
            stream.Seek(0, SeekOrigin.Begin);

            SpeakerIdentificationServiceClient speakerIDClient = new SpeakerIdentificationServiceClient("c6b005dcf13e45b6a91485d38763277b");

            // Fetch existing profiles
            ObjectCache memCache = MemoryCache.Default;
            var         profiles = memCache.Get("SpeakerProfiles") != null?memCache.Get("SpeakerProfiles") as List <Profile> : new List <Profile>();

            List <Guid> testProfileIds = (from prof in profiles select prof.ProfileId).ToList();

            OperationLocation processPollingLocation = speakerIDClient.IdentifyAsync(stream, testProfileIds.ToArray(), false).Result;

            IdentificationOperation identificationResponse = null;
            int      numOfRetries       = 10;
            TimeSpan timeBetweenRetries = TimeSpan.FromSeconds(5.0);

            while (numOfRetries > 0)
            {
                Task.Delay(timeBetweenRetries);
                identificationResponse = speakerIDClient.CheckIdentificationStatusAsync(processPollingLocation).Result;

                if (identificationResponse.Status == Status.Succeeded)
                {
                    break;
                }
                else if (identificationResponse.Status == Status.Failed)
                {
                    throw new IdentificationException(identificationResponse.Message);
                }
                numOfRetries--;
            }
            if (numOfRetries <= 0)
            {
                throw new IdentificationException("Identification operation timeout.");
            }

            //"Identification Done."

            //Values now accessible!!
            var _identificationResultTxtBlk     = identificationResponse.ProcessingResult.IdentifiedProfileId.ToString();
            var _identificationConfidenceTxtBlk = identificationResponse.ProcessingResult.Confidence.ToString();
        }
        public async void IdentifySpeaker(Stream audioStream, Guid[] speakerIds)
        {
            try
            {
                OperationLocation location = await _speakerIdentificationClient.IdentifyAsync(audioStream, speakerIds);

                if (location == null)
                {
                    RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs("Failed to identify speaker."));
                    return;
                }

                GetIdentificationOperationStatus(location);
            }
            catch (IdentificationException ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to identify speaker: {ex.Message}"));
            }
            catch (Exception ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to identify speaker: {ex.Message}"));
            }
        }
        public async void CreateSpeakerEnrollment(Stream audioStream, Guid profileId)
        {
            try
            {
                OperationLocation location = await _speakerIdentificationClient.EnrollAsync(audioStream, profileId);

                if (location == null)
                {
                    RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs("Failed to start enrollment process."));
                    return;
                }

                GetEnrollmentOperationStatus(location);
            }
            catch (IdentificationException ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to add speaker enrollment: {ex.Message}"));
            }
            catch (Exception ex)
            {
                RaiseOnIdentificationError(new SpeakerIdentificationErrorEventArgs($"Failed to add speaker enrollment: {ex.Message}"));
            }
        }
Beispiel #16
0
        /// <summary>
        /// A helper method that's used to create an enrollment/identification request
        /// </summary>
        /// <typeparam name="E">The type of exception</typeparam>
        /// <param name="audioStream">The audio stream to enroll/identify from</param>
        /// <param name="requestUri">The Http endpoint for identification/enrollment</param>
        /// <returns>An operation object encoding the operation Url</returns>
        private async Task <OperationLocation> OperationAsync <E>(Stream audioStream, string requestUri) where E : Exception
        {
            var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString("u"));

            content.Add(new StreamContent(audioStream), "Data", "testFile_" + DateTime.Now.ToString("u"));
            HttpResponseMessage response = await _httpClient.PostAsync(requestUri, content).ConfigureAwait(false);

            if (response.StatusCode == HttpStatusCode.Accepted)
            {
                IEnumerable <string> operationLocation = response.Headers.GetValues(_OPERATION_LOCATION_HEADER);
                if (operationLocation.Count() == 1)
                {
                    string            operationUrl = operationLocation.First();
                    OperationLocation location     = new OperationLocation();
                    location.Url = operationUrl;
                    return(location);
                }
                else
                {
                    throw CreateExceptionFromType <E>("Incorrect server response");
                }
            }
            else
            {
                string resultStr = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                ErrorResponse errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(resultStr);
                if (errorResponse.Error != null)
                {
                    throw CreateExceptionFromType <E>(errorResponse.Error.Message);
                }
                else
                {
                    throw CreateExceptionFromType <E>(response.StatusCode.ToString());
                }
            }
        }
Beispiel #17
0
        public virtual EnrollmentOperation CheckEnrollmentStatus(OperationLocation location)
        {
            var response = RepositoryClient.SendGet(ApiKeys.SpeakerRecognition, location.Url);

            return(JsonConvert.DeserializeObject <EnrollmentOperation>(response));
        }
Beispiel #18
0
        public virtual async Task <IdentificationOperation> CheckIdentificationStatusAsync(OperationLocation location)
        {
            var response = await RepositoryClient.SendGetAsync(ApiKeys.SpeakerRecognition, location.Url);

            return(JsonConvert.DeserializeObject <IdentificationOperation>(response));
        }