Exemple #1
0
        /// <summary>
        /// Starts a speech recognition session through the custom UI.
        /// </summary>
        private async void SpeakButton_Click(object sender, RoutedEventArgs e)
        {
            // Always call RecognizeSpeechToTextAsync from inside
            // a try block because it calls a web service.
            try
            {
                while (true)
                {
                    SpeechRecognitionResult result = await SR.RecognizeSpeechToTextAsync();

                    // Write the result to the TextBlock.
                    if (result.TextConfidence != SpeechRecognitionConfidence.Rejected)
                    {
                        speechCache.Add(result.Text);
                        ResultText.Text = string.Join("\n", speechCache);
                    }
                    else
                    {
                        ResultText.Text = "Sorry, I did not get that.";
                    }
                }
            }
            catch (Exception ex)
            {
                // If there's an exception, show the Type and Message.
                ResultText.Text = string.Format("{0}: {1}", ex.GetType(), ex.Message);
            }
        }
Exemple #2
0
        private async void SpeakButton_Click(object sender, RoutedEventArgs e)
        {
            this.cancelled = false;
            AlternatesListBox.ItemsSource = null;

            // Use a try block because RecognizeSpeechToTextAsync depends on a web service.
            try
            {
                // Start speech recognition.
                var result = await SR.RecognizeSpeechToTextAsync();

                // Show the TextConfidence.
                if (weatherq)
                {
                    ConfidenceText.Text = Globals.weather;
                }
                else
                {
                    ConfidenceText.Text = "";
                }

                // Display the text.
                FinalResult.Text = result.Text;

                //place text in a Global Variable
                Globals.text = FinalResult.Text;

                // Fill a string array with the alternate results.
                var alternates = result.GetAlternates(5);
                doTask(Globals.text);
                if (alternates.Count > 1)
                {
                    string[] s = new string[alternates.Count];
                    for (int i = 1; i < alternates.Count; i++)
                    {
                        s[i] = alternates[i].Text;
                    }

                    // Populate the alternates ListBox with the array.
                    AlternatesListBox.ItemsSource = s;
                    AlternatesTitle.Visibility    = Visibility.Visible;
                }
                else
                {
                    AlternatesTitle.Visibility = Visibility.Collapsed;
                }
            }
            catch (Exception ex)
            {
                // If there's an exception, show it in the Complete panel.
                if (ex.GetType() != typeof(OperationCanceledException))
                {
                    FinalResult.Text = string.Format("{0}: {1}",
                                                     ex.GetType().ToString(), ex.Message);
                    SetPanel(CompletePanel);
                }
            }
        }
        private async void SpeakButton_Click(object sender, RoutedEventArgs e)
        {
            // Always use a try block because RecognizeSpeechToTextAsync
            // depends on a web service.
            try
            {
                /*
                 *  Use this object to do wonderful things, e.g.:
                 *    - get user email => user.Profile["email"].ToString()
                 *    - get facebook/google/twitter/etc access token => user.Profile["identities"][0]["access_token"]
                 *    - get Windows Azure AD groups => user.Profile["groups"]
                 *    - etc.
                 */

                // Start speech recognition.
                var result = await SR.RecognizeSpeechToTextAsync();

                // Display the text.
                FinalResult.Text = result.Text;

                // Show the TextConfidence.
                ShowConfidence(result.TextConfidence);

                // Fill a string array with the alternate results.
                var alternates = result.GetAlternates(5);
                if (alternates.Count > 1)
                {
                    string[] s = new string[alternates.Count];
                    for (int i = 1; i < alternates.Count; i++)
                    {
                        s[i] = alternates[i].Text;
                    }

                    // Populate the alternates ListBox with the array.
                    AlternatesListBox.ItemsSource = s;
                    AlternatesTitle.Visibility    = Visibility.Visible;
                }
                else
                {
                    AlternatesTitle.Visibility = Visibility.Collapsed;
                }

                //AlternatesListBox.ItemsSource = result.GetAlternates(5);
            }
            catch (Exception ex)
            {
                // If there's an exception, show it in the Complete panel.
                if (ex.GetType() != typeof(OperationCanceledException))
                {
                    FinalResult.Text = string.Format("{0}: {1}",
                                                     ex.GetType().ToString(), ex.Message);
                    SetPanel(CompletePanel);
                }
            }
        }
        async private void StartRecButton_Click(object sender, RoutedEventArgs e)
        {
            // Prevent concurrent calls to an async method
            StartRecButton.IsEnabled = false;

            // Reset all the text
            VolumeTextBlock.Text              = "";
            CaptureStateTextBlock.Text        = "";
            IntermediateResultsTextBlock.Text = "";
            ConfidenceTextBlock.Text          = "";
            FinalResultTextBlock.Text         = "";
            AlternatesTextBlock.Text          = "";

            // Use a try block because RecognizeSpeechToTextAsync depends on a web service which can throw exceptions.
            try
            {
                // Start speech recognition and await the result
                // As this is occuring, the RecognizerResultReceived will fire as the user is speaking.
                var result = await speechRec.RecognizeSpeechToTextAsync();

                // Show the TextConfidence.
                ConfidenceTextBlock.Text = "Confidence: " + Enum.GetName(typeof(SpeechRecognitionConfidence), result.TextConfidence);

                // Display the text.
                if (result.Text != null)
                {
                    FinalResultTextBlock.Text = result.Text;
                }

                // Fill a string with the alternate results.
                var alternates = result.GetAlternates(5);
                if (alternates.Count > 1)
                {
                    string s = "";
                    for (int i = 1; i < alternates.Count; i++)
                    {
                        s += "\n" + alternates[i].Text;
                    }
                    AlternatesTextBlock.Text = "Alternates: " + s;
                }
            }
            catch (Exception ex)
            {
                // If there's an exception, show it instead of the Final Result.
                if (ex.GetType() != typeof(OperationCanceledException))
                {
                    FinalResultTextBlock.Text = string.Format("{0}: {1}",
                                                              ex.GetType().ToString(), ex.Message);
                }
            }

            // Finished recording, allow recording again.
            StartRecButton.IsEnabled = true;
        }
Exemple #5
0
        private async void SpeakButton_Click(object sender, RoutedEventArgs e)
        {
            String s;

            try
            {
                var result = await SR.RecognizeSpeechToTextAsync();

                s = result.Text;
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] == ' ')
                    {
                        s = s.Replace(' ', '_');
                    }
                    else if (s[i] == '_')
                    {
                        continue;
                    }
                    else if (s[i] < 65 || (s[i] > 90 && s[i] < 97))
                    {
                        s = s.Remove(i, 1);
                    }
                    else if (s[i] > 122)
                    {
                        s = s.Remove(i, 1);
                    }
                }
                ResultText.Text = result.Text;

                HttpClient httpClient = new HttpClient();
                string     url        = "http://127.0.0.1:8000/ml/" + s;

                var response = await httpClient.GetAsync(url);

                // var response = await httpClient.GetAsync("http://192.168.1.121:8000/ml/i_am_happy");
                response.EnsureSuccessStatusCode();
                string content = await response.Content.ReadAsStringAsync();

                //ResultText.Text = content;
                string[] tokens = content.Split(':', ',');
                string   mood;
                //string comp = tokens[1];
                tokens[1] = tokens[1].Trim().Substring(1, tokens[1].Length - 3);
                //BoxText.Text = tokens[1];
                // LayoutRoot.Background = background;

                if (Double.Parse(tokens[1], CultureInfo.CurrentCulture) == 0)
                {
                    mood = "Undetermined";
                }

                else if (Double.Parse(tokens[1], CultureInfo.CurrentCulture) >= 0.5)
                {
                    mood = "Happy";
                }
                else if (Double.Parse(tokens[1], CultureInfo.CurrentCulture) < 0.5 && Double.Parse(tokens[1], CultureInfo.CurrentCulture) > 0)
                {
                    mood = "Cheerful";
                }
                else if (Double.Parse(tokens[1], CultureInfo.CurrentCulture) < 0 && Double.Parse(tokens[1], CultureInfo.CurrentCulture) >= -0.5)
                {
                    mood = "Gloomy";
                }
                else
                {
                    mood = "Sad";
                }

                BoxText.Text = mood;
            }
            catch (Exception ex)
            {
                s = "";
                ResultText.Text = "Please Try Again";
            }
        }