Esempio n. 1
0
        public async Task <SpeechTranslationResult> TranslateSpeech(ClientTranslationInput input)
        {
            _input = input;
            //_audioInput = Convert.ToBase64String(File.ReadAllBytes(_input.Base64String));
            //_audioInput = _input.Base64String;

            Byte[] b = Convert.FromBase64String(input.Base64String);
            System.IO.File.WriteAllBytes(@"speak3.wav", b);
            _audioInput = Convert.ToBase64String(File.ReadAllBytes("speak3.wav"));

            this.Result = await Translate();

            return(this.Result);
        }
        public async Task <IActionResult> AzureTranslate([FromBody] ClientTranslationInput clientInput)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            //Language can be set in the url
            string[] input = new[] { $"https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language={clientInput.InputLanguage}&format=detailed", clientInput.Base64String };
            SpeechRecognitionResult textResult = _bingSpeechService.ParseSpeectToText(input);

            string detectedText = "";

            try
            {
                JObject json = JObject.Parse(textResult.JSONResult);
                detectedText = json["NBest"][0]["Display"].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            if (string.IsNullOrEmpty(detectedText))
            {
                sw.Stop();
                return(BadRequest("No text detected"));
            }

            TextTranslationResult result = await Task.Run(() => _azureTextTranslatorService.TranslateText(detectedText, clientInput.OutputLanguages.ToList()));

            if (result == null)
            {
                sw.Stop();
                return(BadRequest("An unknown error has occured"));
            }

            sw.Stop();
            result.TotalBackendTimeInMilliseconds = sw.ElapsedMilliseconds;
            result.DetectedText = detectedText;
            return(Ok(result));
        }
        public async Task <IActionResult> WatsonTranslate([FromBody] ClientTranslationInput clientInput)
        {
            var       input = new string[] { clientInput.Base64String };
            Stopwatch sw    = new Stopwatch();

            sw.Start();

            SpeechRecognitionResult textResult = _watsonSpeechToTextService.ParseSpeectToText(input);

            string detectedText = "";

            try
            {
                JObject json = JObject.Parse(textResult.JSONResult);
                detectedText = json["results"][0]["alternatives"][0]["transcript"].ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            if (string.IsNullOrEmpty(detectedText))
            {
                sw.Stop();
                return(BadRequest("No text detected"));
            }

            TextTranslationResult result = await Task.Run(() => _watsonTextTranslationService.TranslateText(detectedText, clientInput.InputLanguage, clientInput.OutputLanguages.ToList()));

            if (result == null)
            {
                sw.Stop();
                return(BadRequest("An unknown error has occured"));
            }

            sw.Stop();
            result.TotalBackendTimeInMilliseconds = sw.ElapsedMilliseconds;
            result.DetectedText = detectedText;
            return(Ok(result));
        }