public static void TranscriptTranslation() { //Console.WriteLine("Hello World!"); var pathToVideo = @"C:\Users\v-isbojo\Pictures\OtherLangVideo\aa.mp4"; var video = Video.ImportVideo(pathToVideo); var audio = ffmpeg.ExtractAudio(video); var secretProvider = new SecretProvider(translationkeySecretIdentifier, subscriptionKeySecretIdentifier, applicationId); Transcript transcript = new Transcript(secretProvider, audio, Transcript.Language.EnglishUS); Translation translate = new Translation(secretProvider, transcript); Console.WriteLine("Press any key to continue."); Console.ReadKey(); }
//private static readonly string endpoint = Environment.GetEnvironmentVariable(endpoint_var); // This sample requires C# 7.1 or later for async/await. // Async call to the Translator Text APIS public async Task TranslateTextRequest( SecretProvider secretProvider, Language[] listOfLanguages = default(Language[]), string route = "/translate?api-version=3.0") { foreach (Language lang in listOfLanguages) { route += "&to=" + LanguageCode(lang); } object[] body = new object[] { new { Text = textToBeTranslated } }; var requestBody = JsonConvert.SerializeObject(body); using (var client = new HttpClient()) using (var request = new HttpRequestMessage()) { // Build the request. // Set the method to Post. request.Method = HttpMethod.Post; // Construct the URI and add headers. request.RequestUri = new Uri(endpoint_var + route); request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); request.Headers.Add("Ocp-Apim-Subscription-Key", secretProvider.GetTranslationKey()); // Send the request and get response. HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false); // Read response as a string. string result = await response.Content.ReadAsStringAsync(); // Deserialize the response using the classes created earlier. TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject <TranslationResult[]>(result); // Iterate over the deserialized results. foreach (TranslationResult o in deserializedOutput) { // Print the detected input language and confidence score. Console.WriteLine("Detected input language: {0}\nConfidence score: {1}\n", o.DetectedLanguage.Language, o.DetectedLanguage.Score); // Iterate over the results and print each translation. foreach (TranslationText t in o.Translations) { translations.Add(t); Console.WriteLine("Translated to {0}: {1}", t.To, t.Text); } } } }
public Translation(SecretProvider secretProvider, Transcript transcript, Language[] languages = default(Language[])) { if (languages == default(Language[])) { listOfLanguages = new Language[] { Language.German, Language.Italian, Language.SerbianLatin }; } else { listOfLanguages = languages; } textToBeTranslated = transcript.TranscriptBulkText.Value; try { TranslateTextRequest(secretProvider, listOfLanguages).Wait(); } catch (Exception ex) { _ = ex; } }
public static void TestingTranscript() { var secretProvider = new SecretProvider(translationkeySecretIdentifier, subscriptionKeySecretIdentifier, applicationId); //write value // Write the string array to a new file named "WriteLines.txt". using (StreamWriter outputFile = new StreamWriter(Path.Combine("E:\\diplomskiTesting", "HeyHeyHeyIspravljeno.txt"))) { string[] foldersInDirectory = Directory.GetDirectories("E:\\diplomskiTesting"); float wordErrorRateSum = 0; int testCount = 0; foreach (var folder in foldersInDirectory) { string[] subfoldersInDirectory = Directory.GetDirectories(folder); string testFile = ""; foreach (var subFolder in subfoldersInDirectory) { var allFiles = Directory.GetFiles(subFolder); Parallel.ForEach(allFiles, (file) => { if (file.EndsWith(".mp4")) { try { var video = Video.ImportVideo(file); var audio = ffmpeg.ExtractAudio(video); Transcript transcript = new Transcript(secretProvider, audio, Path.GetFileName(folder)); testFile = allFiles.Where((f) => f.Contains(Path.GetFileNameWithoutExtension(file))).Where(x => x.EndsWith(".vtt")).FirstOrDefault(); var wer = WordErrorRate(testFile, transcript.TranscriptBulkText.Value); wordErrorRateSum += wer; outputFile.WriteLine("WER for file " + file + " " + wer); testCount++; Console.WriteLine("Press any key to continue."); //Console.ReadKey(); } catch (Exception ex) { outputFile.WriteLine("Skipping " + file); Console.WriteLine("Skipping " + file); } } }); //foreach (var file in allFiles) //{ //} } outputFile.WriteLine(Path.GetFileName(folder)); outputFile.WriteLine(wordErrorRateSum); outputFile.WriteLine(testCount); outputFile.WriteLine(wordErrorRateSum / testCount); } outputFile.WriteLine("KRAJ"); outputFile.WriteLine(wordErrorRateSum); outputFile.WriteLine(testCount); outputFile.WriteLine(wordErrorRateSum / testCount); } }
private async Task RecognizeSpeechAsync( SecretProvider secretProvider, string pathWav = @"C:\Users\v-isbojo\Pictures\OtherLangVideo\frfr_output.wav", string cogServiceOption = "simple", string speechLanguage = "en-us" ) { var config = SpeechConfig.FromSubscription(secretProvider.GetSubscriptionKey(), "francecentral"); config.SpeechRecognitionLanguage = speechLanguage; switch (cogServiceOption) { case "simple": config.OutputFormat = OutputFormat.Simple; break; case "detailedtext": case "detailedbest": config.OutputFormat = OutputFormat.Detailed; break; default: throw new ArgumentException(); } var stopRecognition = new TaskCompletionSource <int>(); using (var audioConfig = AudioConfig.FromWavFileInput(pathWav)) { using (var recognizer = new SpeechRecognizer(config, audioConfig)) { // Subscribes to events. recognizer.Recognizing += (s, e) => { Console.WriteLine($"\n Partial result: {e.Result.Text}."); }; recognizer.Recognized += (s, e) => { if (e.Result.Reason == ResultReason.RecognizedSpeech) { switch (cogServiceOption) { case "simple": case "detailedtext": Console.WriteLine($"RECOGNIZED: Text={e.Result.Text}"); AddLineInfo(new TranscriptLineInfo(e.Result.Text, e.Result.OffsetInTicks, e.Result.Duration)); break; case "detailedbest": Console.WriteLine($"RECOGNIZED: Text={e.Result.Best().FirstOrDefault()?.Text}"); AddLineInfo(new TranscriptLineInfo(e.Result.Best().FirstOrDefault()?.Text, e.Result.OffsetInTicks, e.Result.Duration)); break; default: throw new ArgumentException(); } } else if (e.Result.Reason == ResultReason.NoMatch) { Console.WriteLine($"NOMATCH: Speech could not be recognized."); } }; recognizer.Canceled += (s, e) => { Console.WriteLine($"CANCELED: Reason={e.Reason}"); if (e.Reason == CancellationReason.Error) { Console.WriteLine($"CANCELED: ErrorDetails={e.ErrorDetails}"); Console.WriteLine($"CANCELED: Did you update the subscription info?"); } stopRecognition.TrySetResult(0); }; recognizer.SessionStarted += (s, e) => { Console.WriteLine("\n Session started event."); }; recognizer.SessionStopped += (s, e) => { Console.WriteLine("\n Session stopped event."); Console.WriteLine("\nStop recognition."); stopRecognition.TrySetResult(0); }; // Starts continuous recognition. Uses StopContinuousRecognitionAsync() to stop recognition. await recognizer.StartContinuousRecognitionAsync().ConfigureAwait(false); // Waits for completion. // Use Task.WaitAny to keep the task rooted. Task.WaitAny(new[] { stopRecognition.Task }); // Stops recognition. await recognizer.StopContinuousRecognitionAsync().ConfigureAwait(false); } } //string pathTranscript = pathWav.Substring(0, pathWav.Length - 3) + "txt"; System.IO.File.WriteAllText(Path.Combine(Path.GetDirectoryName(pathWav), Path.GetFileNameWithoutExtension(pathWav)) + ".txt", TranscriptBulkText.Value); }
public Transcript(SecretProvider secretProvider, Audio audio, string lang) { RecognizeSpeechAsync(secretProvider, audio.Path, "detailedbest", lang).Wait(); }
public Transcript(SecretProvider secretProvider, Audio audio, Language lang) { RecognizeSpeechAsync(secretProvider, audio.Path, "detailedbest", LanguageCode(lang)).Wait(); }