public static string Main1(string s)
        {
            string model    = null;
            string audio    = null;
            string hotwords = null;
            bool   extended = false;

            model = "C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.pbmm";
            var scorer = "C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.scorer";

            audio = "C:/Users/Nour El-Din/Documents/deepspeech/audio";
            //hotwords = GetArgument(args, "--hot_words");
            extended = true;
            Stopwatch stopwatch = new Stopwatch();

            try
            {
                Console.WriteLine("Loading model...");
                stopwatch.Start();
                // sphinx-doc: csharp_ref_model_start
                using (IDeepSpeech sttClient = new DeepSpeech("C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.pbmm"))
                {
                    // sphinx-doc: csharp_ref_model_stop
                    stopwatch.Stop();
                    Console.WriteLine($"Model loaded - {stopwatch.Elapsed.Milliseconds} ms");
                    stopwatch.Reset();
                    if (scorer != null)
                    {
                        Console.WriteLine("Loading scorer...");
                        sttClient.EnableExternalScorer("C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.scorer");
                    }

                    if (hotwords != null)
                    {
                        Console.WriteLine($"Adding hot-words {hotwords}");
                        char[]   sep         = { ',' };
                        string[] word_boosts = hotwords.Split(sep);
                        foreach (string word_boost in word_boosts)
                        {
                            char[]   sep1 = { ':' };
                            string[] word = word_boost.Split(sep1);
                            sttClient.AddHotWord(word[0], float.Parse(word[1]));
                        }
                    }
                    Directory.SetCurrentDirectory("C:/Users/Nour El-Din/Downloads/Graduation-Project-Dragons-master/Graduation Project Dragons/wwwroot/Video");
                    string v_Name           = s.Split('.')[0];
                    var    enviroment       = System.Environment.CurrentDirectory;
                    string projectDirectory = Directory.GetParent(enviroment).Parent.FullName;
                    Console.WriteLine(projectDirectory);
                    string m_Path    = @$ "C:/Users/Nour El-Din/Downloads/Graduation-Project-Dragons-master/Graduation Project Dragons/wwwroot/Video/{v_Name}.mp3";
                    bool   mp3_Found = File.Exists(m_Path) ? true : false;
                    string w_Path    = @$ "C:/Users/Nour El-Din/Downloads/Graduation-Project-Dragons-master/Graduation Project Dragons/wwwroot/Video/{v_Name}.wav";
                    bool   wav_Found = File.Exists(w_Path) ? true : false;
                    string strCmdText;
                    if (!mp3_Found)
                    {
                        strCmdText = $"/c ffmpeg -i {v_Name}.mp4 {v_Name}.mp3";
                        System.Diagnostics.Process.Start("CMD.exe", strCmdText);
                    }
                    else if (!wav_Found)
                    {
                        strCmdText = $"/c ffmpeg -i {v_Name}.mp3 -acodec pcm_s16le -ac 1 -ar 16000 {v_Name}.wav";
                        System.Diagnostics.Process.Start("CMD.exe", strCmdText);
                    }
                    else
                    {
                        string audioFile  = $"C:/Users/Nour El-Din/Downloads/Graduation-Project-Dragons-master/Graduation Project Dragons/wwwroot/Video/{v_Name}.wav";
                        var    waveBuffer = new WaveBuffer(File.ReadAllBytes(audioFile));
                        using (var waveInfo = new WaveFileReader(audioFile))
                        {
                            Console.WriteLine("Running inference....");

                            stopwatch.Start();
                            string w = "";
                            string speechResult;
                            // sphinx-doc: csharp_ref_inference_start
                            if (extended)
                            {
                                Metadata metaResult = sttClient.SpeechToTextWithMetadata(waveBuffer.ShortBuffer,
                                                                                         Convert.ToUInt32(waveBuffer.MaxSize / 2), 1);
                                speechResult = MetadataToString(metaResult.Transcripts[0]);
                                data        new_Data   = new data();
                                List <data> full_Text  = new List <data>();
                                double      word_Start = 0;;
                                double      word_End;
                                string      temp     = "";
                                int         counter  = 0;
                                int         counter2 = 0;
                                foreach (var c in metaResult.Transcripts[0].Tokens)
                                {
                                    counter2++;
                                    if (counter == 0)
                                    {
                                        word_Start = c.StartTime;
                                        counter++;
                                    }
                                    temp += c.Text;
                                    if (c.Text == " ")
                                    {
                                        counter        = 0;
                                        word_End       = c.StartTime;
                                        new_Data.start = word_Start;
                                        new_Data.end   = word_End;
                                        new_Data.word  = temp;
                                        full_Text.Add(new_Data);
                                        temp     = "";
                                        new_Data = new data();

                                        continue;
                                    }
                                    w += c.Text;
                                    if (counter2 == metaResult.Transcripts[0].Tokens.Length - 1)
                                    {
                                        word_End       = c.StartTime;
                                        new_Data.start = word_Start;
                                        new_Data.end   = word_End;
                                        new_Data.word  = temp;
                                        full_Text.Add(new_Data);
                                        temp = "";
                                    }
                                }
                            }
                            else
                            {
                                speechResult = sttClient.SpeechToText(waveBuffer.ShortBuffer,
                                                                      Convert.ToUInt32(waveBuffer.MaxSize / 2));
                            }
                            // sphinx-doc: csharp_ref_inference_stop
                            stopwatch.Stop();
                            Console.WriteLine($"Audio duration: {waveInfo.TotalTime.ToString()}");
                            Console.WriteLine($"Inference took: {stopwatch.Elapsed.ToString()}");
                            Console.WriteLine((extended ? $"Extended result: " : "Recognized text: ") + speechResult);
                            return(w);
                        }
                        waveBuffer.Clear();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(" ");
        }
Ejemplo n.º 2
0
        public static string Main1(string s)
        {
            string model    = null;
            bool   extended = false;

            model = "C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.pbmm";
            var scorer = "C:/Users/Nour El-Din/Documents/deepspeech/deepspeech-0.9.3-models.scorer";

            extended = true;
            string    wwwPath   = HomeController.Environment.WebRootPath;
            string    path      = Path.Combine(HomeController.Environment.WebRootPath, "Video");
            Stopwatch stopwatch = new Stopwatch();

            try
            {
                Console.WriteLine("Loading model...");
                stopwatch.Start();
                // sphinx-doc: csharp_ref_model_start
                using (IDeepSpeech sttClient = new DeepSpeech(model))
                {
                    // sphinx-doc: csharp_ref_model_stop
                    stopwatch.Stop();
                    Console.WriteLine($"Model loaded - {stopwatch.Elapsed.Milliseconds} ms");
                    stopwatch.Reset();
                    if (scorer != null)
                    {
                        Console.WriteLine("Loading scorer...");
                        sttClient.EnableExternalScorer(scorer);
                    }
                    Directory.SetCurrentDirectory(path);
                    string v_Name    = s.Split('.')[0];
                    string x         = path + @$ "/{v_Name}.mp4";
                    string m_Path    = path + @$ "/{v_Name}.mp3";
                    bool   mp3_Found = File.Exists(m_Path) ? true : false;
                    string w_Path    = path + @$ "/{v_Name}.wav";
                    bool   wav_Found = File.Exists(w_Path) ? true : false;
                    string strCmdText;
                    if (!mp3_Found)
                    {
                        strCmdText = $"/c ffmpeg -i {v_Name}.mp4 {v_Name}.mp3";
                        Process process = new Process();
                        process.StartInfo.FileName    = "cmd.exe";
                        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        process.StartInfo.Arguments   = strCmdText;
                        process.Start();
                        process.WaitForExit();
                    }

                    if (!wav_Found)
                    {
                        strCmdText = $"/c ffmpeg -i {v_Name}.mp3 -acodec pcm_s16le -ac 1 -ar 16000 {v_Name}.wav";
                        Process process = new Process();
                        process.StartInfo.FileName    = "cmd.exe";
                        process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        process.StartInfo.Arguments   = strCmdText;
                        process.Start();
                        process.WaitForExit();
                    }
                    string audioFile  = path + $"/{v_Name}.wav";
                    var    waveBuffer = new WaveBuffer(File.ReadAllBytes(audioFile));
                    using (var waveInfo = new WaveFileReader(audioFile))
                    {
                        Console.WriteLine("Running inference....");
                        stopwatch.Start();
                        string w = "";
                        string speechResult;
                        // sphinx-doc: csharp_ref_inference_start
                        if (extended)
                        {
                            Metadata metaResult = sttClient.SpeechToTextWithMetadata(waveBuffer.ShortBuffer,
                                                                                     Convert.ToUInt32(waveBuffer.MaxSize / 2), 1);
                            speechResult = MetadataToString(metaResult.Transcripts[0]);
                            data        new_Data   = new data();
                            List <data> full_Text  = new List <data>();
                            double      word_Start = 0;;
                            double      word_End;
                            string      temp     = "";
                            int         counter  = 0;
                            int         counter2 = 0;
                            int         count    = 0;
                            foreach (var c in metaResult.Transcripts[0].Tokens)
                            {
                                counter2++;
                                if (counter == 0)
                                {
                                    word_Start = c.StartTime;
                                    counter++;
                                }
                                temp += c.Text;
                                if (c.Text == " ")
                                {
                                    counter        = 0;
                                    word_End       = c.StartTime;
                                    new_Data.start = word_Start;
                                    new_Data.end   = word_End;
                                    new_Data.word  = temp;
                                    full_Text.Add(new_Data);
                                    w       += temp + ',';
                                    w       += (double)word_Start + ",";
                                    w       += (double)word_End + "/";
                                    temp     = "";
                                    new_Data = new data();

                                    continue;
                                }
                                //w += c.Text;
                                if (counter2 == metaResult.Transcripts[0].Tokens.Length)
                                {
                                    word_End       = c.StartTime;
                                    new_Data.start = word_Start;
                                    new_Data.end   = word_End;
                                    new_Data.word  = temp;
                                    full_Text.Add(new_Data);
                                    w   += temp + ',';
                                    w   += (double)word_Start + ",";
                                    w   += (double)word_End + "/";
                                    temp = "";
                                }
                                count++;
                            }
                        }
                        else
                        {
                            speechResult = sttClient.SpeechToText(waveBuffer.ShortBuffer,
                                                                  Convert.ToUInt32(waveBuffer.MaxSize / 2));
                        }
                        // sphinx-doc: csharp_ref_inference_stop
                        stopwatch.Stop();
                        Console.WriteLine($"Audio duration: {waveInfo.TotalTime.ToString()}");
                        Console.WriteLine($"Inference took: {stopwatch.Elapsed.ToString()}");
                        Console.WriteLine((extended ? $"Extended result: " : "Recognized text: ") + speechResult);
                        waveBuffer.Clear();
                        return(w);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(" ");
        }